Giter VIP home page Giter VIP logo

better-than-before's Introduction

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:

import { setups,  preparing } from 'better-than-before';

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
});

...

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

better-than-before's People

Contributors

cht8687 avatar stevemao avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.