Skip to content

Instantly share code, notes, and snippets.

@magalhini
Forked from eswak/mochachaisinon.js
Created October 5, 2018 15:33
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 magalhini/f4d763672408b77d214ddec931ac37cd to your computer and use it in GitHub Desktop.
Save magalhini/f4d763672408b77d214ddec931ac37cd to your computer and use it in GitHub Desktop.
An example of asynchronous JavaScript testing using Mocha + Chai + Sinon
var chai = require('chai');
var assert = chai.assert;
var expect = chai.expect;
var should = chai.should();
var sinon = require('sinon');
function myAsyncFunction(callback) {
// 50ms delay before callback
setTimeout(function() {
console.log('hello');
callback('hello');
}, 50);
}
// first (and only) set of tests
describe('myAsyncFunction', function(){
var sandbox; // sinon.js sandbox
// this function will be called before every single test
beforeEach(function() {
// create a sandbox
sandbox = sinon.sandbox.create();
// stub some console methods (replaces object.method with a stub function)
sandbox.stub(console, 'log');
sandbox.stub(console, 'error');
});
// this function will be called after every single test
afterEach(function() {
// restore the environment as it was before
sandbox.restore();
});
// first test
it('should return hello as callback parameter', function(done){
myAsyncFunction(function(data) { // callback
// test stuff
// the 3 assertions are doing the same thing,
// it's just 3 different ways to write it
assert.equal(data, 'hello');
data.should.equal('hello');
expect(data).to.eql('hello');
// this test is done, go to the next one
done();
});
});
// second test
it('should console.log hello', function(done) {
myAsyncFunction(function(data) {
sinon.assert.calledOnce(console.log);
sinon.assert.calledWithExactly(console.log, 'hello');
sinon.assert.notCalled(console.error);
done();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment