w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
should.js not causing mocha test to fail

When testing asynchronous code, you need to tell Mocha when the test is complete (regardless of whether it passed or failed). This is done by specifying an argument to the test function, which Mocha populates with a done function. So your code might look like this:

describe("tide retriever", function() {
    it("should retrieve and parse tide CSV data", function(done) {
        tideRetriever.get(from, to).then(
            function(entries) { // resolve
            function(err) { // reject
                should.fail("Promise rejected", err);

Note that the way Mocha knows this is an async test and it needs to wait until done() is called is just by specifying that argument.

Also, if your promise has a "completed" handler, which fires both on success and failure, you can alternatively call done() in that, thus saving a call.

More info at: http://mochajs.github.io/mocha/#asynchronous-code

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.