machineghost
machineghost

Reputation: 35730

Mocha + Require Issue

I'm trying to get Mocha (browser-version, not Node) running inside my (Require.js) environment, but I'm running in to a strange issue.

If I do:

<script src="require.js" data-main='mochaTestMain.js'></script>

and then do the following in mochaTestMain.js:

require(['someFile'], function() {
    describe('foo', function() {
        it('foo', function() {
            chai.expect(false).to.equal(true)
        });
    });
});

my "foo" spec results show up. However if I put that exact same test inside someFile.js (inside a define statement), the test doesn't show up; it's like Mocha was never even aware that it existed. If I add a console.log inside the describe, it shows up, but if I add one inside the it it doesn't.

Does anyone have any suggestions as to what I can do to get Mocha to recognize tests defined in modules?

Upvotes: 0

Views: 692

Answers (1)

ddotsenko
ddotsenko

Reputation: 4996

I don't see where you call "mocha.run()" If it runs before the above tests, that may be one reason.

You can gain control over the timing of things by yanking the logic out of async threads into in-line flow:

contents of someTestFile.js:

;(function(){

    function setUpTests(){
        describe('foo', function() {
            it('foo', function() {
                chai.expect(false).to.equal(true)
            })
        })
    }

    // note, we are returning a test "constructor"
    define(function(){return setUpTests})

})();

contents of mochaTestMain.js:

;(function(){

    require(["someTestFile"], function(setUpTests){
        // defining of the test must come before
        setUpTests()
        // you run the test suite
        mocha.run()
    })

})();

Upvotes: 1

Related Questions