Глобальные значения
В скриптах тестирования, Jest создает эти функции и объекты в глобальной области видимости. Вам не нужно ничего импортировать или подключать к скрипту для работы с этими функциями и объектами. However, if you prefer explicit imports, you can do import {describe, expect, test} from '@jest/globals'.
Примеры TypeScript с этой страницы будут работать только в том случае, если вы явно импортируете Jest API:
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)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)
Выполняет функцию после всех тестов в этом файле. Если функция возвращает промис или является генератором, Jest ждет пока промис разрешится, а затем запускает тесты.
Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait before aborting. 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();
});
});
Here the afterAll ensures that cleanUpDatabase is called after all tests run.
If afterAll is inside a describe block, it runs at the end of the describe block.
If you want to run some cleanup after every test instead of after all tests, use afterEach instead.
afterEach(fn, timeout)
Выполняет функцию после каждого теста в файле. Если функция возвращает промис или является генератором, Jest ждет пока промис разрешится, а затем запускает тесты.
Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait before aborting. 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();
});
});
Here the afterEach ensures that cleanUpDatabase is called after each test runs.
If afterEach is inside a describe block, it only runs after the tests that are inside this describe block.
If you want to run some cleanup just once, after all of the tests run, use afterAll instead.
beforeAll(fn, timeout)
Выполняет функцию перед запуском каждого теста в этом файле. Если функция возвращает промис или является генератором, Jest ждет пока промис разрешится, а затем запускает тест.
Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait before aborting. 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);
});
});
Here the beforeAll ensures that the database is set up before tests run. If setup was synchronous, you could do this without beforeAll. Ключевым является то, что Jest будет ждать разрешения промиса, поэтому вы можете установить асинхронно.
If beforeAll is inside a describe block, it runs at the beginning of the describe block.
If you want to run something before every test instead of before any test runs, use beforeEach instead.