从命令行运行测试


¥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}

提供 --experimental-strip-types 时,将匹配以下附加模式:

¥When --experimental-strip-types is supplied, the following additional patterns are 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}

或者,可以提供一个或多个 glob 模式作为 Node.js 命令的最终参数,如下所示。Glob 模式遵循 glob(7) 的行为。glob 模式应在命令行上用双引号引起来,以防止 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.