Handling dependencies not installed with npm in Mocha?
I have a working Node application and I am trying to add Mocha tests, but I am getting some odd import errors.
This is my file structure:
package.json
index.js
src/
chart.js
test/
test_chart.js
This is what my file looks like chart.js
:
global.jQuery = require('jquery');
global.$ = global.jQuery;
require('typeahead');
require('bloodhound');
var bootstrap = require('bootstrap');
var Handlebars = require('handlebars');
var Highcharts = require('highcharts-browserify');
var parse = require('csv-parse');
var moment = require('moment');
var analyseChart = {
doSomething: function() { ... }
};
module.exports = analyseChart;
I am currently importing everything from /src
into one file index.js
and then linking to browserify
, which works fine, no errors in the application.
I have a section in package.json that defines dependencies not available via npm like this:
"browser": {
"chosen": "./vendor/chosen.jquery.min.js",
"typeahead": "./vendor/typeahead.bundle.js",
"bloodhound": "./vendor/bloodhound.js"
}
Now I want to start writing Mocha tests for functions in /src
.
This is my first stub in test_chart.js
:
var chart = require('../src/chart');
chart.doSomething();
But when I run mocha
I get the following error:
/Users/.../js/node_modules/typeahead/node_modules/dom/lib/matches.js:2
var proto = Element.prototype;
^
ReferenceError: Element is not defined
at Object.<anonymous> (/Users/.../js/node_modules/typeahead/node_modules/dom/lib/matches.js:2:13)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/.../js/node_modules/typeahead/node_modules/dom/index.js:4:15)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/.../js/node_modules/typeahead/typeahead.js:3:11)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/.../js/src/chart.js:3:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/.../js/test/test_chart.js:3:13)
How can I fix this import error for Mocha?
I think maybe it doesn't like require('typeahead')
it because it can't see the dependency paths browser
I installed in package.json
.
Is there a way to make these files available to Mocha as well as the browser?
Or should I use a different test suite?
source to share
No one has answered this question yet
See similar questions:
or similar: