npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bed-rock

v2.6.0

Published

A javascript unit testing framework

Downloads

79

Readme

Bedrock Build Status

NPM

           ,                  /\.__      _.-\
          /~\,      __       /~    \   ./    \
        ,/  /_\   _/  \    ,/~,_.~'"\ /_\_  /'\
       / \ /## \ / V#\/\  /~8#  # ## V8  #\/8 8\
     /~#'#"#""##V&#&# ##\/88#"#8# #" #\#&"##" ##\
    j# ##### #"#\&&"####/###&  #"#&## #&" #"#&#"#'\
   /#"#"#####"###'\&##"/&#"####"### # #&#&##"#"### \
  J#"###"#"#"#"####'\# #"##"#"##"#"#####&"## "#"&"##|\ 

Bedrock is a javascript BDD / TDD unit testing framework for Node.js.

It comprises a CLI test runner/reporter, assertion library and utilities including stubs and spies.

Installation

'bed-rock' is available as an npm package:

$ npm install bed-rock --save-dev

Add the following to scripts in package.json (with optional configuration flags):

'test' : 'bed-rock'

Run tests:

npm test

Bedrock will automatically load any files that match '*.spec.js'. This can be changed by passing a custom pattern (see configuration)

Configuration

Bedrock accepts several flags on startup. Note that bed-rock will need to be restarted if running in watch mode, if any of these flags are changed.

Set your test file extention (default spec)

--ext='my-test-ext'

Enable watch mode; Bedrock will automatically reload on file changes

--watch

Disable create of global window and document (DOM) objects

--nodom

Disable printing of the failure summary details

--nosumm

Usage

Bedrock exposes several core functions for composing tests:

context(description: string, tests: function)

a container for one or more tests (can be nested with other contexts)

test(description: string, assertions: function)

a container for a single test. Can contain one or more assertions. A spec whose expectations all succeed will be passing and a spec with any failures will fail

expect(subject: any)

creates an assertion for a test

expect(subject: any).not

creates an assertion for a test and reversed the evaluation logic

xcontext(description: string, tests: function)

a container for one or more tests (can be nested with other contexts) which will be ignored. NOTE: this overrides any nested focused tests (ftest) defined inside the context

xtest(description: string, assertions: function)

a container for a single test which will be ignored

ftest(description: string, assertions: function)

a container for a single test which will be focused. The presence of a single focused tests will cause any unfocused tests to be ignored.

spy(target: any, functionName: string)

a wrapper to spy on or stub an existing function or property of an object. See Spies & Stubs for more details

Basic example test file

var context = require('bed-rock').context;  
var test = require('bed-rock').test;  
var expect = require('bed-rock').expect;  

/* es6 import
import { context, test, expect } from 'bed-rock;
*/

context("GIVEN the state of the world", () => {
    let myTestSubject = 1;

    context("WHEN some action(s) have been performed")

    test('THEN I expect this outcome',() =>{
        expect(myTestSubject).toEqual(1);
    });

});

Comprehensive example usage of all matchers can be found in 'example-matchers.js' in the root directory. Note that exactly half of these example tests fail; this is deliberate and designed to show how each matcher can be set-up to both pass and fail.

Hooks

These must be declared before any tests in a context. Prefer variable assignment and function calls within these hooks rather.

setup(function)

runs once before all tests in a context

setupEach(function)

runs before each test in a context

teardown(function)

runs once after all tests in a context

tearDownEach(function)

runs after each test in a context

Matchers

toBe(value: any) subject and value are equal using '==='

toEqual(value: any) subject and value are deeply equal

toBeDefined() subject is defined

toBeUndefined() subject is undefined

toBeNull() subject is null

toBeNotNull() subject is not null

toBeGreaterThan(target: number) subject is greater than a target

toBeLessThan(target: number) subject is less than a target

toBeGreaterThanOrEqualTo(target: number) subject is greater than or equal to a target

toBeLessThanOrEqualTo(target: number) subject is less than or equal to a target

toBeBetweenInclusive(minimumThreshold: number, maximumThreshold: number) subject is between the minimumThreshold and maximumThreshold including these boundaries

toBeBetweenExclusive(minimumThreshold: number, maximumThreshold: number) subject is between the minimumThreshold and maximumThreshold excluding these boundaries

toBeCloseToInclusive(target: number, delta: number) subject is within the delta of the target, including boundaries

toBeCloseToExclusive(target: number, delta: number) subject is within the delta of the target, excluding boundaries

toBeTypeOf(name: string) subject is of type name

toRespondTo(name: string) subject has property or function of name

toHaveLength(length: number) subject (Array, Map, Set or String) has length

toHaveKey(key: any) subject (Map or Object) contains key

toContain(item: any) subject (Array, Set, Map, String) contains the item

toBeFalsey() subject evaluates to false in a boolean context

toBeTruthy() subject evaluates to true in a boolean context

toBeStringContaining(text: string, caseSensitive: boolean) subject (string) contains text with optional case sensitivity (default false)

toBeStringMatching(regexPattern: RegEx) subject (string) matches the regexPatern

toThrow(message: string) subject throws message. NOTE: function should be passed by name, NOT executed e.g. expect(myThrowingFunction).toThrow('My error Message'); To pass arguments to a function that is expected to throw, use the with() function.

toThrowError(errorType: any, errorMessage: string) subject throws an error of errorType with errorMessage. NOTE: function should be passed by name, NOT executed e.g. expect(myThrowingFunction).toThrow('My error Message'); To pass arguments to a function that is expected to throw an error, use the with() function.

with(arguments: any) pass arguments to a function that is expected toThrow or toThrowError. The call to with() should come immediately following the expectation containing the function name e.g. expect(myThrowingFunction).with(1).toThrow('My error Message');

toHaveBeenCalled(callCount: Number) subject (Spy) was called callCount times

toHaveBeenCalledWith(...args: []) subject's (Spy) call history contains at least one call with specified args

toHaveBeenCalledWithFirst(...args: []) subject's (Spy) first call arguments equal specified args

toHaveBeenCalledWithLast(...args: []) subject's (Spy) last call arguments equal specified args

Spies and Stubs

Bedrock combines the notion of spies and stubs:

spy(target: any, functionName: string)

a wrapper to spy on or stub an existing function or property of an object

andReturn(value: any)

stubs the spy target's attribute to return the value

andFake(function: Function)

stubs the spy target's attribute to call the function

reset()

resets a spy, cleaing the call cound and call history

restore()

restores the spy target's original function call or value

getCallCount()

returns the spy's call count. Prefer toHaveBeenCalled() matcher

getCallHistory()

returns an array of the spy's call history. Prefer toHaveBeenCalledWith() matcher

Future development

  • Async matchers (promise resolution)