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

How to run several QUnit (node.js) test files using a single command?

本文关键字:node js 文件 测试 QUnit 单个 何使用 命令 运行      更新时间:2023-09-26

场景

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

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

node test/my_test.js

它按预期工作。

但是,当我们在/测试目录中有多个测试时,并尝试使用以下命令将所有文件作为套件运行

node test/*.js

只有第一个文件(按字母顺序(被执行。

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

问题

我们如何用单个命令 执行多个测试文件?

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

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

附言:回购教程不错!(