Handling dependencies not installed with npm, in Mocha?

£可爱£侵袭症+ 提交于 2019-12-22 11:03:30

问题


I have a working Node application and I'm trying to add Mocha tests, but 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 chart.js file looks like:

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;

Currently I import everything from /src into a single index.js file, then bundle it with browserify, which works just fine, no errors in the application.

I have a section in package.json that defines dependencies not available via npm as follows:

 "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 the 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') because it can't see the browser dependency paths that I set in package.json.

Is there a way I can make these files available to Mocha as well as browserify?

Or should I use another testing package altogether?

来源:https://stackoverflow.com/questions/30488600/handling-dependencies-not-installed-with-npm-in-mocha

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!