Matchery to metody, które pozwalają sprawdzać wartości na różne sposoby. W kontekście Parku Jurajskiego - to różne typy sensorów, z których każdy wykrywa inny rodzaj zagrożenia.
toBe używa ścisłego porównania. Sprawdza identyczność - idealny dla typów prymitywnych.1test('toBe - porównanie prymitywów', () => {
2 const dinoCount = 15;
3 expect(dinoCount).toBe(15); // liczba
4
5 const species = 'T-Rex';
6 expect(species).toBe('T-Rex'); // string
7
8 const isAlive = true;
9 expect(isAlive).toBe(true); // boolean
10});toEqual sprawdza równość strukturalną obiektów. Porównuje zawartość, nie referencje.1test('toEqual - porównanie obiektów', () => {
2 const dino1 = { name: 'Rex', species: 'T-Rex' };
3 const dino2 = { name: 'Rex', species: 'T-Rex' };
4
5 // toBe FAIL - różne referencje (obiekty w pamięci)
6 // expect(dino1).toBe(dino2); // To nie zadziała!
7
8 // toEqual PASS - ta sama zawartość
9 expect(dino1).toEqual(dino2); // To zadziała!
10
11 // Działa też z zagnieżdżonymi obiektami
12 const enclosure = {
13 name: 'Zone A',
14 dinosaurs: [
15 { name: 'Rex', diet: 'carnivore' }
16 ]
17 };
18
19 expect(enclosure).toEqual({
20 name: 'Zone A',
21 dinosaurs: [
22 { name: 'Rex', diet: 'carnivore' }
23 ]
24 });
25});1test('toContain - sprawdzanie zawartości', () => {
2 const dinosaurs = ['T-Rex', 'Velociraptor', 'Triceratops'];
3
4 expect(dinosaurs).toContain('T-Rex');
5 expect(dinosaurs).toContain('Velociraptor');
6
7 // Dla stringów
8 const report = 'Dinozaur T-Rex w strefie A';
9 expect(report).toContain('T-Rex');
10 expect(report).toContain('strefie A');
11});1test('toHaveLength - sprawdzanie ilości', () => {
2 const carnivores = ['T-Rex', 'Velociraptor', 'Spinosaurus'];
3 expect(carnivores).toHaveLength(3);
4
5 const emptyEnclosure = [];
6 expect(emptyEnclosure).toHaveLength(0);
7
8 // Działa też ze stringami
9 const code = 'DINO-001';
10 expect(code).toHaveLength(8);
11});1test('sprawdzanie prawdziwości', () => {
2 // Truthy values
3 expect(1).toBeTruthy();
4 expect('text').toBeTruthy();
5 expect([]).toBeTruthy(); // pusta tablica jest truthy!
6 expect({}).toBeTruthy(); // pusty obiekt jest truthy!
7
8 // Falsy values
9 expect(0).toBeFalsy();
10 expect('').toBeFalsy();
11 expect(null).toBeFalsy();
12 expect(undefined).toBeFalsy();
13 expect(false).toBeFalsy();
14});1test('sprawdzanie null i undefined', () => {
2 const activeDino = getDinosaur('Rex-001');
3 const extinctDino = getDinosaur('Unknown');
4
5 expect(activeDino).toBeDefined();
6 expect(activeDino).not.toBeNull();
7
8 expect(extinctDino).toBeNull();
9 expect(extinctDino).toBeDefined(); // null jest defined!
10
11 let uninitializedDino;
12 expect(uninitializedDino).toBeUndefined();
13});1test('porównania liczbowe', () => {
2 const health = 85;
3
4 expect(health).toBeGreaterThan(80);
5 expect(health).toBeGreaterThanOrEqual(85);
6 expect(health).toBeLessThan(100);
7 expect(health).toBeLessThanOrEqual(85);
8
9 // Dla liczb zmiennoprzecinkowych - NIE używaj toBe!
10 expect(0.1 + 0.2).toBeCloseTo(0.3);
11});toThrow sprawdza, czy funkcja rzuca wyjątek. UWAGA: musisz opakować wywołanie w funkcję!1function releaseDinosaur(dinosaur, securityLevel) {
2 if (securityLevel < 5) {
3 throw new Error('Poziom bezpieczeństwa za niski!');
4 }
5 if (dinosaur.species === 'T-Rex' && securityLevel < 9) {
6 throw new Error('T-Rex wymaga poziomu bezpieczeństwa 9!');
7 }
8 return { released: true };
9}
10
11test('toThrow - testowanie wyjątków', () => {
12 const rex = { name: 'Rex', species: 'T-Rex' };
13
14 // POPRAWNIE - opakuj w funkcję strzałkową
15 expect(() => releaseDinosaur(rex, 3)).toThrow();
16 expect(() => releaseDinosaur(rex, 3)).toThrow('Poziom bezpieczeństwa za niski!');
17 expect(() => releaseDinosaur(rex, 7)).toThrow('T-Rex wymaga poziomu bezpieczeństwa 9!');
18
19 // Sprawdź, że NIE rzuca wyjątku
20 expect(() => releaseDinosaur(rex, 9)).not.toThrow();
21});Każdy matcher można zanegować za pomocą
.not:1test('modyfikator not', () => {
2 const dinosaurs = ['T-Rex', 'Triceratops'];
3
4 expect(dinosaurs).not.toContain('Stegosaurus');
5 expect(dinosaurs.length).not.toBe(0);
6 expect(dinosaurs).not.toEqual([]);
7
8 const health = 85;
9 expect(health).not.toBeLessThan(50);
10});