【问题标题】:How to run several QUnit (node.js) test files using a single command?如何使用单个命令运行多个 QUnit (node.js) 测试文件?
【发布时间】:2015-09-27 13:02:03
【问题描述】:

场景

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

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

node test/my_test.js

它按预期工作。

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

node test/*.js

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

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

问题

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

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

【问题讨论】:

    标签: javascript node.js testing tdd qunit


    【解决方案1】:

    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的其他答案中查看更多详细信息

    附:很好的教程在回购! ;)

    【讨论】:

      猜你喜欢
      • 2014-09-02
      • 2015-05-22
      • 1970-01-01
      • 2020-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-05
      相关资源
      最近更新 更多