Why do I see “define not defined” when running a Mocha test with RequireJS?

前端 未结 5 1715
暗喜
暗喜 2020-12-23 20:22

I am trying to understand how to develop stand-alone Javascript code. I want to write Javscript code with tests and modules, running from the command line. So I have insta

5条回答
  •  攒了一身酷
    2020-12-23 21:05

    The Mocha documentation is lacking on how to set this stuff up, and it's perplexing to figure out because of all the magic tricks it does under the hood.

    I found the keys to getting browser files using require.js to work in Mocha under Node: Mocha has to have the files added to its suites with addFile:

    mocha.addFile('lib/tests/Main_spec_node');
    

    And second, use beforeEach with the optional callback to load your modules asynchronously:

    describe('Testing "Other"', function(done){
        var Other;
        beforeEach(function(done){
            requirejs(['lib/Other'], function(_File){
                Other = _File;
                done(); // #1 Other Suite will run after this is called
            });
        });
    
        describe('#1 Other Suite:', function(){
            it('Other.test', function(){
                chai.expect(Other.test).to.equal(true);
            });
        });
    });
    

    I created a bootstrap for how to get this all working: https://github.com/clubajax/mocha-bootstrap

提交回复
热议问题