-->

How to require modules for testing NodeJS with Int

2019-05-10 19:15发布

问题:

I'm getting the error Attempt to require unloaded module xxxxxx with the following intern.js test (myfile.js below does a require('xxxxxx')) for testing NodeJS.

define(function(require) {
  var bdd = require('intern!bdd');
  var myfile = require('../myfile.js');
  bdd.describe('the thing being tested', function() {
    bdd.it('do the test', function() {
      ...

The directory structure is

intern.js
myfile.js
test
|-- test.js

How do I properly require a file? There's no examples on how to do it with the BDD test interface. There are examples of doing it with the other style like in How do I load the node.js http module from within an intern.js test? but that doesn't use BDD.

Does it have anything to do with properties I need to set in intern.js file?

回答1:

require works the same with any of the Intern interfaces. The tricky part is that the AMD require (which you're using in the code above) is not the same as Node's require (which is what you need to use to load Node modules).

If myfiles.js is a node module, you'll need to use Dojo's node plugin to load it, like:

var myfile = require('intern/dojo/node!../myfile');


回答2:

Step 1 do it in define

define([
   'intern!xyzModule',
   'intern/chai!abcModule',
],function(xyz,abc){

}

The above is equilant to

var xyz = require(intern!xyzModule);
var abc = require(intern!abcModule);

but some cases it can not load using require so its best to use above method, happened to me.