简体   繁体   English

如何要求使用Intern JS测试NodeJS的模块?

[英]How to require modules for testing NodeJS with Intern JS?

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. 我收到错误Attempt to require unloaded module xxxxxx使用以下intern.js测试Attempt to require unloaded module xxxxxx (下面的myfile.js执行require('xxxxxx') )来测试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. 关于如何使用BDD测试界面,没有示例。 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? 有一些例子可以使用其他样式,例如如何在intern.js测试中加载node.js http模块? but that doesn't use BDD. 但这不使用BDD。

Does it have anything to do with properties I need to set in intern.js file? 它与我需要在intern.js文件中设置的属性有什么关系吗?

require works the same with any of the Intern interfaces. require与任何Intern接口的工作方式相同。 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). 棘手的部分是AMD要求(您在上面的代码中使用)与Node的要求不同(这是您需要用来加载Node模块的)。

If myfiles.js is a node module, you'll need to use Dojo's node plugin to load it, like: 如果myfiles.js是一个节点模块,你需要使用Dojo的node插件加载它,如:

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

Step 1 do it in define 第1步在定义中执行

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. 但有些情况下它无法加载使用,所以最好使用上面的方法,发生在我身上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM