Define suite-level utilities and test structure for mocha tests
Save yourself the hassle of manually constucting suite definitions.
Bundle common logic (such as setting up sandboxes) and minimize the
number of require()
statements in your test files.
// _suite.js
const suite = require('mocha-suite');
const sinon = require('sinon');
const chai = require('chai');
module.exports = suite((tests, options) => {
const sandbox = sinon.sandbox.create();
afterEach(() => sandbox.restore());
tests({
spy: sandbox.spy,
stub: sandbox.stub,
expect: chai.expect
});
});
// app.spec.js
const suite = require('./_suite');
const MyApp = require('../src/app');
suite('MyApp', ({ expect, spy }) => {
describe('someFunc()', () => {
it('should call spy', () => {
const bootstrap = spy();
const app = new MyApp(bootstrap);
expect(bootstrap.called).to.be.true;
});
});
});
suite(suiteDefinition
, [describe]
)
Returns a renderedSuite
function.
suiteDefinition
: A function which accepts a test function which must be called in the suite definition in order to run your tests.describe
: An override fordescribe()
. Optional
renderedSuite(description
, [options]
, tests
)
description
: Text to distinguish this suite from others, will be passed as the first parameter todescribe()
.options
: Any type of object or value representing options which will be passed as the second parameter to the provided suite definition. Optionaltests
: A function that runs all of your suite'sdescribe()
s andit()
s when called.
The returned renderedSuite
has the properties only
and skip
which allows it
to operate like mocha
's describe()
:
// isolated.spec.js
const suite = require('./_suite');
suite.only('Isolated', ({ expect, spy }) => {
/* tests will be run exclusively */
});
// skipped.spec.js
const suite = require('./_suite');
suite.skip('Skipped', ({ expect, spy }) => {
/* tests will be skipped */
});