Modern QA2026Jest: Unit and Integration Testing — tiles
Log inJoin
11 / 80 · 12 Programming for QA · JavaScript and TypeScript for QA← prev⊞ allnext →☰ Read as one page

2.3Jest: Unit and Integration Testing

Jest is the most popular JavaScript test framework. It is commonly used for unit tests, API tests, and component tests.

// user.test.ts
describe("User API", () => {
    let authToken: string;

    beforeAll(async () => {
        const response = await fetch(`${BASE_URL}/auth/login`, {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify({ email: "test@example.com", password: "pass123" })
        });
        const data = await response.json();
        authToken = data.access_token;
    });

    test("GET /users/me returns current user", async () => {
        const response = await fetch(`${BASE_URL}/users/me`, {
            headers: { Authorization: `Bearer ${authToken}` }
        });
        expect(response.status).toBe(200);

        const user = await response.json();
        expect(user).toHaveProperty("id");
        expect(user).toHaveProperty("email", "test@example.com");
        expect(user).not.toHaveProperty("password");
    });

    test("GET /users/me without auth returns 401", async () => {
        const response = await fetch(`${BASE_URL}/users/me`);
        expect(response.status).toBe(401);
    });
});

Jest Matchers for QA

// Equality
expect(status).toBe(200);                    // strict equality
expect(user).toEqual({ id: 1, name: "Alice" }); // deep equality

// Truthiness
expect(token).toBeDefined();
expect(error).toBeNull();
expect(items.length).toBeTruthy();

// Numbers
expect(responseTime).toBeLessThan(1000);
expect(items.length).toBeGreaterThanOrEqual(1);

// Strings
expect(message).toMatch(/success/i);
expect(url).toContain("/api/v1");

// Arrays and Objects
expect(roles).toContain("admin");
expect(user).toHaveProperty("email");
expect(errors).toHaveLength(0);