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

test-when-it

v1.1.0

Published

Adds 'When' syntax to Mocha for more expressive test setups.

Downloads

8

Readme

WhenIt / ThenIt syntax for JS unit tests.

Works with Mocha or Jasmine test frameworks.

The intent is to provide a way to make small reusable functions that are used between several tests, without having to duplicate code between tests. It also provides a way to move test names from plain strings into function names.

Installation

Get the files.

npm install when-it --save-dev

Add to your test environment.

require('when-it');

// or with a regular script tag

<script src="node_modules/test-when-it/index.js" type="text/javascript"></script>

Use

setIt(propertyName, propertyValue)

This function assignes a property getter with the name propertyName to the this context. The property is deleted after each test.

The propertyValue parameter can be a literal value, or a function. If a function is passed, it is called each time the property is accessed and the return value is used as the property value (like a property getter). (The value is not memoized like Ruby/rspec's let())

describe('using setIt', function() {
	setIt('fromLiteral', 'valueFromLiteral');

	setIt('fromFunc', function() {
		return 'valueFromFunc';
	});

	it('makes a value available on the test context', function() {
		expect(this.fromLiteral).to.equal('valueFromLiteral');
	});

	it('makes the return value of a function available on the test context', function() {
		expect(this.fromFunc).to.equal('valueFromFunc');
	});

});

whenIt(..)

The whenIt(...) function takes any number of arguments that act as test setup (aka beforeEach). The last parameter must be a function that is executed as the test context.

The following things can be passed to the whenIt function:

  • Function - The function will be executed as a beforeEach step. If the function has a .name then the name will be appended to the test name. If the function returns another function, then that returned function will be called in an afterEach step. Example:
describe('example', function() {
	function setsUserToBob() {
		this.user = 'Bob';

		return function() {
			delete this.user;
		}
	}

	whenIt(setsUserToBob, function() {
		it('can access the user name', function() {
			expect(this.user).to.equal('Bob');
		});
	});
});

// The above code is equivelant to this "normal" mocha test code:

describe('example', function() {
	describe('when it sets user to bob ', function() {
		beforeEach(function() {
			this.user = 'Bob';
		});

		afterEach(function() {
			delete this.user;
		});

		it('then it can access the user name', function() {
			expect(this.user).to.equal('Bob');
		});
	});
});
  • String - The string will be appended to the test name.

  • Object - The properties of the object will be copied to the this test context during the beforeEach step. The properties will then be deleted from the this context in the afterEach. Example:

describe('example', function() {
	whenIt('sets user to bob', { user: 'Bob' }, function() {
		it('can access the user name', function() {
			expect(this.user).to.equal('Bob');
		});
	});
});
  • Fixture - See further information below.

thenIt(..)

The thenIt function can be passed multiple functions. Each function is executed as its own it() test. Example:

describe('example', function() {
	function setsTheUserToAString() {
		expect(this.user).to.be.instanceOf(String);
	}

	function setsTheUserToBob() {
		expect(this.user).to.equal('Bob');
	}

	whenIt('sets user to bob', { user: 'Bob' }, function() {
		thenIt(setsTheUserToAString, setsTheUserToBob);
	});
});

// The above code is equivelant to this "normal" mocha test code:

describe('example', function() {
	describe('when it sets user to Bob', function() {
		beforeEach(function() {
			this.user = 'Bob';	
		});

		afterEach(function() {
			delete this.user;
		});

		it('sets the user to a string', function() {
			expect(this.user).to.be.instanceOf(String);
		});

		it('sets the user to Bob', function() {
			expect(this.user).to.equal('Bob');
		});
	});
});

new Fixture(...)

An instance of the Fixture object can be passed to whenIt and it used to encapsulate a name and test before/after steps.

The constructor must be passed an object of the form:

var doesSomething = new Fixture({
	name: 'a string name to insert into the test name', // required.
	before: function() {}, // run during beforeEach. optional.
	after: function() {} // run during afterEach. optional.
});

whenIt(doesSomething, function() {
	it(...);
});

The instance also exposes a .apply() function. You can pass any arguments to the function, and those arguments will be passed to the before and after functions.

var setsUser = new Fixture({
	name: 'sets the user',
	before: function(name) { this.user = name; },
	after: function(name) { delete this.user; }
});

whenIt(setsUser.apply('Bob'), 'to Bob', function() {

	// full test name is "when it sets the user to Bob the user is set to Bob"
	it('the user is set to Bob', function() {
		expect(this.user).to.equal('Bob');
	});
});