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:
; ;
But what if test B depends on test A:
; ;
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:
;
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;; // factory;; ; ; ; ...
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;; ; ; ; ; ...
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