全局变量
在你的测试文件中,Jest 将每个方法和对象放入全局环境中。你无需要求或导入任何内容即可使用它们。但是,如果你更喜欢显式导入,则可以执行 import {describe, expect, test} from '@jest/globals'
。
¥In your test files, Jest puts each of these methods and objects into the global environment. You don't have to require or import anything to use them. However, if you prefer explicit imports, you can do import {describe, expect, test} from '@jest/globals'
.
仅当你显式导入 Jest API 时,此页面中的 TypeScript 示例才会按照文档说明的方式工作:
¥The TypeScript examples from this page will only work as documented if you explicitly import Jest APIs:
import {expect, jest, test} from '@jest/globals';
有关如何使用 TypeScript 设置 Jest 的详细信息,请参阅 入门 指南。
¥Consult the Getting Started guide for details on how to setup Jest with TypeScript.
方法
¥Methods
- 参考
afterAll(fn, timeout)
afterEach(fn, timeout)
beforeAll(fn, timeout)
beforeEach(fn, timeout)
describe(name, fn)
describe.each(table)(name, fn, timeout)
describe.only(name, fn)
describe.only.each(table)(name, fn)
describe.skip(name, fn)
describe.skip.each(table)(name, fn)
test(name, fn, timeout)
test.concurrent(name, fn, timeout)
test.concurrent.each(table)(name, fn, timeout)
test.concurrent.only.each(table)(name, fn)
test.concurrent.skip.each(table)(name, fn)
test.each(table)(name, fn, timeout)
test.failing(name, fn, timeout)
test.failing.each(name, fn, timeout)
test.only.failing(name, fn, timeout)
test.skip.failing(name, fn, timeout)
test.only(name, fn, timeout)
test.only.each(table)(name, fn)
test.skip(name, fn)
test.skip.each(table)(name, fn)
test.todo(name)
- TypeScript 用法
参考
¥Reference
afterAll(fn, timeout)
该文件中的所有测试完成后运行函数。如果函数返回一个 Promise 或者是一个生成器,Jest 会等待该 Promise 解析后再继续。
¥Runs a function after all the tests in this file have completed. If the function returns a promise or is a generator, Jest waits for that promise to resolve before continuing.
或者,你可以提供 timeout
(以毫秒为单位)来指定中止之前等待的时间。默认超时为 5 秒。
¥Optionally, you can provide a timeout
(in milliseconds) for specifying how long to wait before aborting. The default timeout is 5 seconds.
如果你想要清理跨测试共享的某些全局设置状态,这通常很有用。
¥This is often useful if you want to clean up some global setup state that is shared across tests.
例如:
¥For example:
const globalDatabase = makeGlobalDatabase();
function cleanUpDatabase(db) {
db.cleanUp();
}
afterAll(() => {
cleanUpDatabase(globalDatabase);
});
test('can find things', () => {
return globalDatabase.find('thing', {}, results => {
expect(results.length).toBeGreaterThan(0);
});
});
test('can insert a thing', () => {
return globalDatabase.insert('thing', makeThing(), response => {
expect(response.success).toBeTruthy();
});
});
这里 afterAll
确保在所有测试运行后调用 cleanUpDatabase
。
¥Here the afterAll
ensures that cleanUpDatabase
is called after all tests run.
如果 afterAll
在 describe
块内,则它在描述块的末尾运行。
¥If afterAll
is inside a describe
block, it runs at the end of the describe block.
如果你想在每次测试后而不是在所有测试后运行一些清理,请改用 afterEach
。
¥If you want to run some cleanup after every test instead of after all tests, use afterEach
instead.
afterEach(fn, timeout)
此文件中的每一项测试完成后运行一个函数。如果函数返回一个 Promise 或者是一个生成器,Jest 会等待该 Promise 解析后再继续。
¥Runs a function after each one of the tests in this file completes. If the function returns a promise or is a generator, Jest waits for that promise to resolve before continuing.
或者,你可以提供 timeout
(以毫秒为单位)来指定中止之前等待的时间。默认超时为 5 秒。
¥Optionally, you can provide a timeout
(in milliseconds) for specifying how long to wait before aborting. The default timeout is 5 seconds.
如果你想要清理每个测试创建的一些临时状态,这通常很有用。
¥This is often useful if you want to clean up some temporary state that is created by each test.
例如:
¥For example:
const globalDatabase = makeGlobalDatabase();
function cleanUpDatabase(db) {
db.cleanUp();
}
afterEach(() => {
cleanUpDatabase(globalDatabase);
});
test('can find things', () => {
return globalDatabase.find('thing', {}, results => {
expect(results.length).toBeGreaterThan(0);
});
});
test('can insert a thing', () => {
return globalDatabase.insert('thing', makeThing(), response => {
expect(response.success).toBeTruthy();
});
});
这里 afterEach
确保每次测试运行后调用 cleanUpDatabase
。
¥Here the afterEach
ensures that cleanUpDatabase
is called after each test runs.
如果 afterEach
位于 describe
块内,则它仅在该描述块内的测试之后运行。
¥If afterEach
is inside a describe
block, it only runs after the tests that are inside this describe block.
如果你只想在所有测试运行后运行一次清理操作,请改用 afterAll
。
¥If you want to run some cleanup just once, after all of the tests run, use afterAll
instead.
beforeAll(fn, timeout)
在运行此文件中的任何测试之前运行一个函数。如果函数返回一个 Promise 或者是一个生成器,Jest 会在运行测试之前等待该 Promise 解析。
¥Runs a function before any of the tests in this file run. If the function returns a promise or is a generator, Jest waits for that promise to resolve before running tests.
或者,你可以提供 timeout
(以毫秒为单位)来指定中止之前等待的时间。默认超时为 5 秒。
¥Optionally, you can provide a timeout
(in milliseconds) for specifying how long to wait before aborting. The default timeout is 5 seconds.
如果你想设置一些将由许多测试使用的全局状态,这通常很有用。
¥This is often useful if you want to set up some global state that will be used by many tests.
例如:
¥For example:
const globalDatabase = makeGlobalDatabase();
beforeAll(() => {
// Clears the database and adds some testing data.
// Jest will wait for this promise to resolve before running tests.
return globalDatabase.clear().then(() => {
return globalDatabase.insert({testData: 'foo'});
});
});
// Since we only set up the database once in this example, it's important
// that our tests don't modify it.
test('can find things', () => {
return globalDatabase.find('thing', {}, results => {
expect(results.length).toBeGreaterThan(0);
});
});
这里,beforeAll
确保在测试运行之前设置数据库。如果设置是同步的,你可以在没有 beforeAll
的情况下执行此操作。关键是 Jest 将等待 promise 解决,因此你也可以进行异步设置。
¥Here the beforeAll
ensures that the database is set up before tests run. If setup was synchronous, you could do this without beforeAll
. The key is that Jest will wait for a promise to resolve, so you can have asynchronous setup as well.
如果 beforeAll
在 describe
块内,则它在描述块的开头运行。
¥If beforeAll
is inside a describe
block, it runs at the beginning of the describe block.
如果你想在每个测试之前而不是任何测试运行之前运行某些内容,请改用 beforeEach
。
¥If you want to run something before every test instead of before any test runs, use beforeEach
instead.
beforeEach(fn, timeout)
在运行此文件中的每个测试之前运行一个函数。如果函数返回一个 Promise 或者是一个生成器,Jest 会在运行测试之前等待该 Promise 解析。
¥Runs a function before each of the tests in this file runs. If the function returns a promise or is a generator, Jest waits for that promise to resolve before running the test.
或者,你可以提供 timeout
(以毫秒为单位)来指定中止之前等待的时间。默认超时为 5 秒。
¥Optionally, you can provide a timeout
(in milliseconds) for specifying how long to wait before aborting. The default timeout is 5 seconds.
如果你想重置许多测试将使用的某些全局状态,这通常很有用。
¥This is often useful if you want to reset some global state that will be used by many tests.
例如:
¥For example:
const globalDatabase = makeGlobalDatabase();
beforeEach(() => {
// Clears the database and adds some testing data.
// Jest will wait for this promise to resolve before running tests.
return globalDatabase.clear().then(() => {
return globalDatabase.insert({testData: 'foo'});
});
});
test('can find things', () => {
return globalDatabase.find('thing', {}, results => {
expect(results.length).toBeGreaterThan(0);
});
});
test('can insert a thing', () => {
return globalDatabase.insert('thing', makeThing(), response => {
expect(response.success).toBeTruthy();
});
});
这里 beforeEach
确保每次测试都会重置数据库。
¥Here the beforeEach
ensures that the database is reset for each test.
如果 beforeEach
位于 describe
块内,则它针对描述块中的每个测试运行。
¥If beforeEach
is inside a describe
block, it runs for each test in the describe block.
如果你只需要在运行任何测试之前运行一些设置代码一次,请改用 beforeAll
。
¥If you only need to run some setup code once, before any tests run, use beforeAll
instead.
describe(name, fn)
describe(name, fn)
创建一个将多个相关测试组合在一起的块。例如,如果你有一个 myBeverage
对象,该对象应该是美味但不酸的,你可以使用以下命令来测试它:
¥describe(name, fn)
creates a block that groups together several related tests. For example, if you have a myBeverage
object that is supposed to be delicious but not sour, you could test it with:
const myBeverage = {
delicious: true,
sour: false,
};
describe('my beverage', () => {
test('is delicious', () => {
expect(myBeverage.delicious).toBeTruthy();
});
test('is not sour', () => {
expect(myBeverage.sour).toBeFalsy();
});
});
这不是必需的 - 你可以直接在顶层写入 test
块。但如果你希望将测试组织成组,这会很方便。
¥This isn't required - you can write the test
blocks directly at the top level. But this can be handy if you prefer your tests to be organized into groups.
如果你有测试层次结构,你还可以嵌套 describe
块:
¥You can also nest describe
blocks if you have a hierarchy of tests:
const binaryStringToNumber = binString => {
if (!/^[01]+$/.test(binString)) {
throw new CustomError('Not a binary number.');
}
return parseInt(binString, 2);
};
describe('binaryStringToNumber', () => {
describe('given an invalid binary string', () => {
test('composed of non-numbers throws CustomError', () => {
expect(() => binaryStringToNumber('abc')).toThrow(CustomError);
});
test('with extra whitespace throws CustomError', () => {
expect(() => binaryStringToNumber(' 100')).toThrow(CustomError);
});
});
describe('given a valid binary string', () => {
test('returns the correct number', () => {
expect(binaryStringToNumber('100')).toBe(4);
});
});
});
describe.each(table)(name, fn, timeout)
如果你不断使用不同的数据重复相同的测试套件,请使用 describe.each
。describe.each
允许你编写一次测试套件并传入数据。
¥Use describe.each
if you keep duplicating the same test suites with different data. describe.each
allows you to write the test suite once and pass data in.
describe.each
有两个 API:
¥describe.each
is available with two APIs:
1. describe.each(table)(name, fn, timeout)
-
table
:数组的Array
以及每行传递到fn
中的参数。如果你传入一个一维基础类型数组,它将在内部映射到一个表,即[1, 2, 3] -> [[1], [2], [3]]
。¥
table
:Array
of Arrays with the arguments that are passed into thefn
for each row. If you pass in a 1D array of primitives, internally it will be mapped to a table i.e.[1, 2, 3] -> [[1], [2], [3]]
. -
name
:String
测试套件的标题。¥
name
:String
the title of the test suite.-
通过使用
printf
格式 按位置注入参数来生成唯一的测试标题:¥Generate unique test titles by positionally injecting parameters with
printf
formatting:-
%p
- pretty-format。 -
%s
-字符串。¥
%s
- String. -
%d
-编号。¥
%d
- Number. -
%i
- 整数。¥
%i
- Integer. -
%f
- 浮点值。¥
%f
- Floating point value. -
%j
- JSON。 -
%o
- 目的。¥
%o
- Object. -
%#
- 测试用例的索引。¥
%#
- Index of the test case. -
%$
- 测试用例编号。¥
%$
- Number of the test case. -
%%
- 单个百分号 ('%')。这不会消耗参数。¥
%%
- single percent sign ('%'). This does not consume an argument.
-
-
或者通过使用
$variable
注入测试用例对象的属性来生成唯一的测试标题¥Or generate unique test titles by injecting properties of test case object with
$variable
-
要注入嵌套对象值,你可以使用 keyPath,即
$variable.path.to.value
(仅适用于 "own" 属性,例如$variable.constructor.name
不起作用)¥To inject nested object values use you can supply a keyPath i.e.
$variable.path.to.value
(only works for "own" properties, e.g.$variable.constructor.name
wouldn't work) -
你可以使用
$#
注入测试用例的索引¥You can use
$#
to inject the index of the test case -
除
%%
之外,不能将$variable
与printf
格式一起使用¥You cannot use
$variable
with theprintf
formatting except for%%
-
-
-
fn
:Function
要运行的测试套件,这是将接收每行中的参数作为函数参数的函数。¥
fn
:Function
the suite of tests to be run, this is the function that will receive the parameters in each row as function arguments. -
或者,你可以提供
timeout
(以毫秒为单位)来指定中止之前等待每行的时间。默认超时为 5 秒。¥Optionally, you can provide a
timeout
(in milliseconds) for specifying how long to wait for each row before aborting. The default timeout is 5 seconds.
示例:
¥Example:
describe.each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
])('.add(%i, %i)', (a, b, expected) => {
test(`returns ${expected}`, () => {
expect(a + b).toBe(expected);
});
test(`returned value not be greater than ${expected}`, () => {
expect(a + b).not.toBeGreaterThan(expected);
});
test(`returned value not be less than ${expected}`, () => {
expect(a + b).not.toBeLessThan(expected);
});
});
describe.each([
{a: 1, b: 1, expected: 2},
{a: 1, b: 2, expected: 3},
{a: 2, b: 1, expected: 3},
])('.add($a, $b)', ({a, b, expected}) => {
test(`returns ${expected}`, () => {
expect(a + b).toBe(expected);
});
test(`returned value not be greater than ${expected}`, () => {
expect(a + b).not.toBeGreaterThan(expected);
});
test(`returned value not be less than ${expected}`, () => {
expect(a + b).not.toBeLessThan(expected);
});
});
2. describe.each
table(name, fn, timeout)
-
table
:Tagged Template Literal
-
第一行变量名称列标题以
|
分隔¥First row of variable name column headings separated with
|
-
使用
${value}
语法作为模板字面量表达式提供的一行或多行后续数据。¥One or more subsequent rows of data supplied as template literal expressions using
${value}
syntax.
-
-
name
:String
是测试套件的标题,使用$variable
将测试数据从标记的模板表达式注入到套件标题中,$#
是行的索引。¥
name
:String
the title of the test suite, use$variable
to inject test data into the suite title from the tagged template expressions, and$#
for the index of the row.-
要注入嵌套对象值,你可以使用 keyPath,即
$variable.path.to.value
(仅适用于 "own" 属性,例如$variable.constructor.name
不起作用)¥To inject nested object values use you can supply a keyPath i.e.
$variable.path.to.value
(only works for "own" properties, e.g.$variable.constructor.name
wouldn't work)
-
-
fn
:Function
要运行的测试套件,这是将接收测试数据对象的函数。¥
fn
:Function
the suite of tests to be run, this is the function that will receive the test data object. -
或者,你可以提供
timeout
(以毫秒为单位)来指定中止之前等待每行的时间。默认超时为 5 秒。¥Optionally, you can provide a
timeout
(in milliseconds) for specifying how long to wait for each row before aborting. The default timeout is 5 seconds.
示例:
¥Example:
describe.each`
a | b | expected
${1} | ${1} | ${2}
${1} | ${2} | ${3}
${2} | ${1} | ${3}
`('$a + $b', ({a, b, expected}) => {
test(`returns ${expected}`, () => {
expect(a + b).toBe(expected);
});
test(`returned value not be greater than ${expected}`, () => {
expect(a + b).not.toBeGreaterThan(expected);
});
test(`returned value not be less than ${expected}`, () => {
expect(a + b).not.toBeLessThan(expected);
});
});
describe.only(name, fn)
也在别名下:fdescribe(name, fn)
¥Also under the alias: fdescribe(name, fn)
如果你只想运行一个描述块,则可以使用 describe.only
:
¥You can use describe.only
if you want to run only one describe block:
describe.only('my beverage', () => {
test('is delicious', () => {
expect(myBeverage.delicious).toBeTruthy();
});
test('is not sour', () => {
expect(myBeverage.sour).toBeFalsy();
});
});
describe('my other beverage', () => {
// ... will be skipped
});
describe.only.each(table)(name, fn)
也在别名下:fdescribe.each(table)(name, fn)
和 fdescribe.each
table(name, fn)
¥Also under the aliases: fdescribe.each(table)(name, fn)
and fdescribe.each`table`(name, fn)
如果你只想运行数据驱动测试的特定测试套件,请使用 describe.only.each
。
¥Use describe.only.each
if you want to only run specific tests suites of data driven tests.
describe.only.each
有两个 API:
¥describe.only.each
is available with two APIs:
describe.only.each(table)(name, fn)
describe.only.each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
])('.add(%i, %i)', (a, b, expected) => {
test(`returns ${expected}`, () => {
expect(a + b).toBe(expected);
});
});
test('will not be run', () => {
expect(1 / 0).toBe(Infinity);
});
describe.only.each
table(name, fn)
describe.only.each`
a | b | expected
${1} | ${1} | ${2}
${1} | ${2} | ${3}
${2} | ${1} | ${3}
`('returns $expected when $a is added to $b', ({a, b, expected}) => {
test('passes', () => {
expect(a + b).toBe(expected);
});
});
test('will not be run', () => {
expect(1 / 0).toBe(Infinity);
});
describe.skip(name, fn)
也在别名下:xdescribe(name, fn)
¥Also under the alias: xdescribe(name, fn)
如果你不想运行特定 describe
块的测试,可以使用 describe.skip
:
¥You can use describe.skip
if you do not want to run the tests of a particular describe
block:
describe('my beverage', () => {
test('is delicious', () => {
expect(myBeverage.delicious).toBeTruthy();
});
test('is not sour', () => {
expect(myBeverage.sour).toBeFalsy();
});
});
describe.skip('my other beverage', () => {
// ... will be skipped
});
使用 describe.skip
通常是暂时注释掉一大块测试的更干净的替代方案。请注意,describe
块仍将运行。如果你有一些设置也应该跳过,请在 beforeAll
或 beforeEach
块中进行。
¥Using describe.skip
is often a cleaner alternative to temporarily commenting out a chunk of tests. Beware that the describe
block will still run. If you have some setup that also should be skipped, do it in a beforeAll
or beforeEach
block.
describe.skip.each(table)(name, fn)
也在别名下:xdescribe.each(table)(name, fn)
和 xdescribe.each
table(name, fn)
¥Also under the aliases: xdescribe.each(table)(name, fn)
and xdescribe.each`table`(name, fn)
如果你想停止运行一套数据驱动测试,请使用 describe.skip.each
。
¥Use describe.skip.each
if you want to stop running a suite of data driven tests.
describe.skip.each
有两个 API:
¥describe.skip.each
is available with two APIs:
describe.skip.each(table)(name, fn)
describe.skip.each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
])('.add(%i, %i)', (a, b, expected) => {
test(`returns ${expected}`, () => {
expect(a + b).toBe(expected); // will not be run
});
});
test('will be run', () => {
expect(1 / 0).toBe(Infinity);
});
describe.skip.each
table(name, fn)
describe.skip.each`
a | b | expected
${1} | ${1} | ${2}
${1} | ${2} | ${3}
${2} | ${1} | ${3}
`('returns $expected when $a is added to $b', ({a, b, expected}) => {
test('will not be run', () => {
expect(a + b).toBe(expected); // will not be run
});
});
test('will be run', () => {
expect(1 / 0).toBe(Infinity);
});
test(name, fn, timeout)
也在别名下:it(name, fn, timeout)
¥Also under the alias: it(name, fn, timeout)
测试文件中你所需要的只是运行测试的 test
方法。例如,假设有一个函数 inchesOfRain()
应为零。你的整个测试可能是:
¥All you need in a test file is the test
method which runs a test. For example, let's say there's a function inchesOfRain()
that should be zero. Your whole test could be:
test('did not rain', () => {
expect(inchesOfRain()).toBe(0);
});
第一个参数是测试名称;第二个参数是一个包含要测试的期望的函数。第三个参数(可选)是 timeout
(以毫秒为单位),用于指定中止之前等待的时间。默认超时为 5 秒。
¥The first argument is the test name; the second argument is a function that contains the expectations to test. The third argument (optional) is timeout
(in milliseconds) for specifying how long to wait before aborting. The default timeout is 5 seconds.
如果从 test
返回 Promise,Jest 将等待 Promise 解析后再让测试完成。例 如,假设 fetchBeverageList()
返回一个 promise,该 promise 应该解析为其中包含 lemon
的列表。你可以使用以下方法进行测试:
¥If a promise is returned from test
, Jest will wait for the promise to resolve before letting the test complete. For example, let's say fetchBeverageList()
returns a promise that is supposed to resolve to a list that has lemon
in it. You can test this with:
test('has lemon in it', () => {
return fetchBeverageList().then(list => {
expect(list).toContain('lemon');
});
});
尽管对 test
的调用将立即返回,但在 promise 解决之前测试不会完成。欲了解更多详情,请参阅 测试异步代码 页。
¥Even though the call to test
will return right away, the test doesn't complete until the promise resolves. For more details, see Testing Asynchronous Code page.
test.concurrent(name, fn, timeout)
也在别名下:it.concurrent(name, fn, timeout)
¥Also under the alias: it.concurrent(name, fn, timeout)
如果你希望测试同时运行,请使用 test.concurrent
。
¥Use test.concurrent
if you want the test to run concurrently.
第一个参数是测试名称;第二个参数是一个异步函数,其中包含要测试的期望。第三个参数(可选)是 timeout
(以毫秒为单位),用于指定中止之前等待的时间。默认超时为 5 秒。
¥The first argument is the test name; the second argument is an asynchronous function that contains the expectations to test. The third argument (optional) is timeout
(in milliseconds) for specifying how long to wait before aborting. The default timeout is 5 seconds.
test.concurrent('addition of 2 numbers', async () => {
expect(5 + 3).toBe(8);
});
test.concurrent('subtraction 2 numbers', async () => {
expect(5 - 3).toBe(2);
});
使用 maxConcurrency
配置选项可防止 Jest 同时执行超过指定数量的测试。
¥Use the maxConcurrency
configuration option to prevent Jest from executing more than the specified amount of tests at the same time.
test.concurrent.each(table)(name, fn, timeout)
也在别名下:it.concurrent.each(table)(name, fn, timeout)
¥Also under the alias: it.concurrent.each(table)(name, fn, timeout)
如果你不断使用不同的数据重复相同的测试,请使用 test.concurrent.each
。test.each
允许你编写一次测试并传入数据,测试全部异步运行。
¥Use test.concurrent.each
if you keep duplicating the same test with different data. test.each
allows you to write the test once and pass data in, the tests are all run asynchronously.
test.concurrent.each
有两个 API:
¥test.concurrent.each
is available with two APIs:
1. test.concurrent.each(table)(name, fn, timeout)
-
table
:数组的Array
以及传递到每行测试fn
中的参数。如果你传入一个一维基础类型数组,它将在内部映射到一 个表,即[1, 2, 3] -> [[1], [2], [3]]
¥
table
:Array
of Arrays with the arguments that are passed into the testfn
for each row. If you pass in a 1D array of primitives, internally it will be mapped to a table i.e.[1, 2, 3] -> [[1], [2], [3]]
-
name
:String
测试块的标题。¥
name
:String
the title of the test block.-
通过使用
printf
格式 按位置注入参数来生成唯一的测试标题:¥Generate unique test titles by positionally injecting parameters with
printf
formatting:-
%p
- pretty-format。 -
%s
-字符串。¥
%s
- String. -
%d
-编号。¥
%d
- Number. -
%i
- 整数。¥
%i
- Integer. -
%f
- 浮点值。¥
%f
- Floating point value. -
%j
- JSON。 -
%o
- 目的。¥
%o
- Object. -
%#
- 测试用例的索引。¥
%#
- Index of the test case. -
%$
- 测试用例编号。¥
%$
- Number of the test case. -
%%
- 单个百分号 ('%')。这不会消耗参数。¥
%%
- single percent sign ('%'). This does not consume an argument.
-
-
-
fn
:Function
要运行的测试,这是将接收每行中的参数作为函数参数的函数,这必须是异步函数。¥
fn
:Function
the test to be run, this is the function that will receive the parameters in each row as function arguments, this will have to be an asynchronous function. -
或者,你可以提供
timeout
(以毫秒为单位)来指定中止之前等待每行的时间。默认超时为 5 秒。¥Optionally, you can provide a
timeout
(in milliseconds) for specifying how long to wait for each row before aborting. The default timeout is 5 seconds.
示例:
¥Example:
test.concurrent.each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
])('.add(%i, %i)', async (a, b, expected) => {
expect(a + b).toBe(expected);
});
2. test.concurrent.each
table(name, fn, timeout)
-
table
:Tagged Template Literal
-
第一行变量名称列标题以
|
分隔¥First row of variable name column headings separated with
|
-
使用
${value}
语法作为模板字面量表达式提供的一行或多行后续数据。¥One or more subsequent rows of data supplied as template literal expressions using
${value}
syntax.
-
-
name
:String
测试的标题,使用$variable
将测试数据从标记的模板表达式注入到测试标题中。¥
name
:String
the title of the test, use$variable
to inject test data into the test title from the tagged template expressions.-
要注入嵌套对象值,你可以使用 keyPath,即
$variable.path.to.value
(仅适用于 "own" 属性,例如$variable.constructor.name
不起作用)¥To inject nested object values use you can supply a keyPath i.e.
$variable.path.to.value
(only works for "own" properties, e.g.$variable.constructor.name
wouldn't work)
-
-
fn
:Function
要运行的测试,这是将接收测试数据对象的函数,这必须是异步函数。¥
fn
:Function
the test to be run, this is the function that will receive the test data object, this will have to be an asynchronous function. -
或者,你可以提供
timeout
(以毫秒为单位)来指定中止之前等待每行的时间。默认超时为 5 秒。¥Optionally, you can provide a
timeout
(in milliseconds) for specifying how long to wait for each row before aborting. The default timeout is 5 seconds.
示例:
¥Example:
test.concurrent.each`
a | b | expected
${1} | ${1} | ${2}
${1} | ${2} | ${3}
${2} | ${1} | ${3}
`('returns $expected when $a is added to $b', async ({a, b, expected}) => {
expect(a + b).toBe(expected);
});
test.concurrent.only.each(table)(name, fn)
也在别名下:it.concurrent.only.each(table)(name, fn)
¥Also under the alias: it.concurrent.only.each(table)(name, fn)
如果你只想同时运行具有不同测试数据的特定测试,请使用 test.concurrent.only.each
。
¥Use test.concurrent.only.each
if you want to only run specific tests with different test data concurrently.
test.concurrent.only.each
有两个 API:
¥test.concurrent.only.each
is available with two APIs:
test.concurrent.only.each(table)(name, fn)
test.concurrent.only.each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
])('.add(%i, %i)', async (a, b, expected) => {
expect(a + b).toBe(expected);
});
test('will not be run', () => {
expect(1 / 0).toBe(Infinity);
});
test.only.each
table(name, fn)
test.concurrent.only.each`
a | b | expected
${1} | ${1} | ${2}
${1} | ${2} | ${3}
${2} | ${1} | ${3}
`('returns $expected when $a is added to $b', async ({a, b, expected}) => {
expect(a + b).toBe(expected);
});
test('will not be run', () => {
expect(1 / 0).toBe(Infinity);
});
test.concurrent.skip.each(table)(name, fn)
也在别名下:it.concurrent.skip.each(table)(name, fn)
¥Also under the alias: it.concurrent.skip.each(table)(name, fn)
如果你想停止运行异步数据驱动测试的集合,请使用 test.concurrent.skip.each
。
¥Use test.concurrent.skip.each
if you want to stop running a collection of asynchronous data driven tests.
test.concurrent.skip.each
有两个 API:
¥test.concurrent.skip.each
is available with two APIs:
test.concurrent.skip.each(table)(name, fn)
test.concurrent.skip.each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
])('.add(%i, %i)', async (a, b, expected) => {
expect(a + b).toBe(expected); // will not be run
});
test('will be run', () => {
expect(1 / 0).toBe(Infinity);
});
test.concurrent.skip.each
table(name, fn)
test.concurrent.skip.each`
a | b | expected
${1} | ${1} | ${2}
${1} | ${2} | ${3}
${2} | ${1} | ${3}
`('returns $expected when $a is added to $b', async ({a, b, expected}) => {
expect(a + b).toBe(expected); // will not be run
});
test('will be run', () => {
expect(1 / 0).toBe(Infinity);
});
test.each(table)(name, fn, timeout)
也在别名下:it.each(table)(name, fn)
和 it.each
table(name, fn)