Skip to content

Instantly share code, notes, and snippets.

Created September 30, 2016 02:23
Show Gist options
  • Save anonymous/cc71303a6a3362873eed1baf251c96e6 to your computer and use it in GitHub Desktop.
Save anonymous/cc71303a6a3362873eed1baf251c96e6 to your computer and use it in GitHub Desktop.
test mocha behavior on errors after timeout
describe("with done:", function() {
it("done(error) after timeout", function(done) {
this.timeout(10)
setTimeout(function() { done(new Error("GOTCHA!")) }, 100)
})
it("this one is safe because error is reported through done", function(done) {
this.timeout(5000)
setTimeout(done, 1000)
})
})
*.js --slow 10000
{
"scripts": {
"pretest:old": "npm i mocha@2.x",
"test:old": "npm run test:run",
"pretest:new": "npm i mocha@3.x",
"test:new": "npm run test:run",
"test:run": "mocha --version && mocha --opts mocha.opts",
"test": "npm run test:old || npm run test:new"
}
}
describe("with promise:", function() {
it("reject after timeout", function(done) {
this.timeout(10)
return new Promise(function(resolve, reject) {
setTimeout(resolve, 100)
}).then(function() { throw new Error("GOTCHA!") })
})
it("this one is safe because error is reported through promise", function(done) {
this.timeout(5000)
setTimeout(done, 1000)
})
})
describe("with throw:", function() {
it("throw after timeout", function(done) {
this.timeout(10)
setTimeout(function() { throw new Error("GOTCHA!") }, 100)
})
it("if the other test was not timing out, it would not interfere with this one", function(done) {
this.timeout(5000)
setTimeout(done, 1000)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment