test([name][, options][, fn])
name
<string> 测试的名称,报告测试结果时显示。 默认值: Thename
fn
的属性,如果fn
没有名称,则为'<anonymous>'
。options
<Object> 测试的配置选项。 支持以下属性:concurrency
<number> | <boolean> 如果提供了一个数字,则多个测试将并行运行。 如果为真,则它将并行运行 (cpu 内核 - 1) 个测试。 对于子测试,它将是Infinity
并行测试。 如果不为真,则一次只会运行一个测试。 如果未指定,则子测试从其父测试继承此值。 默认值:false
。only
<boolean> 如果为真,并且测试上下文配置为运行only
测试,则将运行此测试。 否则跳过测试。 默认值:false
。signal
<AbortSignal> 允许中止正在进行的测试。skip
<boolean> | <string> 如果为真,则跳过测试。 如果提供了字符串,则该字符串将作为跳过测试的原因显示在测试结果中。 默认值:false
。todo
<boolean> | <string> 如果为真,则测试标记为TODO
。 如果提供了字符串,则该字符串会显示在测试结果中作为测试为TODO
的原因。 默认值:false
。timeout
<number> 测试失败的毫秒数。 如果未指定,则子测试从其父测试继承此值。 默认值:Infinity
。
fn
<Function> | <AsyncFunction> 被测试的函数。 此函数的第一个参数是TestContext
对象。 如果测试使用回调,则回调函数作为第二个参数传入。 默认值: 无操作的函数。- 返回: <Promise> 测试完成后使用
undefined
解决。
test()
函数是从 test
模块导入的值。
每次调用此函数都会在 TAP 输出中创建一个测试点。
传给 fn
参数的 TestContext
对象可用于执行与当前测试相关的操作。
示例包括跳过测试、添加额外的 TAP 诊断信息、或创建子测试。
test()
返回 Promise
,一旦测试完成就解决。
返回值通常可以被顶层测试丢弃。
但是,应该使用子测试的返回值来防止父测试先完成并取消子测试,如下例所示。
test('top level test', async (t) => {
// 如果在下一行删除了 'await',
// 则以下子测试中的 setTimeout() 将导致它的父测试寿命超过其父测试。
// 一旦父测试完成,则它将取消所有未完成的子测试。
await t.test('longer running subtest', async (t) => {
return new Promise((resolve, reject) => {
setTimeout(resolve, 1000);
});
});
});
如果完成时间超过 timeout
毫秒,则可以使用 timeout
选项使测试失败。
但是,它不是取消测试的可靠机制,因为正在运行的测试可能会阻塞应用程序线程,从而阻止预定的取消。
name
<string> The name of the test, which is displayed when reporting test results. Default: Thename
property offn
, or'<anonymous>'
iffn
does not have a name.options
<Object> Configuration options for the test. The following properties are supported:concurrency
<number> | <boolean> If a number is provided, then that many tests would run in parallel. If truthy, it would run (number of cpu cores - 1) tests in parallel. For subtests, it will beInfinity
tests in parallel. If falsy, it would only run one test at a time. If unspecified, subtests inherit this value from their parent. Default:false
.only
<boolean> If truthy, and the test context is configured to runonly
tests, then this test will be run. Otherwise, the test is skipped. Default:false
.signal
<AbortSignal> Allows aborting an in-progress test.skip
<boolean> | <string> If truthy, the test is skipped. If a string is provided, that string is displayed in the test results as the reason for skipping the test. Default:false
.todo
<boolean> | <string> If truthy, the test marked asTODO
. If a string is provided, that string is displayed in the test results as the reason why the test isTODO
. Default:false
.timeout
<number> A number of milliseconds the test will fail after. If unspecified, subtests inherit this value from their parent. Default:Infinity
.
fn
<Function> | <AsyncFunction> The function under test. The first argument to this function is aTestContext
object. If the test uses callbacks, the callback function is passed as the second argument. Default: A no-op function.- Returns: <Promise> Resolved with
undefined
once the test completes.
The test()
function is the value imported from the test
module. Each
invocation of this function results in the creation of a test point in the TAP
output.
The TestContext
object passed to the fn
argument can be used to perform
actions related to the current test. Examples include skipping the test, adding
additional TAP diagnostic information, or creating subtests.
test()
returns a Promise
that resolves once the test completes. The return
value can usually be discarded for top level tests. However, the return value
from subtests should be used to prevent the parent test from finishing first
and cancelling the subtest as shown in the following example.
test('top level test', async (t) => {
// The setTimeout() in the following subtest would cause it to outlive its
// parent test if 'await' is removed on the next line. Once the parent test
// completes, it will cancel any outstanding subtests.
await t.test('longer running subtest', async (t) => {
return new Promise((resolve, reject) => {
setTimeout(resolve, 1000);
});
});
});
The timeout
option can be used to fail the test if it takes longer than
timeout
milliseconds to complete. However, it is not a reliable mechanism for
canceling tests because a running test might block the application thread and
thus prevent the scheduled cancellation.