run([options])


  • options <Object> 运行测试的配置选项。支持以下属性:

    ¥options <Object> Configuration options for running tests. The following properties are supported:

    • concurrency <number> | <boolean> 如果提供了一个数字,那么许多测试进程将并行运行,其中每个进程对应一个测试文件。如果是 true,它将并行运行 os.availableParallelism() - 1 个测试文件。如果是 false,它一次只会运行一个测试文件。默认值:false

      ¥concurrency <number> | <boolean> If a number is provided, then that many test processes would run in parallel, where each process corresponds to one test file. If true, it would run os.availableParallelism() - 1 test files in parallel. If false, it would only run one test file at a time. Default: false.

    • cwd<string> 指定测试运行器要使用的当前工作目录。根据 测试运行器执行模型 用作解析文件的基本路径。默认值:process.cwd()

      ¥cwd: <string> Specifies the current working directory to be used by the test runner. Serves as the base path for resolving files according to the test runner execution model. Default: process.cwd().

    • files<Array> 包含要运行的文件列表的数组。默认值:来自 测试运行器执行模型 的匹配文件。

      ¥files: <Array> An array containing the list of files to run. Default: matching files from test runner execution model.

    • forceExit<boolean> 配置测试运行器在所有已知测试完成执行后退出进程,即使事件循环将保持活动状态。默认值:false

      ¥forceExit: <boolean> Configures the test runner to exit the process once all known tests have finished executing even if the event loop would otherwise remain active. Default: false.

    • globPatterns<Array> 包含用于匹配测试文件的 glob 模式列表的数组。此选项不能与 files 一起使用。默认值:来自 测试运行器执行模型 的匹配文件。

      ¥globPatterns: <Array> An array containing the list of glob patterns to match test files. This option cannot be used together with files. Default: matching files from test runner execution model.

    • inspectPort <number> | <Function> 设置测试子进程的检查器端口。这可以是数字,也可以是不带参数并返回数字的函数。如果提供了一个空值,每个进程都有自己的端口,从主进程的 process.debugPort 递增。如果 isolation 选项设置为 'none',则忽略此选项,因为没有生成子进程。默认值:undefined

      ¥inspectPort <number> | <Function> Sets inspector port of test child process. This can be a number, or a function that takes no arguments and returns a number. If a nullish value is provided, each process gets its own port, incremented from the primary's process.debugPort. This option is ignored if the isolation option is set to 'none' as no child processes are spawned. Default: undefined.

    • isolation <string> 配置测试隔离的类型。如果设置为 'process',则每个测试文件都在单独的子进程中运行。如果设置为 'none',则所有测试文件都在当前进程中运行。默认值:'process'

      ¥isolation <string> Configures the type of test isolation. If set to 'process', each test file is run in a separate child process. If set to 'none', all test files run in the current process. Default: 'process'.

    • only<boolean> 如果为真,测试上下文将仅运行设置了 only 选项的测试

      ¥only: <boolean> If truthy, the test context will only run tests that have the only option set

    • setup <Function> 接受 TestsStream 实例并可用于在运行任何测试之前设置监听器的函数。默认值:undefined

      ¥setup <Function> A function that accepts the TestsStream instance and can be used to setup listeners before any tests are run. Default: undefined.

    • execArgv <Array> 生成子进程时传递给 node 可执行文件的 CLI 标志数组。当 isolation'none' 时,此选项无效。默认值:[]

      ¥execArgv <Array> An array of CLI flags to pass to the node executable when spawning the subprocesses. This option has no effect when isolation is 'none'. Default: []

    • argv <Array> 生成子进程时传递给每个测试文件的 CLI 标志数组。当 isolation'none' 时,此选项无效。默认值:[]

      ¥argv <Array> An array of CLI flags to pass to each test file when spawning the subprocesses. This option has no effect when isolation is 'none'. Default: [].

    • signal <AbortSignal> 允许中止正在进行的测试执行。

      ¥signal <AbortSignal> Allows aborting an in-progress test execution.

    • testNamePatterns <string> | <RegExp> | <Array> 一个字符串、正则表达式或正则表达式数组,可用于仅运行名称与提供的模式匹配的测试。测试名称模式被解释为 JavaScript 正则表达式。对于执行的每个测试,也会运行任何相应的测试钩子,例如 beforeEach()。默认值:undefined

      ¥testNamePatterns <string> | <RegExp> | <Array> A String, RegExp or a RegExp Array, that can be used to only run tests whose name matches the provided pattern. Test name patterns are interpreted as JavaScript regular expressions. For each test that is executed, any corresponding test hooks, such as beforeEach(), are also run. Default: undefined.

    • testSkipPatterns <string> | <RegExp> | <Array> 字符串、RegExp 或 RegExp 数组,可用于排除名称与提供的模式匹配的正在运行的测试。测试名称模式被解释为 JavaScript 正则表达式。对于执行的每个测试,也会运行任何相应的测试钩子,例如 beforeEach()。默认值:undefined

      ¥testSkipPatterns <string> | <RegExp> | <Array> A String, RegExp or a RegExp Array, that can be used to exclude running tests whose name matches the provided pattern. Test name patterns are interpreted as JavaScript regular expressions. For each test that is executed, any corresponding test hooks, such as beforeEach(), are also run. Default: undefined.

    • timeout <number> 测试执行将在几毫秒后失败。如果未指定,则子测试从其父测试继承此值。默认值:Infinity

      ¥timeout <number> A number of milliseconds the test execution will fail after. If unspecified, subtests inherit this value from their parent. Default: Infinity.

    • watch <boolean> 是否以监视模式运行。默认值:false

      ¥watch <boolean> Whether to run in watch mode or not. Default: false.

    • shard <Object> 在特定分片中运行测试。默认值:undefined

      ¥shard <Object> Running tests in a specific shard. Default: undefined.

      • index <number> 是 1 到 <total> 之间的正整数,指定要运行的分片的索引。此选项是必需的。

        ¥index <number> is a positive integer between 1 and <total> that specifies the index of the shard to run. This option is required.

      • total <number> 是一个正整数,指定将测试文件拆分为的分片总数。此选项是必需的。

        ¥total <number> is a positive integer that specifies the total number of shards to split the test files to. This option is required.

    • coverage <boolean> 启用 代码覆盖率 集合。默认值:false

      ¥coverage <boolean> enable code coverage collection. Default: false.

    • coverageExcludeGlobs <string> | <Array> 使用 glob 模式从代码覆盖范围中排除特定文件,该模式可以匹配绝对和相对文件路径。此属性仅在 coverage 设置为 true 时适用。如果同时提供了 coverageExcludeGlobscoverageIncludeGlobs,则文件必须满足两个条件才能包含在覆盖率报告中。默认值:undefined

      ¥coverageExcludeGlobs <string> | <Array> Excludes specific files from code coverage using a glob pattern, which can match both absolute and relative file paths. This property is only applicable when coverage was set to true. If both coverageExcludeGlobs and coverageIncludeGlobs are provided, files must meet both criteria to be included in the coverage report. Default: undefined.

    • coverageIncludeGlobs <string> | <Array> 使用 glob 模式在代码覆盖范围内包含特定文件,该模式可以匹配绝对和相对文件路径。此属性仅在 coverage 设置为 true 时适用。如果同时提供了 coverageExcludeGlobscoverageIncludeGlobs,则文件必须满足两个条件才能包含在覆盖率报告中。默认值:undefined

      ¥coverageIncludeGlobs <string> | <Array> Includes specific files in code coverage using a glob pattern, which can match both absolute and relative file paths. This property is only applicable when coverage was set to true. If both coverageExcludeGlobs and coverageIncludeGlobs are provided, files must meet both criteria to be included in the coverage report. Default: undefined.

    • lineCoverage <number> 要求覆盖行的最小百分比。如果代码覆盖率未达到指定的阈值,则该过程将以代码 1 退出。默认值:0

      ¥lineCoverage <number> Require a minimum percent of covered lines. If code coverage does not reach the threshold specified, the process will exit with code 1. Default: 0.

    • branchCoverage <number> 要求覆盖分支的最小百分比。如果代码覆盖率未达到指定的阈值,则该过程将以代码 1 退出。默认值:0

      ¥branchCoverage <number> Require a minimum percent of covered branches. If code coverage does not reach the threshold specified, the process will exit with code 1. Default: 0.

    • functionCoverage <number> 要求覆盖函数的最小百分比。如果代码覆盖率未达到指定的阈值,则该过程将以代码 1 退出。默认值:0

      ¥functionCoverage <number> Require a minimum percent of covered functions. If code coverage does not reach the threshold specified, the process will exit with code 1. Default: 0.

  • 返回:<TestsStream>

    ¥Returns: <TestsStream>

注意:shard 用于跨机器或进程水平并行测试运行,非常适合跨不同环境的大规模执行。它与 watch 模式不兼容,该模式是通过自动重新运行文件更改测试来实现快速代码迭代而定制的。

¥Note: shard is used to horizontally parallelize test running across machines or processes, ideal for large-scale executions across varied environments. It's incompatible with watch mode, tailored for rapid code iteration by automatically rerunning tests on file changes.

import { tap } from 'node:test/reporters';
import { run } from 'node:test';
import process from 'node:process';
import path from 'node:path';

run({ files: [path.resolve('./tests/test.js')] })
 .on('test:fail', () => {
   process.exitCode = 1;
 })
 .compose(tap)
 .pipe(process.stdout);const { tap } = require('node:test/reporters');
const { run } = require('node:test');
const path = require('node:path');

run({ files: [path.resolve('./tests/test.js')] })
 .on('test:fail', () => {
   process.exitCode = 1;
 })
 .compose(tap)
 .pipe(process.stdout);