Last active
August 29, 2015 14:05
-
-
Save virtualandy/8e3665068469524357f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var chai = require("chai"); | |
var sinon = require("sinon"); | |
var sinonChai = require("sinon-chai"); | |
chai.should(); | |
chai.use(sinonChai); | |
function asyncHello(name, delay, cb) { | |
setTimeout(function() { | |
console.log("running after ", delay); | |
cb("hello " + name); | |
}, delay); | |
} | |
suite('Mega Suite', function(){ | |
suite("testing async hello", function() { | |
test('should call the callback', function(done) { | |
var cb = sinon.spy(); | |
asyncHello("foo", 500, cb); | |
cb.should.have.been.called(); | |
done(); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cat package.json | |
{ | |
"name": "mocha-meetup", | |
"version": "0.0.0", | |
"description": "", | |
"main": "index.js", | |
"author": "", | |
"license": "ISC", | |
"devDependencies": { | |
"chai": "^1.9.1", | |
"mocha": "^1.21.4", | |
"sinon": "^1.10.3", | |
"sinon-chai": "^2.5.0" | |
}, | |
"scripts": { | |
"test": "node_modules/.bin/mocha --ui=tdd test/*.js" | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ken Powers has a great writeup on some of Sinon's advance features. I guess one approach is to use fake timers and skip using Mocha's
done()
functionality.Based on that blog, I tweaked the test to look like:
Which works. Using done seems cleaner/simpler, though.