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

better-than-before

v1.0.0

Published

Better setup for tests

Downloads

1,856

Readme

better-than-before Build Status Coverage Status

Better setup for tests

What's this?

I used to put the code for preparing each test in before or setup block, and clean it up in after or teardown. But what if the tests don't share the same setup? Easy, I can write something like this:

test('A', () => {
	// preparing for test A
	...
});

test('B', () => {
	// preparing for test B
	...
});

But what if test B depends on test A:

test('A', () => {
	// preparing for test A
	...
});

test('B', () => {
	// Also depend on "preparing for test A"
	// preparing for test B
	// but if A fails or skipped, the setup of B is not complete
	...
});

Normally this should work if tests are run synchronously. However, A lot of times I only want to run one test with grep. Or, if test A fails, test B also fails but it shouldn't. I was tempted to do something like this:

beforeEach(() => {
	// testId indicates this setup is run immediately before which test
	if (testId === 'A') {
		// setup for test A
		...
	}

	if (testId === 'B') {
		// including the setup for test A
		// setup for test B
		...
	}
});

You might say it is very easy to find a smarter way. This is true for use-cases when your setup doesn't include things like setting up a whole mock git history.

Install

$ npm install --save-dev better-than-before

Usage

With this module, you could simplify your setup using beautiful declarative DSL syntax:

// class
import BetterThanBefore from 'better-than-before';
import { setups,  preparing } = new BetterThanBefore();

// factory
import betterThanBefore from 'better-than-before';
import { setups,  preparing } = betterThanBefore();

setups([
	() => {
		// setup for all tests
	},
	() => {
		// setup for all tests except for tests only need the first setup
	},
	...
]);

test('A', () => {
	preparing(1); // I only need the first setup
});

test('B', () => {
	preparing(2); // I need both first and second setup
});

...

Not in order?

All you need to do is to provide a teardown function with tearsWithJoy. The teardown function needs to clean up all the setups and once preparing() is called, it will rebuild the setups that's needed.

// class
import BetterThanBefore from 'better-than-before';
import { setups,  preparing, tearsWithJoy } = new BetterThanBefore();

setups([
	() => {
		// setup for all tests
	},
	() => {
		// setup for all tests except for tests only need the first setup
	},
	...
]);

tearsWithJoy(() => {
	// teardown the setups!
});

test('B', () => {
	preparing(2); // I need both first and second setup
});

test('A', () => {
	// teardown is run
	// then rerun the first setup
	preparing(1); // I only need the first setup
});

...

context

An optional context object can be passed to setup functions and returned by preparing():

setups([ (context) => { context.number = 42; }, (context) => { context.number //=> 42 }, ... ]);

test('A', () => { preparing(1); //=> {number: 42} });

test('B', () => { preparing(2); //=> {number: 42} });

Caveat

The most straightforward way of using this module is to put this in the test block. But test frameworks like mocha also time how long a test runs and you might easily hit timeout. I could hack into the framework but I decided not to. Make sure you increase your timeout and ignore the performance warnings. If you use this module in a performance framework make sure it's not run in the test block.

License

MIT © Steve Mao