Skip to content

Instantly share code, notes, and snippets.

@apaleslimghost
Last active January 23, 2017 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apaleslimghost/6557fd5d7b66a64e4031e272bcbe8c92 to your computer and use it in GitHub Desktop.
Save apaleslimghost/6557fd5d7b66a64e4031e272bcbe8c92 to your computer and use it in GitHub Desktop.

stub-all

simplifies creating a lot of (sinon) stubs in your (mocha) tests

installation

npm install @quarterto/stub-all

usage

const stubAll = require('@quarterto/stub-all');
const sinon = require('sinon');

describe('foo', () => {
  stubAll(() => [
    sinon.stub(foo, 'bar'),
    sinon.stub(foo, 'baz'),
    sinon.stub(foo, 'quux'),
  ])
});

Pass a function returning an array of stubs to stubAll. It calls the function in before, creating your stubs, resets them all in beforeEach (making sure things like wasCalled are clean in each test, and restores them all in after.

licence

ISC

module.exports = createStubs => {
const stubs = [];
before(() => {
stubs.push.apply(stubs, createStubs());
});
beforeEach(() => {
stubs.forEach(s => s.reset());
});
after(() => {
stubs.forEach(s => s.restore && s.restore());
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment