测试运行器执行模型


【Test runner execution model】

当启用进程级测试隔离时,每个匹配的测试文件都会在单独的子进程中执行。任何时刻运行的子进程的最大数量由 --test-concurrency 标志控制。如果子进程以 0 的退出代码结束,则测试被视为通过。否则,测试被视为失败。测试文件必须能够由 Node.js 执行,但不要求内部必须使用 node:test 模块。

【When process-level test isolation is enabled, each matching test file is executed in a separate child process. The maximum number of child processes running at any time is controlled by the --test-concurrency flag. 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.】

每个测试文件都会像普通脚本一样执行。也就是说,如果测试文件本身使用 node:test 来定义测试,那么所有这些测试都会在单个应用线程中执行,而不管 test()concurrency 选项的值是多少。

【Each test file is executed as if it was a regular script. That is, if the test file itself uses node:test to define tests, all of those tests will be executed within a single application thread, regardless of the value of the concurrency option of test().】

当进程级别的测试隔离被禁用时,每个匹配的测试文件都会被导入到测试运行器进程中。一旦所有测试文件都被加载,顶层测试将以单线程的方式执行。由于所有测试文件都在相同的上下文中运行,因此测试之间可能会以在启用隔离时无法出现的方式相互影响。例如,如果某个测试依赖于全局状态,那么该状态可能会被来自其他文件的测试修改。

【When process-level test isolation is disabled, each matching test file is imported into the test runner process. Once all test files have been loaded, the top level tests are executed with a concurrency of one. Because the test files are all run within the same context, it is possible for tests to interact with each other in ways that are not possible when isolation is enabled. For example, if a test relies on global state, it is possible for that state to be modified by a test originating from another file.】