Question

We're using Karma and Mocha along with Squire for mocking dependencies loaded with RequireJs. Without Squire, our tests run fine however as soon as we initialize a Squire object, everything start breaking:

define(['squire'], function (Squire) {
    var injector = new Squire(); // comment this out and everything works again
});

We found that sometimes Karma reported that no tests were run (even though breakpoints set inside test functions would be hit) and sometimes it would report random errors that shouldn't have been failing. Karma just goes a bit crazy.

How do I use Squire without my tests going crazy?

Was it helpful?

Solution

Ok, I resolved this issue after finding a random github issue. My Karma test-main.js file loaded tests and ran a callback like so:

var tests = Object.keys(window.__karma__.files).filter(function (file) {
    return /Spec\.js$/.test(file);
});

requirejs.config({
    // ... more code here

    // ask Require.js to load these files (all our tests)
    deps: tests,

    // start test run, once Require.js is done
    callback: function () {
        mocha.setup('bdd');
        mocha.ignoreLeaks();
        mocha.run();
    }
});

Now instead of defining deps and callback in the requirejs.config, I added the following code at the end of test-main.js:

// load all tests
require(tests, function () {
    // start test run once requirejs is done
    mocha.setup('bdd');
    mocha.ignoreLeaks();
    mocha.run();
});

And everything works swimmingly again! Big thanks to @FabienDeshayes on GitHub for this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top