Skip to main content
Version: 25.x

Asamblare și dezasamblare

Adesea, în timp ce scrieți teste aveți nevoie de configurări, care trebuie să se ruleze înainte de teste, şi diverse curățări, care trebuie să se ruleze după execuția testelor. Jest oferă funcţii utilitare pentru aceste situații.

Repetarea configurării mai multor teste

Dacă aveţi ceva rulați ceva cod în mod repetat pentru mai multe teste, puteţi utiliza beforeEach şi afterEach.

De exemplu, să prespunem că mai multe teste interacţionează cu o bază de date a oraşelor. Avem o metodă initializeCityDatabase() care trebuie să fie apelată înainte de fiecare dintre aceste teste, si o metodă clearCityDatabase() care trebuie să fie apelată după fiecare dintre aceste teste. Aceasta se poate realiza cu:

beforeEach(() => {
initializeCityDatabase();
});

afterEach(() => {
clearCityDatabase();
});

test('city database has Vienna', () => {
expect(isCity('Vienna')).toBeTruthy();
});

test('city database has San Juan', () => {
expect(isCity('San Juan')).toBeTruthy();
});

beforeEach şi afterEach pot rula cod asincron în acelaşi mod cum testele pot rula cod asincron - fie primesc un parametru done sau returnează o promisiune. De exemplu, în cazul în care initializeCityDatabase() returnează o promisiune care este rezolvată atunci când baza de date s-a inițializat, ar trebui să returnăm acea promisiune:

beforeEach(() => {
return initializeCityDatabase();
});

Configurare unică

În unele cazuri, trebuie să configurăm o singură dată, la începutul unui fişier. This can be especially bothersome when the setup is asynchronous, so you can't do it inline. Jest oferă beforeAll şi afterAll pentru aceste situaţii.

De exemplu, dacă initializeCityDatabase şi clearCityDatabase returnează promisiuni, şi baza de date cu oraşe ar putea fi refolosită între teste, am putea schimba codul nostru de testare în felul următor:

beforeAll(() => {
return initializeCityDatabase();
});

afterAll(() => {
return clearCityDatabase();
});

test('city database has Vienna', () => {
expect(isCity('Vienna')).toBeTruthy();
});

test('city database has San Juan', () => {
expect(isCity('San Juan')).toBeTruthy();
});

Delimitare

The top level before* and after* hooks apply to every test in a file. The hooks declared inside a describe block apply only to the tests within that describe block.

For example, let's say we had not just a city database, but also a food database. We could do different setup for different tests:

// Applies to all tests in this file
beforeEach(() => {
return initializeCityDatabase();
});

test('city database has Vienna', () => {
expect(isCity('Vienna')).toBeTruthy();
});

test('city database has San Juan', () => {
expect(isCity('San Juan')).toBeTruthy();
});

describe('matching cities to foods', () => {
// Applies only to tests in this describe block
beforeEach(() => {
return initializeFoodDatabase();
});

test('Vienna <3 veal', () => {
expect(isValidCityFoodPair('Vienna', 'Wiener Schnitzel')).toBe(true);
});

test('San Juan <3 plantains', () => {
expect(isValidCityFoodPair('San Juan', 'Mofongo')).toBe(true);
});
});

Note that the top-level beforeEach is executed before the beforeEach inside the describe block. It may help to illustrate the order of execution of all hooks.

beforeAll(() => console.log('1 - beforeAll'));
afterAll(() => console.log('1 - afterAll'));
beforeEach(() => console.log('1 - beforeEach'));
afterEach(() => console.log('1 - afterEach'));
test('', () => console.log('1 - test'));
describe('Scoped / Nested block', () => {
beforeAll(() => console.log('2 - beforeAll'));
afterAll(() => console.log('2 - afterAll'));
beforeEach(() => console.log('2 - beforeEach'));
afterEach(() => console.log('2 - afterEach'));
test('', () => console.log('2 - test'));
});

// 1 - beforeAll
// 1 - beforeEach
// 1 - test
// 1 - afterEach
// 2 - beforeAll
// 1 - beforeEach
// 2 - beforeEach
// 2 - test
// 2 - afterEach
// 1 - afterEach
// 2 - afterAll
// 1 - afterAll

Order of execution of describe and test blocks

Jest executes all describe handlers in a test file before it executes any of the actual tests. This is another reason to do setup and teardown inside before* and after* handlers rather than inside the describe blocks. Once the describe blocks are complete, by default Jest runs all the tests serially in the order they were encountered in the collection phase, waiting for each to finish and be tidied up before moving on.

Consider the following illustrative test file and output:

describe('outer', () => {
console.log('describe outer-a');

describe('describe inner 1', () => {
console.log('describe inner 1');
test('test 1', () => {
console.log('test for describe inner 1');
expect(true).toBe(true);
});
});

console.log('describe outer-b');

test('test 1', () => {
console.log('test for describe outer');
expect(true).toBe(true);
});

describe('describe inner 2', () => {
console.log('describe inner 2');
test('test for describe inner 2', () => {
console.log('test for describe inner 2');
expect(false).toBe(false);
});
});

console.log('describe outer-c');
});

// describe outer-a
// describe inner 1
// describe outer-b
// describe inner 2
// describe outer-c
// test for describe inner 1
// test for describe outer
// test for describe inner 2

Sfaturi generale

În cazul în care un test eșuează, unul dintre primele lucruri pe care ar trebui să le verificaţi este dacă dacă testul cade si atunci când rulează doar el. To run only one test with Jest, temporarily change that test command to a test.only:

test.only('this will be the only test that runs', () => {
expect(true).toBe(false);
});

test('this test will not run', () => {
expect('A').toBe('A');
});

Dacă aveţi un test care cade adesea atunci când face parte dintr-o suită mai mare de teste, dar nu cade atunci când rulează singur, cu siguranță ceva de la un test precedent interferează cu acesta. Puteţi rezolva adesea acest lucru curățarea stării comune cu beforeEach. If you're not sure whether some shared state is being modified, you can also try a beforeEach that logs data.