Skip to content

Instantly share code, notes, and snippets.

@garth
Created November 16, 2015 09:23
Show Gist options
  • Save garth/7367f25e2dee19f9098a to your computer and use it in GitHub Desktop.
Save garth/7367f25e2dee19f9098a to your computer and use it in GitHub Desktop.
A wrapper for chai that adds a count of the number times expect is called.
import { expect as chai } from 'chai';
let expected = null;
let actual = 0;
export default {
expect(target) {
actual++;
return chai(target);
},
expectCount(count) {
expected = count;
},
reset() {
expected = null;
actual = 0;
},
check() {
if (this.currentTest.state === 'failed' || expected === null || expected === actual) { return; }
let err = new Error(`expected ${expected} assertions, got ${actual}`);
this.currentTest.emit('error', err);
}
}
import counter, { expect, expectCount } from './helpers/chaiCounter';
beforeEach(counter.reset);
afterEach(counter.check);
describe('something', function () {
it('should work', function () {
// we expect two asserts to be called, if no asserts are made then the test should fail
expectCount(2);
const state = {
set(path, value) {
expect(path).to.equal('someKey');
expect(value).to.equal('someValue');
}
};
setState(state);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment