1.0.3 • Published 5 years ago

@holomodules/test-kit v1.0.3

Weekly downloads
-
License
-
Repository
bitbucket
Last release
5 years ago

@holonis/test-kit

Collected utilities for less intrusive unit test setup

Usage

const { expect, stubs } = require('@holonis/test-kit');

This is a single module that bundles the versions of the unit testing tools we use in code. As of the initial version this was chai and sinon. Because we use those, we also attach the sinon-chai feature to chai since it gives a bunch of nice assertion tools for the sinon fakes.

We assign some names to a few things to make them easier to use in the tests. For example, you can get the expect function or the match object from sinon or the stubs manager.

stubs

That is the final part. Since it was messy to use the sinon fakes directly with not additional tooling, we introduce a stubs manager. That is just a magic object that knows how to easily create new stubs for functions, objects and classes and how to reset them. It also names them and makes it easier to get at them later.

// To get a stub for a function that always returns true
stubs.functionStub('functionName');
stubs.functionName.returns(true);

// To get a stub for an object with a method that returns 10
// and another that returns 'hello'.
stubs.objectStub('objectName', 'gimmeTen', 'sayHello');
stubs.objectName.gimmeTen.returns(10);
stubs.objectName.sayHello.returns('hello');

// To get a stub for a class with a method that returns 10
// and another that returns 'hello'.
stubs.objectStub('ClassName', 'gimmeTen', 'sayHello');
stubs.className.gimmeTen.returns(10);
stubs.className.sayHello.returns('hello');
// Code that does: new stubs.ClassName('init');
// satisfies this expectation:
expect(stubs.ClassName).to.have.been.calledWith('init');

In order to make this simpler to use we automatically create a constructor function for a class stub with the capitalized name and it will return a stub for an object with the lowercase name.

Also, we have grafted the sandbox.reset method onto the stubs so that we can reset all of the stubs at the end of each it in the tests.

1.0.3

5 years ago