Jenny M Jenny M - 3 months ago 11
Node.js Question

Mocha test are not running in right order using before/beforeEach

I use the following code and in the before I copy files that the tests after are using assertion but when I run it the before/each is happen after the test, what am I missing here?

I tried also with beforeach without success

describe(" Handler", function() {

before((done) => {

fs.stat(utils.pathSetup() + "/ins/", function (err, stats) {
if (!err) {
console.log("success");
} else {
let lclpath = utils.pathSetup();
Loder.add({folderPath: lclpath + "/test/testcontent/ins"});
console.log(err);
}
});
done();
});


//This are called before the previous statement why????????
//This should happen after the before has finished,they are in the same test block and this called is right after...

Handler.invoke(req, res)
.then((Ref) => {

}).done();

ee.on('Started', (arg) => {
evtResStart = arg;
});


in debug when I put BP it stops in before and click on step into take me to the Handler.invoke instead of inside the before...:(

Any idea what could be the reason for such thing?

Lee Lee
Answer

If you are using done in your tests/befores, you need to call done() inside your callbacks, e.g.

before((done) => {
    fs.stat(utils.pathSetup() + "/ins/", function (err, stats) {
        if (!err) {
            console.log("success");
        } else {
            let lclpath = utils.pathSetup();
            Loder.add({folderPath: lclpath + "/test/testcontent/ins"});
            console.log(err);
        }
        done();
    });
});

Edit: Assuming the comments in your question are supposed to be formatted as code, is your Handler stuff in at it block?

it('should do something', () => {
    Handler.invoke(req, res)
        .then((Ref) => {

        }).done();

    ee.on('Started', (arg) => {
        evtResStart = arg;
    });
});