mardi 2 août 2016

Mocha test and subsequent calls to 'require' modules

I have a project that uses Mocha test runner via the commandline (mocha -- test/test-dir) and Browserify. Currently the unit tests are very simple and we just require the source javascript (unbrowserified) to run the tests on. So far it has worked fine.

//test/test-dir/my-test.js

var foo = require('../../js/src/foo.js');
var assert = require('assert');

describe('foo.getBar', function() {
...

We recently wanted to add another dependency to foo.js, typeahead.js.

//js/src/foo.js

var jQuery = require("jquery");
var typeahead = require("typeahead.js");

...

Now our simple test fails with a TypeError: _.mixin is not a function. This _.mixin is defined in typeahead.js but I'm suspecting that the nested require in foo.js is not being evaluated by Mocha?

Not too familiar with Mocha or Browserify, I thought that switching the test to use the browserified version of the foo.js would help as then the required modules would be built and available in foo.min.js, although I'm seeing the same error. I know that typeahead supports CommonJS packages so I'm not sure what is wrong with these tests? Any help would be very much appreciated. Thanks!

Aucun commentaire:

Enregistrer un commentaire