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.
方法
afterAll(fn, timeout)
afterEach(fn, timeout)
beforeAll(fn, timeout)
beforeEach(fn, timeout)
describe(name, fn)
describe.only(name, fn)
describe.skip(name, fn)
test(name, fn, timeout)
test.only(name, fn, timeout)
test.skip(name, fn)
参考
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.
这通常是有用的如果你想要清理一些在测试之间共享的全局设置状态。
例如:
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)
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.
这通常是有用的如果你想要清理一些在每个测试之间共享的全局设置状态。
例如:
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)
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.
如果要设置将被许多测试使用的一些全局状态,这通常很有用。
例如:
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
确保数据库设置运行测试之前。 If setup was synchronous, you could do this without beforeAll
. 关键是Jest将等待承诺解决,所以您也可以进行异步设置。
如果beforeAll
在describe
块内,则它将在 describe 块的开头运行。
如果要在每次测试之前运行某些操作,而不是在任何测试运行之前运行,请改用beforeEach
。
beforeEach(fn, timeout)
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.
Optionally, you can provide a timeout
(in milliseconds) for specifying how long to wait before aborting. 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
确保每个测试重置数据库。
如果beforeEach
在describe
块内,则它将在 describe 块中为每个测试运行。
如果只需要运行一些设置代码,在任何测试运行之前,请使用beforeAll
。
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.
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);
});
});
});
describe.only(name, fn)
还有别名:fdescribe(name, fn)
如果你只想运行一个描述块,你可以使用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.skip(name, fn)
还有别名:xdescribe(name, fn)
如果不想运行特定的描述块,可以使用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
});
test(name, fn, timeout)
还有别名:it(name, fn, timeout)
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);
});
第一个参数是测试名称; 第二个参数是包含测试期望的函数。 The third argument (optional) is timeout
(in milliseconds) for specifying how long to wait before aborting. Note: The default timeout is 5 seconds.
Note: If a promise is returned from
test
, Jest will wait for the promise to resolve before letting the test complete. Jest will also wait if you provide an argument to the test function, usually calleddone
. This could be handy when you want to test callbacks. See how to test async code here.
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
的调用将立即返回,测试还没有完成,直到承诺也解决。
test.only(name, fn, timeout)
还有别名: it.only(name, fn)
或 fit(name, fn)
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.
Optionally, you can provide a timeout
(in milliseconds) for specifying how long to wait before aborting. Note: 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.skip(name, fn)
还有别名:it.skip(name, fn)
或 xit(name, fn)
或 xtest(name, fn)
当你维护一个很大的代码库时,有时可能会发现一个由于某种原因被暂时破坏的测试。 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.
比如说,你有这些测试︰
test('it is raining', () => {
expect(inchesOfRain()).toBeGreaterThan(0);
});
test.skip('it is not snowing', () => {
expect(inchesOfSnow()).toBe(0);
});
只有“it is raining”测试才会运行,因为另一个测试是使用test.skip
。
You could comment the test out, but it's often a bit nicer to use test.skip
because it will maintain indentation and syntax highlighting.