- 全局设定
- 方法
- 参考
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)
1. 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 Usage
全局设定
Jest会将这些方法和对象注入到测试文件的全局环境里, 所以你在使用的时候不再需要进行require或者import。 如果你习惯编写明确的导入,你可以在测试文件顶部添加 import {describe, expect, test} from '@jest/globals'
。
信息
The TypeScript examples from this page will only work as documented if you explicitly import Jest APIs:
import {expect, jest, test} from '@jest/globals';
Consult the Getting Started guide for details on how to setup Jest with TypeScript.
方法
- 参考
- 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)
- 1. 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 Usage
参考
afterAll(fn, timeout)
文件内所有测试完成后执行的钩子函数。 如果传入的回调函数返回值是 promise 或者 generator,Jest 会等待 promise resolve 再继续执行。
可选地传入第二个参数 timeout(毫秒) 指定函数执行超时时间。 The default timeout is 5 seconds.
使用 afterAll 非常方便你清理一些在测试用例之间共享的全局状态。
例如:
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
完成清理操作。
如果 afterAll
定义在 describe
块的内部,它将会在 describe 块结束时执行。
如果你想每个测试完成时都运行一遍钩子,请使用 afterEach
afterEach(fn, timeout)
文件内每个测试完成后执行的钩子函数。 如果传入的回调函数返回值是 promise 或者 generator,Jest 会等待 promise resolve 再继续执行。
可选地传入第二个参数 timeout(毫秒) 指定函数执行超时时间。 The default timeout is 5 seconds.
使用 afterEach 会非常方便你清理一些在每个测试中创建的临时状态。
例如:
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
。
如果 afterEach
定义在 describe
块的内部,它会在该 describe 块内的每一个测试结束时执行。
如果你只想在所有测试完成后执行一遍钩子,请使用 afterAll
。
beforeAll(fn, timeout)
文件内所有测试开始前执行的钩子函数。 如果传入的回调函数返回值是 promise 或者 generator,Jest 会等待 promise resolve 再继续执行。
可选地传入第二个参数 timeout(毫秒) 指定函数执行超时时间。 The default timeout is 5 seconds.
使用 beforeAll 会非常方便你设置一些在测试用例之间共享的全局状态。
例如:
const globalDatabase = makeGlobalDatabase();
beforeAll(() => {
// 清空数据库并添加几条数据
// Jest 会等待 promise resolve 再去执行测试
return globalDatabase.clear().then(() => {
return globalDatabase.insert({testData: 'foo'});
});
});
// 因为我们在开始前设置了一个全局共享的数据库
// 所以我们测试时不能修改数据库的数据
test('can find things', () => {
return globalDatabase.find('thing', {}, results => {
expect(results.length).toBeGreaterThan(0);
});
});
上述代码中 beforeAll
确保在所有测试运行之前完成数据库的初始化操作。 如果初始化操作是同步的,你可以不用 beforeAll
直接执行。 关键是 Jest 会等待 promise resolve,所以可以进行异步的初始化操作。
如果 beforeAll
定义在 describe
块的内部,它将会在 describe 块开始前执行。
如果要在每次测试开始之前执行某些操作,而不是在每次测试开始之前执行,请使用beforeEach
。
beforeEach(fn, timeout)
文件内每个测试开始前执行的钩子函数。 如果传入的回调函数返回值是 promise 或者 generator,Jest 会等待 promise resolve 再继续执行测试。
可选地传入第二个参数 timeout(毫秒) 指定函数执行超时时间。 The default timeout is 5 seconds.
使用 beforeEach 非常方便你重新设置一些全局状态在每个测试开始前。
例如:
const globalDatabase = makeGlobalDatabase();
beforeEach(() => {
// Clears the database and adds some testing data.
// Jest 会等待 promise resolve 再去执行测试
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
确保了每个测试开始前会重置数据库数据。
如果 beforeEach
定义在 describe
块内,它将会在该 describe
块内的每个测试运行之前执行。
如果你需要在运行所有测试之前执行一些初始化代码, 请使用 beforeAll
。
describe(name, fn)
describe(name, fn)
是一个将多个相关的测试组合在一起的块。 比如,现在有一个myBeverage
对象,描述了某种饮料好喝但是不酸,通过以下方式测试:
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
块直接写在最外层。 但是如果你习惯按组编写测试,使用 describe
包裹相关测试用例更加友好。
如果你有多层级的测试,你也可以嵌套使用 describe
块:
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
有两种使用方式:
1. describe.each(table)(name, fn, timeout)
table
:数组类型
。table里的数据将按照顺序作为参数传给 fn 函数 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
:字符串类型
,代表测试套件的标题- 根据注入参数的位置生成相应的测试标题,参考 printf formatting:
%p
- pretty-format.%s
- String.%d
- Number.%i
- Integer.%f
- Floating point value.%j
- JSON.%o
- Object.%#
- Index of the test case.%%
- single percent sign (‘%’). 它不使用参数。
- 或者根据注入对象(
$variable
)上的属性生成相应的测试标题,如:- To inject nested object values use you can supply a keyPath i.e.
$variable.path.to.value
- 使用
$#
注入当前测试用例的顺序编号 - You cannot use
$variable
with theprintf
formatting except for%%
- To inject nested object values use you can supply a keyPath i.e.
- 根据注入参数的位置生成相应的测试标题,参考 printf formatting:
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
(毫秒)指定该测试套件每次运行的最长时间。 The default timeout is 5 seconds.
示例:
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
|
- One or more subsequent rows of data supplied as template literal expressions using
${value}
syntax.
- First row of variable name column headings separated with
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.- To inject nested object values use you can supply a keyPath i.e.
$variable.path.to.value
- To inject nested object values use you can supply a keyPath i.e.
fn
:Function
the suite of tests to be run, this is the function that will receive the test data object.- 可选地传入第三个参数
timeout
(毫秒)指定该测试套件每次运行的最长时间。 The default timeout is 5 seconds.
示例:
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)
运行某个指定的describe
块,你可以使用 describe.only
:
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)
或 fit(name, fn)
用多组数组测试某个特定的测试套件,使用 describe.only.each
describe.only.each
有两种使用方式:
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)
如果想跳过特定的 describe
块,可以使用describe.skip
:
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
内。
describe.skip.each(table)(name, fn)
别名: xdescribe.each(table)(name, fn)
或 fit(name, fn)
跳过用多组数组测试某个特定的测试套件,使用 describe.skip.each
describe.skip.each
有两种使用方式:
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)
test
是将运行测试的方法。 例如,现在有一个函数inchesOfRain()
,返回值应该为零。 你的整个测试可能是:
test('did not rain', () => {
expect(inchesOfRain()).toBe(0);
});
第一个参数是测试名称; 第二个参数是包含测试期望的函数。 可选地传入第三个参数 timeout(毫秒) 指定测试超时时间。 The default timeout is 5 seconds.
If a promise is returned from test
, Jest will wait for the promise to resolve before letting the test complete. 例如,假设fetchBeverageList()
返回一个应该解析为具有lemon
的列表的承诺。 You can test this with:
test('has lemon in it', () => {
return fetchBeverageList().then(list => {
expect(list).toContain('lemon');
});
});
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.
提示
Jest will also wait if you provide an argument to the test function, usually called done
. This could be handy when you want to test callbacks.
test.concurrent(name, fn, timeout)
别名: it.concurt(name, fn, timeout)
注意
test.concurrent
is considered experimental - see here for details on missing features and other issues.
Use test.concurrent
if you want the test to run concurrently.
The first argument is the test name; the second argument is an asynchronous function that contains the expectations to test. 可选地传入第三个参数 timeout(毫秒) 指定测试超时时间。 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);
});
提示
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.combut.each(table)(name, fn, timeout)
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.each
allows you to write the test once and pass data in, the tests are all run asynchronously.
test.concurrent.each
is available with two APIs:
1. test.concurrent.each(table)(name, fn, timeout)
table
:数组
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
:字符串
the title of the test block.- 根据注入参数的位置生成相应的测试标题,参考 printf formatting:
%p
- pretty-format.%s
- String.%d
- Number.%i
- Integer.%f
- Floating point value.%j
- JSON.%o
- Object.%#
- Index of the test case.%%
- single percent sign (‘%’). 它不使用参数。
- 根据注入参数的位置生成相应的测试标题,参考 printf formatting:
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
(毫秒)指定该测试套件每次运行的最长时间。 The default timeout is 5 seconds.
示例:
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
|
- One or more subsequent rows of data supplied as template literal expressions using
${value}
syntax.
- First row of variable name column headings separated with
name
:字符串
the title of the test, use$variable
to inject test data into the test title from the tagged template expressions.- To inject nested object values use you can supply a keyPath i.e.
$variable.path.to.value
- To inject nested object values use you can supply a keyPath i.e.
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
(毫秒)指定该测试套件每次运行的最长时间。 The default timeout is 5 seconds.
示例:
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.context.only.each(table)(name, fn)
Use test.concurrent.only.each
if you want to only run specific tests with different test data concurrently.
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.assurt.skip.each(table)(name, fn)
Use test.concurrent.skip.each
if you want to stop running a collection of asynchronous data driven tests.
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)
Use test.each
if you keep duplicating the same test with different data. test.each
allows you to write the test once and pass data in. test.each
allows you to write the test once and pass data in.
test.each
is available with two APIs:
1. test.each(table)(name, fn, timeout)
table
:数组
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
:字符串
the title of the test block.- 根据注入参数的位置生成相应的测试标题,参考 printf formatting:
%p
- pretty-format.%s
- String.%d
- Number.%i
- Integer.%f
- Floating point value.%j
- JSON.%o
- Object.%#
- Index of the test case.%%
- single percent sign (‘%’). 它不使用参数。
- 或者根据注入对象(
$variable
)上的属性生成相应的测试标题,如:- To inject nested object values use you can supply a keyPath i.e.
$variable.path.to.value
- 使用
$#
注入当前测试用例的顺序编号 - You cannot use
$variable
with theprintf
formatting except for%%
- To inject nested object values use you can supply a keyPath i.e.
- 根据注入参数的位置生成相应的测试标题,参考 printf formatting:
fn
:Function
the test to be run, this is the function that will receive the parameters in each row as function arguments.- 可选地传入第三个参数
timeout
(毫秒)指定该测试套件每次运行的最长时间。 The default timeout is 5 seconds.
示例:
test.each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
])('.add(%i, %i)', (a, b, expected) => {
expect(a + b).toBe(expected);
});
test.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}) => {
expect(a + b).toBe(expected);
});
2. test.each`table`(name, fn, timeout)
table
:Tagged Template Literal
- First row of variable name column headings separated with
|
- One or more subsequent rows of data supplied as template literal expressions using
${value}
syntax.
- First row of variable name column headings separated with
name
:字符串
the title of the test, use$variable
to inject test data into the test title from the tagged template expressions.- To inject nested object values use you can supply a keyPath i.e.
$variable.path.to.value
- To inject nested object values use you can supply a keyPath i.e.
fn
:Function
the test to be run, this is the function that will receive the test data object.- 可选地传入第三个参数
timeout
(毫秒)指定该测试套件每次运行的最长时间。 The default timeout is 5 seconds.
示例:
test.each`
a | b | expected
${1} | ${1} | ${2}
${1} | ${2} | ${3}
${2} | ${1} | ${3}
`('returns $expected when $a is added to $b', ({a, b, expected}) => {
expect(a + b).toBe(expected);
});
test.failing(name, fn, timeout)
别名: it.failing(name, fn, timeout)
备注
This is only available with the default jest-circus runner.
Use test.failing
when you are writing a test and expecting it to fail. These tests will behave the other way normal tests do. If failing
test will throw any errors then it will pass. If it does not throw it will fail.
提示
You can use this type of test i.e. when writing code in a BDD way. In that case the tests will not show up as failing until they pass. Then you can just remove the failing
modifier to make them pass.
It can also be a nice way to contribute failing tests to a project, even if you don’t know how to fix the bug.
示例:
test.failing('it is not equal', () => {
expect(5).toBe(6); // this test will pass
});
test.failing('it is equal', () => {
expect(10).toBe(10); // this test will fail
});
test.failing.each(name, fn, timeout)
Also under the alias: it.failing.each(table)(name, fn)
and it.failing.each`table`(name, fn)
备注
This is only available with the default jest-circus runner.
You can also run multiple tests at once by adding each
after failing
.
示例:
test.failing.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}) => {
expect(a + b).toBe(expected);
});
test.only.failing(name, fn, timeout)
别名: it.only.failing(name, fn, timeout)
和 fit.failing(name, fn, timeout)
备注
This is only available with the default jest-circus runner.
Use test.only.failing
if you want to only run a specific failing test.
test.skip.failing(name, fn, timeout)
别名: it.skip.failing(name, fn, timeout)
、xit.failing(name, fn, timeout)
、xtest.failing(name, fn, timeout)
备注
This is only available with the default jest-circus runner.
Use test.skip.failing
if you want to skip running a specific failing test.
1. test.only(name, fn, timeout)
Also under the aliases: it.only(name, fn, timeout)
, and fit(name, fn, timeout)
当你维护一个很大的代码库时,有时可能会发现一个由于某种原因被暂时破坏的测试。 If you want to skip running this test, but you don’t want to delete this code, you can use test.skip
to specify some tests to skip.
可选地传入第二个参数 timeout(毫秒) 指定函数执行超时时间。 The default timeout is 5 seconds.
比如说,你有这些测试︰
test.only('it is raining', () => {
expect(inchesOfRain()).toBeGreaterThan(0);
});
test('it is not snowing', () => {
expect(inchesOfSnow()).toBe(0);
});
Only the “it is raining” test will run in that test file, since it is run with test.only
.
Usually you wouldn’t check code using test.only
into source control - you would use it for debugging, and remove it once you have fixed the broken tests.
test.only.each(table)(name, fn)
Also under the aliases: it.only.each(table)(name, fn)
, fit.each(table)(name, fn)
, it.only.each`table`(name, fn)
and fit.each`table`(name, fn)
Use test.only.each
if you want to only run specific tests with different test data.
test.only.each
is available with two APIs:
test.only.each(table)(name, fn)
test.only.each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
])('.add(%i, %i)', (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.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}) => {
expect(a + b).toBe(expected);
});
test('will not be run', () => {
expect(1 / 0).toBe(Infinity);
});
test.skip(name, fn)
Also under the aliases: it.skip(name, fn)
, xit(name, fn)
, and xtest(name, fn)
当你维护一个很大的代码库时,有时可能会发现某个测试,会因某些原因而暂时失败。 如果你想跳过运行这个测试,但你不想删除这个代码,你可以使用test.skip
来指定一些要跳过的测试。
比如说,你有这些测试︰
test('it is raining', () => {
expect(inchesOfRain()).toBeGreaterThan(0);
});
test.skip('it is not snowing', () => {
expect(inchesOfSnow()).toBe(0);
});
When you are debugging a large test file, you will often only want to run a subset of tests. You can use .only
to specify which tests are the only ones you want to run in that test file.
您可以简单地注释测试,但使用 test.skip
通常会更好一点,因为它将保留缩进和语法高亮。
test.skip.each(table)(name, fn)
Also under the aliases: it.skip.each(table)(name, fn)
, xit.each(table)(name, fn)
, xtest.each(table)(name, fn)
, it.skip.each`table`(name, fn)
, xit.each`table`(name, fn)
and xtest.each`table`(name, fn)
Use test.skip.each
if you want to stop running a collection of data driven tests.
test.skip.each
is available with two APIs:
test.skip.each(table)(name, fn)
test.skip.each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
])('.add(%i, %i)', (a, b, expected) => {
expect(a + b).toBe(expected); // will not be run
});
test('will be run', () => {
expect(1 / 0).toBe(Infinity);
});
test.skip.each`table`(name, fn)
test.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}) => {
expect(a + b).toBe(expected); // will not be run
});
test('will be run', () => {
expect(1 / 0).toBe(Infinity);
});
test.todo(name)
Also under the alias: it.todo(name)
用 test.todo
来表示你计划要写这些测试。 Use test.todo
when you are planning on writing tests. These tests will be highlighted in the summary output at the end so you know how many tests you still need todo.
const add = (a, b) => a + b;
test.todo('add should be associative');
提示
test.todo
will throw an error if you pass it a test callback function. Use test.skip instead, if you already implemented the test, but do not want it to run.
TypeScript Usage
信息
The TypeScript examples from this page will only work as documented if you explicitly import Jest APIs:
import {expect, jest, test} from '@jest/globals';
Consult the Getting Started guide for details on how to setup Jest with TypeScript.
.each
The .each
modifier offers few different ways to define a table of the test cases. Some of the APIs have caveats related with the type inference of the arguments which are passed to describe
or test
callback functions. Let’s take a look at each of them.
备注
For simplicity test.each
is picked for the examples, but the type inference is identical in all cases where .each
modifier can be used: describe.each
, test.concurrent.only.each
, test.skip.each
, etc.
对象数组
The array of objects API is most verbose, but it makes the type inference a painless task. A table
can be inlined:
import {test} from '@jest/globals';
test.each([
{name: 'a', path: 'path/to/a', count: 1, write: true},
{name: 'b', path: 'path/to/b', count: 3},
])('inline table', ({name, path, count, write}) => {
// arguments are typed as expected, e.g. `write: boolean | undefined`
});
Or declared separately as a variable:
import {test} from '@jest/globals';
const table = [
{a: 1, b: 2, expected: 'three', extra: true},
{a: 3, b: 4, expected: 'seven', extra: false},
{a: 5, b: 6, expected: 'eleven'},
];
test.each(table)('table as a variable', ({a, b, expected, extra}) => {
// again everything is typed as expected, e.g. `extra: boolean | undefined`
});
二维数组
The array of arrays style will work smoothly with inlined tables:
import {test} from '@jest/globals';
test.each([
[1, 2, 'three', true],
[3, 4, 'seven', false],
[5, 6, 'eleven'],
])('inline table example', (a, b, expected, extra) => {
// arguments are typed as expected, e.g. `extra: boolean | undefined`
});
However, if a table is declared as a separate variable, it must be typed as an array of tuples for correct type inference (this is not needed only if all elements of a row are of the same type):
import {test} from '@jest/globals';
const table: Array<[number, number, string, boolean?]> = [
[1, 2, 'three', true],
[3, 4, 'seven', false],
[5, 6, 'eleven'],
];
test.each(table)('table as a variable example', (a, b, expected, extra) => {
// without the annotation types are incorrect, e.g. `a: number | string | boolean`
});
模板字符串
If all values are of the same type, the template literal API will type the arguments correctly:
import {test} from '@jest/globals';
test.each`
a | b | expected
${1} | ${2} | ${3}
${3} | ${4} | ${7}
${5} | ${6} | ${11}
`('template literal example', ({a, b, expected}) => {
// all arguments are of type `number`
});
Otherwise it will require a generic type argument:
import {test} from '@jest/globals';
test.each<{a: number; b: number; expected: string; extra?: boolean}>`
a | b | expected | extra
${1} | ${2} | ${'three'} | ${true}
${3} | ${4} | ${'seven'} | ${false}
${5} | ${6} | ${'eleven'}
`('template literal example', ({a, b, expected, extra}) => {
// without the generic argument in this case types would default to `unknown`
});