context.test([name][, options][, fn])
name
<string> 子测试的名称,在报告测试结果时显示。 默认值: Thename
fn
的属性,如果fn
没有名称,则为'<anonymous>'
。options
<Object> 子测试的配置选项。 支持以下属性:concurrency
<number> 可同时运行的测试数。 如果未指定,则子测试从其父测试继承此值。 默认值:1
。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('top level test', async (t) => {
await t.test(
'This is a subtest',
{ only: false, skip: false, concurrency: 1, todo: false },
(t) => {
assert.ok('some relevant assertion here');
},
);
});
name
<string> The name of the subtest, 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 subtest. The following properties are supported:concurrency
<number> The number of tests that can be run at the same time. If unspecified, subtests inherit this value from their parent. Default:1
.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.
This function is used to create subtests under the current test. This function
behaves in the same fashion as the top level test()
function.
test('top level test', async (t) => {
await t.test(
'This is a subtest',
{ only: false, skip: false, concurrency: 1, todo: false },
(t) => {
assert.ok('some relevant assertion here');
},
);
});