全体
テストファイルでは、Jest はそれぞれのメソッドとオブジェクトをグローバル環境に配置します。 それらを使用するために require または import する必要はありません。 ただし、明示的にインポートしたい場合は、 '@jest/globals' から {describe, expect, test} をインポートすることができます
。
このページの TypeScript の例は、次のように Jest の API を明示的にインポートした場合にのみ動作します。
import {expect, jest, test} from '@jest/globals';
TypeScript で Jest をセットアップする方法の詳細については、Getting Started ガイドを参照してください。
メソッド
- リファレンス
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)
このファイル内のすべてのテストが完了した後に、関数を実行します。 関数がpromiseを返す、またはジェネレータ関数である場合、Jestはそのpromiseが解決されるのを待ちます。
オプションとして、timeout
(ミリ秒) を指定して、中断前にどのくらい待機するかを指定することができます。 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
ブロック内に記述された場合は、 そのブロックの最後にafterAll
関数が実行されます。
全テスト完了後ではなく、個々のテストの完了後にクリーンアップ処理を行いたい場合は、 afterEach
関数を使用して下さい。
異なるテストデータを使用して特定のテストのみを実行する場合は、 test.only.each
を使用してください。
afterEach(fn, timeout)
このファイル内の各テストが完了するたびに、関数を実行します。 関数がpromiseを返す、またはジェネレータ関数である場合、Jestはそのpromiseが解決されるのを待ちます。
オプションとして、timeout
(ミリ秒) を指定して、中断前にどのくらい待機するかを指定することができます。 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
ブロック内に記述された場合は、 afterEach
関数が記述されたブロックのみ、最後に実行されます。
全テストが完了した後に一度だけ何らかのクリーンアップを実行する場合は、 afterAll
関数を使用して下さい。
全テストが完了した後に一度だけ何らかのクリーンアップを実行する場合は、 afterAll
関数を使用して下さい。