How to require modules for testing NodeJS with Intern JS?

喜欢而已 提交于 2019-12-22 09:47:54

问题


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.



来源:https://stackoverflow.com/questions/30828531/how-to-require-modules-for-testing-nodejs-with-intern-js

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