intern - 无法让实习生运行 Node.js 模块

标签 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”,实习生位于“js-test-projects/intern”。我正在使用 Intern 的 1.0.0 分支。每当我尝试包含文件并运行测试时,在“默认为控制台报告器”之后我都没有得到任何输出。这是测试文件。
define([
  'intern!tdd',
  'intern/chai!assert',
  'dojo/node!../lib/HelloWorld'
], function (tdd, assert, HelloWorld) {
  console.log(HelloWorld);
});

最佳答案

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. 你的测试文件应该加载HelloWorld使用 Intern 版本的 Dojo,如下所示:

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

注:您不必使用实习生版本的 Dojo 来加载 HelloWorld这个 AMD 测试中的 node 模块,它只是一种方便的方法。如果您有一些其他 AMD 插件需要节点模块,那很好。

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

node client.js config=node/tests/intern

关于intern - 无法让实习生运行 Node.js 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16466115/

相关文章:

selenium-chromedriver - 未知错误 : failed to write prefs file

testing - 如何使用intern js调试功能测试

javascript - 单击每个第一个子 radio (未定义子长度)

javascript - Intern JS - 如何在链式命令方法中使用 Promise.all()?

node.js - 使用 Sails.js,Dojo javascript 文件/路径不会立即显示在浏览器中

intern - 如何使用通配符在 Intern 中指定测试套件?

javascript - Intern4 和 CDN 资源

javascript - Intern 的 pollUntil 不适用于 Chrome

javascript - 如何在 intern.js 中测试 View 库?