测试运行器执行模型


【Test runner execution model】

当搜索要执行的测试文件时,测试运行器的行为如下:

【When searching for test files to execute, the test runner behaves as follows:】

  • 用户明确提供的任何文件都会被执行。
  • 如果用户没有明确指定任何路径,将按照以下步骤在当前工作目录中递归搜索文件。
  • 除非用户明确提供,否则会跳过 node_modules 目录。
  • 如果遇到名为 test 的目录,测试运行器将递归搜索其中的所有 .js.cjs.mjs 文件。所有这些文件都会被视为测试文件,不需要符合下面详细说明的特定命名约定。这是为了适应将所有测试放在单个 test 目录中的项目。
  • 在所有其他目录中,匹配以下模式的 .js.cjs.mjs 文件被视为测试文件:
    • ^test$ - 基名为字符串 'test' 的文件。例如:test.jstest.cjstest.mjs
    • ^test-.+ - 文件名以字符串 'test-' 开头,后跟一个或多个字符。例如:test-example.jstest-another-example.mjs
    • .+[\.\-\_]test$ - 文件名以 .test-test_test 结尾,前面有一个或多个字符。例如:example.test.jsexample-test.cjsexample_test.mjs
    • Node.js 能识别的其他文件类型,如 .node.json,不会被测试运行器自动执行,但如果在命令行中明确提供,则是支持的。

每个匹配的测试文件都在单独的子进程中执行。如果子进程以退出码 0 结束,则该测试被视为通过。否则,测试被视为失败。测试文件必须可以被 Node.js 执行,但不要求内部使用 node:test 模块。

【Each matching test file is executed in a separate child process. If the child process finishes with an exit code of 0, the test is considered passing. Otherwise, the test is considered to be a failure. Test files must be executable by Node.js, but are not required to use the node:test module internally.】