从命令行运行测试


【Running tests from the command line】

可以通过在命令行传递 --test 标志来调用 Node.js 测试运行器:

【The Node.js test runner can be invoked from the command line by passing the --test flag:】

node --test 

默认情况下,Node.js 将运行与这些模式匹配的所有文件:

【By default, Node.js will run all files matching these patterns:】

  • **/*.test.{cjs,mjs,js}
  • **/*-test.{cjs,mjs,js}
  • **/*_test.{cjs,mjs,js}
  • **/test-*.{cjs,mjs,js}
  • **/test.{cjs,mjs,js}
  • **/test/**/*.{cjs,mjs,js}

除非提供 --no-strip-types,否则还会匹配以下附加模式:

【Unless --no-strip-types is supplied, the following additional patterns are also matched:】

  • **/*.test.{cts,mts,ts}
  • **/*-test.{cts,mts,ts}
  • **/*_test.{cts,mts,ts}
  • **/test-*.{cts,mts,ts}
  • **/test.{cts,mts,ts}
  • **/test/**/*.{cts,mts,ts}

或者,可以将一个或多个全局模式作为 Node.js 命令的最后参数提供,如下所示。全局模式遵循 glob(7) 的行为。为了防止 shell 扩展导致的跨系统兼容性问题,命令行中的全局模式应使用双引号括起来。

【Alternatively, one or more glob patterns can be provided as the final argument(s) to the Node.js command, as shown below. Glob patterns follow the behavior of glob(7). The glob patterns should be enclosed in double quotes on the command line to prevent shell expansion, which can reduce portability across systems.】

node --test "**/*.test.js" "**/*.spec.js" 

匹配的文件将作为测试文件执行。有关测试文件执行的更多信息,请参见测试运行器执行模型部分。

【Matching files are executed as test files. More information on the test file execution can be found in the test runner execution model section.】