全局设定
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. 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'.
方法
-
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.only(name, fn, timeout)
-
test.only.each(table)(name, fn)
-
test.skip(name, fn)
-
test.skip.each(table)(name, fn)
-
test.todo(name)
Reference
afterAll(fn, timeout)
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.
Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait before aborting. Note: The default timeout is 5 seconds. Note: The default timeout is 5 seconds.
例如:
例如:
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 是在 describe 块的内部,它运行在 describe 块结尾。
如果你想要在每个测试后运行一些清理,请使用 afterEach。
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.
afterEach(fn, timeout)
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.
Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait before aborting. Note: The default timeout is 5 seconds. Note: The default timeout is 5 seconds.
例如:
例如:
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 是在 describe 块的内部,它运行在 describe 块结尾。
如果你想要一些清理只运行一次,在所有测试运行后,使用 afterAll 。
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.
beforeAll(fn, timeout)
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.
Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait before aborting. Note: The default timeout is 5 seconds. Note: The default timeout is 5 seconds.
例如:
例如:
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); });}); // Jest will wait for this promise to resolve before running tests. 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(); });});
在这里 beforeAll 确保数据库设置运行测试之前。 If setup was synchronous, you could do this without beforeAll. 关键是Jest将等待承诺解决,所以您也可以进行异步设置。 关键是Jest将等待承诺解决,所以您也可以进行异步设置。
如果要在每次测试之前运行某些操作,而不是在任何测试运行之前运行,请改用beforeEach。
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.
beforeEach(fn, timeout)
Runs a function before each of the tests in this file runs. 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.
Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait before aborting. Note: The default timeout is 5 seconds. Note: The default timeout is 5 seconds.
例如:
例如:
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在describe块内,则它将在 describe 块中为每个测试运行。
如果只需要运行一些设置代码,在任何测试运行之前,请使用beforeAll。
describe(name, fn) creates a block that groups together several related tests. 例如,如果您有一个myBeverage对象,该对象应该是美味但不酸,您可以通过以下方式测试:
describe(name, fn)
describe(name, fn) creates a block that groups together several related tests. 例如,如果您有一个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(); });});
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. But this can be handy if you prefer your tests to be organized into groups.
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')).toThrowError(CustomError); }); test('with extra whitespace throws CustomError', () => { expect(() => binaryStringToNumber(' 100')).toThrowError(CustomError); }); }); describe('given a valid binary string', () => { test('returns the correct number', () => { expect(binaryStringToNumber('100')).toBe(4); }); });});
