无法让Intern运行Node.js模块

时间:2013-05-09 16:02:34

标签: intern

我正在尝试测试Intern,看看它是否适合测试框架。我试图在实习生中测试以下代码。

var HelloWorld;

HelloWorld = (function () {

  function HelloWorld (name) {
    this.name = name || "N/A";
  }

  HelloWorld.prototype.printHello = function() {
    console.log('Hello, ' + this.name);
  };

  HelloWorld.prototype.changeName = function(name) {
    if (name === null || name === undefined) {
      throw new Error('Name is required');
    }
    this.name = name;
  };

  return HelloWorld;

})();

exports = module.exports = HelloWorld;

该文件位于'js-test-projects / node / lib / HelloWorld.js'中,而Intern位于'js-test-projects / intern'。我正在使用实习生的1.0.0分支。每当我尝试包含文件并运行测试时,在“默认为控制台记者”之后我都没有得到任何输出。这是测试文件。

define([
  'intern!tdd',
  'intern/chai!assert',
  'dojo/node!../lib/HelloWorld'
], function (tdd, assert, HelloWorld) {
  console.log(HelloWorld);
});

1 个答案:

答案 0 :(得分:7)

<强> 1。假设以下目录结构(基于问题):

js-test-projects/
    node/
        lib/
            HelloWorld.js   - `HelloWorld` Node module
        tests/
            HelloWorld.js   - Tests for `HelloWorld`
            intern.js       - Intern configuration file
    intern/

<强> 2。您的实习配置文件应包含node包和要运行的任何套件的信息:

// ...

// Configuration options for the module loader
loader: {
    // Packages that should be registered with the loader in each testing environment
    packages: [ 'node' ]
},

// Non-functional test suite(s) to run
suites: [ 'node/tests/HelloWorld' ]

// ...

第3。您的测试文件应使用Intern版本的Dojo加载HelloWorld,如下所示:

define([
    'intern!tdd',
    'intern/chai!assert',
    'intern/dojo/node!./node/lib/HelloWorld.js'
], function (tdd, assert, HelloWorld) {
    console.log(HelloWorld);
});

注意:你没有拥有使用Intern版本的Dojo在这个AMD测试中加载HelloWorld节点模块,这只是一个方便这样做的方式。如果您有一些其他AMD插件,该节点需要一个节点模块,那就完全没问题了。

<强> 4。最后,要在Node.js环境中运行测试,请通过在client.js目录中发出以下命令来使用Intern的intern节点运行程序:

node client.js config=node/tests/intern