如何使用单个命令运行多个QUnit(node.js)测试文件?

时间:2015-07-09 08:54:11

标签: javascript node.js testing tdd qunit

方案

我们正在尝试使用Node.js QUnit Module aka qunitjs进行服务器端测试,以便我们可以编写一个测试并在服务器和客户端上运行它,例如:https://github.com/nelsonic/learn-tdd/blob/master/test.js

当我们使用命令运行单个文件时:

node test/my_test.js

它按预期工作。

但是,如果我们在/test 目录中有多个测试,并尝试使用以下命令将所有文件作为套件运行:< / p>

node test/*.js

仅执行第一个文件(按字母顺序)。

请参阅:https://github.com/nelsonic/hapi-socketio-redis-chat-example/tree/master/test

问题

  

如何 执行多个测试文件 单一命令

我们尝试为此挖掘现有的StackOverflow + GitHub Q / A,但未找到任何匹配项。 (任何建议/帮助非常感谢!

1 个答案:

答案 0 :(得分:0)

QUnit具有开箱即用的命令行运行程序,因此只需运行:

npm install -g qunit
qunit

默认情况下,它将自动在test文件夹中搜索测试并执行所有测试。另外,您可以手动指定测试脚本qunit test/only_this_test.js test/also_this_test.js

正在运行,但测试失败!

如果失败与未定义的函数有关,那是因为您需要告诉测试如何找到被测函数。

修改tests.js:

// Add this in the beginning of tests.js
// Use "require" only if run from command line
if (typeof(require) !== 'undefined') {
    // It's important to define it with the very same name in order to have both browser and CLI runs working with the same test code
    doSomething = require('../index.js').doSomething;
}

修改您的js脚本:

//This goes to the very bottom of index.js
if (typeof module !== 'undefined' && module.exports) {
  exports.doSomething = doSomething; 
}

在我的其他答案中https://stackoverflow.com/a/51861149/1657819

查看更多详细信息

P.S。回购中不错的教程! ;)