support repository: https://github.com/atondelier/coding-dojo-mocha
All are packages to add in the package.json
src
└── service.js
test
├── before.js
├── mocha.opts
└── service-test.jsImportant options
--reporter spec
--recursive
--compilers js:babel-register
--require ./test/before.js
--globals clock,proxyquire
--ui bddexample of mocha.opts file
Add these packages to your package.json
mocha.opts using this
--compilers js:babel-register.babelrc using this
{
"presets": [
"node5"
]
}"use strict";
import chai from 'chai';
import sinon from 'sinon';
import 'sinon-as-promised';
import sinonChai from 'sinon-chai';
import chaiAsPromised from 'chai-as-promised';
import lolex from 'lolex';// prepare chai
chai.should();
chai.use(sinonChai);
chai.use(chaiAsPromised);// expose sinon
global.sinon = sinon;// reset clock mock for each test
setTimeout(() => {
beforeEach(function() {
global.clock = lolex.install();
});
afterEach(function() {
global.clock.uninstall();
});
});describe('MyService', () => {
let service;
beforeEach(() => {
service = new MyService();
});
}); describe('#firstMethod(arg)', () => {
let arg;
}); context('something is wrong', () => {
beforeEach(() => {
arg = 'wrong arg';
});
});
context('everything is ok', () => {
/* [...] */
}); it('should return false', () => {
service.firstMethod(arg).should.be.false;
});chai.should();
foo.should.be.a('string');
foo.should.equal('bar');
foo.should.have.length(3);
tea.should.have.property('flavors')
.with.length(3);var expect = chai.expect;
expect(foo).to.be.a('string');
expect(foo).to.equal('bar');
expect(foo).to.have.length(3);
expect(tea).to.have.property('flavors')
.with.length(3);var assert = chai.assert;
assert.typeOf(foo, 'string');
assert.equal(foo, 'bar');
assert.lengthOf(foo, 3)
assert.property(tea, 'flavors');
assert.lengthOf(tea.flavors, 3);in the exercises, we will use the "should" syntax
support repository: https://github.com/atondelier/coding-dojo-mocha
sinon.spy(service, 'first');
service.second().should.equal(2);
service.first.should.have.been.called;
service.first.restore();sinon.stub(service, 'first').returns(41);
service.second().should.equal(42);
service.first.should.have.been.called;
service.first.restore();const service = {
first: () => 1,
second: () => service.first() + 1
};One simple service
using a spy
using a stub
a spy only monitors
a stub monitors and replaces
support repository: https://github.com/atondelier/coding-dojo-mocha
it('should eventually return true', (done) => {
service.answer(question)
.should.become(true)
.and.notify(done)
;
});it('should eventually return true', () => {
return service.answer(question).should.become(true);
});it('should eventually return true', function* () {
const answer = yield service.answer(question);
answer.should.be.true;
});sinon-as-promised (or sinon >=2) lets you write async stubs
sinon.stub(service, 'getSomethingAsync').withArgs(42).resolves({ /* ... */ });chai-as-promised lets you assert the eventual result
service.getSomethingAsync(42).should.become({ /* ... */ });sinon.stub(service, 'getSomethingAsync').withArgs(null).rejects({ /* ... */ });service.getSomethingAsync(null).should.be.rejectedWith({ /* ... */ });support repository: https://github.com/atondelier/coding-dojo-mocha