We use cookies to enhance your experience on the site
CodeWorlds

Jest Framework - Basics

Jest is the most popular framework for testing JavaScript and TypeScript, created by Meta (formerly Facebook). In our Jurassic Park, Jest will be the main tool for verifying security systems.

Installation and Configuration

1# Installing Jest
2npm install --save-dev jest
3
4# For TypeScript
5npm install --save-dev jest ts-jest @types/jest
6
7# Initializing configuration
8npx jest --init

Test Structure

Every test in Jest consists of three key elements:

describe
,
it
(or
test
), and
expect
.

describe - Grouping Tests

describe
is a block that groups related tests. Think of it as an enclosure in the park - it groups dinosaurs of the same species.

1describe('DinosaurEnclosure', () => {
2  // All tests related to the dinosaur enclosure
3});

it / test - Individual Test Case

it
and
test
are interchangeable - both define a single test. Convention:
it
reads like a sentence in English.

1describe('DinosaurEnclosure', () => {
2  it('should have a fence', () => {
3    // assertion
4  });
5
6  test('fence is electrified', () => {
7    // assertion
8  });
9});

expect - Assertions

expect
is the heart of every test. It compares the actual result with the expected one.

1describe('DinosaurEnclosure', () => {
2  it('should have correct capacity', () => {
3    const enclosure = createEnclosure('T-Rex Zone', 5);
4    expect(enclosure.capacity).toBe(5);
5  });
6
7  it('should start empty', () => {
8    const enclosure = createEnclosure('Raptor Pen', 10);
9    expect(enclosure.dinosaurs).toEqual([]);
10    expect(enclosure.dinosaurs.length).toBe(0);
11  });
12});

Complete Example

Let's see a full example of testing a dinosaur management module:

1// dinosaur.js - production code
2function createDinosaur(name, species, diet) {
3  return {
4    name,
5    species,
6    diet,
7    health: 100,
8    isAlive: true,
9  };
10}
11
12function feedDinosaur(dinosaur, food) {
13  if (dinosaur.diet === 'carnivore' && food === 'meat') {
14    dinosaur.health = Math.min(100, dinosaur.health + 20);
15    return { success: true, message: 'Dinosaur fed!' };
16  }
17  if (dinosaur.diet === 'herbivore' && food === 'plants') {
18    dinosaur.health = Math.min(100, dinosaur.health + 15);
19    return { success: true, message: 'Dinosaur fed!' };
20  }
21  return { success: false, message: 'Wrong food!' };
22}
23
24// dinosaur.test.js - tests
25describe('Dinosaur Management', () => {
26  describe('createDinosaur', () => {
27    it('should create a dinosaur with correct properties', () => {
28      const rex = createDinosaur('Rex', 'T-Rex', 'carnivore');
29
30      expect(rex.name).toBe('Rex');
31      expect(rex.species).toBe('T-Rex');
32      expect(rex.diet).toBe('carnivore');
33      expect(rex.health).toBe(100);
34      expect(rex.isAlive).toBe(true);
35    });
36  });
37
38  describe('feedDinosaur', () => {
39    it('should feed carnivore with meat', () => {
40      const rex = createDinosaur('Rex', 'T-Rex', 'carnivore');
41      rex.health = 70;
42
43      const result = feedDinosaur(rex, 'meat');
44
45      expect(result.success).toBe(true);
46      expect(rex.health).toBe(90);
47    });
48
49    it('should reject wrong food for carnivore', () => {
50      const rex = createDinosaur('Rex', 'T-Rex', 'carnivore');
51
52      const result = feedDinosaur(rex, 'plants');
53
54      expect(result.success).toBe(false);
55    });
56  });
57});

Running Tests

1# Run all tests
2npx jest
3
4# Run a specific file
5npx jest dinosaur.test.js
6
7# Run in watch mode (reacts to changes)
8npx jest --watch
9
10# Run with code coverage reporting
11npx jest --coverage

AAA Pattern (Arrange-Act-Assert)

Every good test should be written according to the AAA pattern:

1it('should increase health when fed correctly', () => {
2  // Arrange - create the needed objects
3  const dino = createDinosaur('Brachio', 'Brachiosaurus', 'herbivore');
4  dino.health = 60;
5
6  // Act - execute the tested operation
7  const result = feedDinosaur(dino, 'plants');
8
9  // Assert - verify the result
10  expect(result.success).toBe(true);
11  expect(dino.health).toBe(75);
12});
Go to CodeWorlds