命令行界面

如何在 从 npm 安装 后使用 QUnit CLI(命令行界面)。

QUnit CLI 选项

Usage: qunit [options] [files]

  Files should be a space-separated list of files, directories, or glob expressions.
  Defaults to 'test/**/*.js'.

Options:
  -V, --version          output the version number
  -f, --filter <filter>  run only matching module or test names
  -m, --module <name>    run only the specified module
  -r, --reporter [name]  specify the reporter to use
  --require <module>     specify a module or script to include before running any tests
  --seed [value]         specify a seed to re-order your tests
  -w, --watch            watch files for changes and re-run the test suite
  -h, --help             display help for command

--filter

仅运行与给定过滤器匹配的测试。过滤器与模块和测试名称匹配,可以是子字符串匹配(不区分大小写),也可以是正则表达式。

示例:--filter foo--filter !foo--filter "/foo/"--filter "!/foo/"

查看 QUnit.config.filter 以获取更多信息。

--module

仅运行属于指定模块的测试。名称不区分大小写匹配,但必须完整。

示例:--module foo--module "Foo"

查看 QUnit.config.module 以获取更多信息。

--reporter

默认情况下,使用 TAP 报告器。

运行 qunit --reporter <name> 以使用不同的报告器,其中 <name> 可以是内置报告器的名称,也可以是实现 js-reporters 规范的 Node 模块。报告器将自动加载和初始化。

内置报告器

--require

这些模块或脚本将在任何测试开始运行之前被加载。

这可用于安装 Node.js require 钩子,例如用于 TypeScript (ts-node/register)、Babel (@babel/register) 或 CoffeeScript (coffeescript/register)。

它还可以用于您自己的设置脚本,以引导环境或调整 QUnit.config。例如

qunit --require ./test/setup.js
// test/setup.js
QUnit.config.noglobals = true;
QUnit.config.notrycatch = true;

global.MyApp = require('./index');

查看 QUnit.config 以获取所有可用的配置选项。

--seed

此选项为您分配 QUnit.config.seed

Node.js CLI 选项

QUnit CLI 使用 Node.js。您可以通过 Node.js CLI 选项,使用 NODE_OPTIONS 环境变量。例如,要使用 --enable-source-maps--inspect,请按如下方式调用 QUnit

NODE_OPTIONS='--enable-source-maps' qunit test/

代码覆盖率

使用 nyc 生成代码覆盖率报告

{
  "scripts": {
    "test": "nyc qunit"
  },
  "devDependencies": {
    "nyc": "*",
    "qunit": "*"
  }
}

在 QUnit 仓库中查看 /test/integration/nyc/ 获取一个最小示例。

有关展示 Node.js 和无头浏览器中测试的统一测试覆盖率报告的更详细示例,请查看 Krinkle/example-node-and-browser-qunit