vendredi 6 décembre 2019

Mocha test called via mocha.run() does not run code inside "it" functions, nor does it run code inside the "before" function

I have a test runner application and it takes a target and runs a test set based on that. The simplified version looks like this:

"use strict";

const mocha = require("mocha");  

const mochaEngine = new mocha({ });

const execute = async function(args)
{ 
    if(!args.targets || args.targets.length < 1)
    {
        args.targets = ["all"];
    }

    for(let targetIndex = 0; targetIndex < args.targets.length; targetIndex++)
    {
        switch(args.targets[targetIndex])
        {
            case "all" :
            {
                mochaEngine.addFile(__dirname + "/tests/module/service_1/core_tests.js");

                mochaEngine.run();
                break;
            }
            default :
            {
                console.log("Unrecognized test target: " + args.targets[targetIndex]);
                break;
            }
        }
    }
};

module.exports.execute = execute; 

The test file it is running is normative, looks exactly like a hello world level test file as far as I can tell, yet the only code that seems to run is the "describes". The "before" and "it"s are completely ignored.

const mocha = require("mocha"); 

const before = mocha.before;
const describe = mocha.describe;
const it = mocha.it;
const done = mocha.done;

const assert = require("assert");

//set local https
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

describe("core functions", function () { 

    before(function() {   

        console.log("running test file " + __dirname + "/" + __filename);

    }); 

    console.log("outer describe");

    describe("core functions", function () { 

        console.log("inner describe");

        it("knows how to equal things", function() { 
            console.log("it 1");
            assert.equal(5, 5);
            done();
        });

        it("knows how to not equal things", function() { 
            console.log("it 2");
            assert.equal(6, 7);
        });

        it("knows how to keep running after a failed test", function() { 
            console.log("it 3");
            assert.equal(5, 5);
        });
    }); 
});

Here's the output:

outer describe
inner describe

Notice it does not log anything from inside the "it" or "before" statements. The debugger also never goes there.

What am I doing wrong here? This looks like every other test file I've ever seen and the only difference is I'm calling .run from code, which shouldn't matter.

Aucun commentaire:

Enregistrer un commentaire