【问题标题】:How to require modules for testing NodeJS with Intern JS?如何使用 Intern JS 测试 NodeJS 的模块?
【发布时间】:2015-06-14 10:57:23
【问题描述】:

我收到错误 Attempt to require unloaded module xxxxxx 与以下用于测试 NodeJS 的 intern.js 测试(myfile.js 下面执行 require('xxxxxx'))。

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() {
      ...

目录结构是

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

如何正确地要求文件?没有关于如何使用 BDD 测试接口执行此操作的示例。在How do I load the node.js http module from within an intern.js test? 中也有使用其他样式的示例,但不使用 BDD。

这和我需要在 intern.js 文件中设置的属性有什么关系吗?

【问题讨论】:

标签: javascript node.js unit-testing mocha.js intern


【解决方案1】:

require 与任何 Intern 接口的工作方式相同。棘手的部分是 AMD 要求(您在上面的代码中使用)与 Node 的要求(您需要使用它来加载 Node 模块)不同。

如果myfiles.js 是一个节点模块,你需要使用Dojo 的node 插件来加载它,比如:

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

【讨论】:

    【解决方案2】:

    第 1 步 在定义中做到这一点

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

    以上是等价于

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

    但在某些情况下它无法使用 require 加载,所以最好使用上述方法,发生在我身上。

    【讨论】:

      猜你喜欢
      • 2015-11-04
      • 1970-01-01
      • 2015-08-02
      • 1970-01-01
      • 1970-01-01
      • 2021-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多