W Parku Jurajskim przed każdą inspekcją wybiegu trzeba przygotować sprzęt, a po inspekcji posprzątać. W testach jest dokładnie tak samo - funkcje setup i teardown pozwalają przygotować i posprzątać środowisko testowe.
beforeEach wykonuje się przed każdym testem w bloku describe. To idealne miejsce na tworzenie świeżych danych testowych.1describe('DinosaurEnclosure', () => {
2 let enclosure;
3 let rex;
4
5 beforeEach(() => {
6 // Przed każdym testem tworzymy nowy wybieg i dinozaura
7 enclosure = createEnclosure('Zone A', 5);
8 rex = createDinosaur('Rex', 'T-Rex', 'carnivore');
9 });
10
11 it('should add dinosaur to enclosure', () => {
12 enclosure.add(rex);
13 expect(enclosure.dinosaurs).toHaveLength(1);
14 expect(enclosure.dinosaurs[0].name).toBe('Rex');
15 });
16
17 it('should start with empty enclosure', () => {
18 // Dzięki beforeEach enclosure jest zawsze świeży!
19 expect(enclosure.dinosaurs).toHaveLength(0);
20 });
21
22 it('should track capacity', () => {
23 enclosure.add(rex);
24 expect(enclosure.remainingCapacity()).toBe(4);
25 });
26});afterEach wykonuje się po każdym teście. Służy do czyszczenia po testach - zamykania połączeń, resetowania stanu.1describe('SecuritySystem', () => {
2 let securitySystem;
3 let alarmLog;
4
5 beforeEach(() => {
6 securitySystem = new SecuritySystem();
7 alarmLog = [];
8 securitySystem.onAlarm((msg) => alarmLog.push(msg));
9 });
10
11 afterEach(() => {
12 // Resetuj system bezpieczeństwa po każdym teście
13 securitySystem.deactivate();
14 alarmLog = [];
15 console.log('System zresetowany po teście');
16 });
17
18 it('should detect breach', () => {
19 securitySystem.simulateBreach('Zone A');
20 expect(alarmLog).toContain('Naruszenie w Zone A');
21 });
22
23 it('should handle multiple alarms', () => {
24 securitySystem.simulateBreach('Zone A');
25 securitySystem.simulateBreach('Zone B');
26 expect(alarmLog).toHaveLength(2);
27 });
28});beforeAll uruchamia się raz przed wszystkimi testami w describe, a afterAll raz po wszystkich. Używaj ich dla kosztownych operacji.1describe('DatabaseTests', () => {
2 let database;
3
4 beforeAll(async () => {
5 // Kosztowna operacja - połączenie z bazą, tylko raz!
6 database = await connectToDatabase();
7 console.log('Baza danych podłączona');
8 });
9
10 afterAll(async () => {
11 // Zamknij połączenie po wszystkich testach
12 await database.close();
13 console.log('Baza danych odłączona');
14 });
15
16 beforeEach(async () => {
17 // Wyczyść dane przed każdym testem
18 await database.clear();
19 });
20
21 it('should save dinosaur', async () => {
22 await database.save({ name: 'Rex', species: 'T-Rex' });
23 const dinos = await database.findAll();
24 expect(dinos).toHaveLength(1);
25 });
26
27 it('should find by species', async () => {
28 await database.save({ name: 'Rex', species: 'T-Rex' });
29 await database.save({ name: 'Blue', species: 'Velociraptor' });
30
31 const raptors = await database.findBySpecies('Velociraptor');
32 expect(raptors).toHaveLength(1);
33 expect(raptors[0].name).toBe('Blue');
34 });
35});Ważne jest zrozumienie kolejności, w jakiej Jest wykonuje setup i teardown:
1describe('Kolejność wykonywania', () => {
2 beforeAll(() => console.log('1. beforeAll'));
3 afterAll(() => console.log('6. afterAll'));
4 beforeEach(() => console.log('2. beforeEach'));
5 afterEach(() => console.log('4. afterEach'));
6
7 it('test 1', () => console.log('3. test 1'));
8 it('test 2', () => console.log('3. test 2'));
9});
10
11// Wynik:
12// 1. beforeAll
13// 2. beforeEach
14// 3. test 1
15// 4. afterEach
16// 2. beforeEach
17// 3. test 2
18// 4. afterEach
19// 6. afterAllSetup i teardown działają hierarchicznie w zagnieżdżonych
describe:1describe('Park Jurajski', () => {
2 beforeEach(() => console.log('Setup: Park'));
3
4 describe('Strefa Drapieżników', () => {
5 beforeEach(() => console.log('Setup: Strefa Drapieżników'));
6
7 it('T-Rex jest w wybiegu', () => {
8 console.log('Test: T-Rex');
9 // Kolejność setup: Park -> Strefa Drapieżników
10 });
11 });
12
13 describe('Strefa Roślinożerców', () => {
14 beforeEach(() => console.log('Setup: Strefa Roślinożerców'));
15
16 it('Triceratops jest w wybiegu', () => {
17 console.log('Test: Triceratops');
18 // Kolejność setup: Park -> Strefa Roślinożerców
19 });
20 });
21});Zagnieżdżone
describe dziedziczą setup z nadrzędnych bloków. beforeEach z rodzica wykonuje się przed beforeEach dziecka.