Skip to content

Instantly share code, notes, and snippets.

@devoto13
Last active February 5, 2017 11:56
Show Gist options
  • Save devoto13/fdb04e9df80e057d44357c8123f85b06 to your computer and use it in GitHub Desktop.
Save devoto13/fdb04e9df80e057d44357c8123f85b06 to your computer and use it in GitHub Desktop.
Demo that Mocha handles Promise returned from the test correctly
// It demonstrates, that instead of explicitly adding `done()` callbacks everywhere you can return Promise from method.
//
// Expected output:
// $ ./node_modules/.bin/_mocha test.js
//
//
// tide retriever
// 1) should retrieve and parse tide CSV data
// 2) should retrieve and parse tide CSV data
//
//
// 0 passing (13ms)
// 2 failing
//
// 1) tide retriever should retrieve and parse tide CSV data:
// Error: Network error
// at Object.get (test.js:5:31)
// at Context.<anonymous> (test.js:12:23)
//
// 2) tide retriever should retrieve and parse tide CSV data:
// Error: Network error
// at Object.get (test.js:5:31)
// at Context.<anonymous> (test.js:26:30)
var should = require('should');
let tideRetriever = {
get: () => {
return Promise.reject(new Error("Network error"));
}
}
let expectedCount = 3;
describe("tide retriever", function() {
it("should retrieve and parse tide CSV data", function(done) {
tideRetriever.get().then(
function(entries) { // resolve
entries.should.be.instanceof(Array).and.have.lengthOf(expectedCount);
done(); // test passes
},
function(err) { // reject
done(err); // Promise rejected
}
).catch(function (err) {
done(err); // should throwed assertion
});
});
it("should retrieve and parse tide CSV data", function() {
return tideRetriever.get().then(
function(entries) { // resolve
entries.should.be.instanceof(Array).and.have.lengthOf(expectedCount);
}
);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment