Testing Exercises

Fill in the blanks to test your knowledge.

1

Write a basic Vitest test that checks 1 + 1 equals 2

('adds numbers', () => {

(1 + 1).toBe(2);

});

2

Group related tests inside a describe block

('Math utils', () => {

it('adds', () => expect(add(1,2)).toBe(3));

});

3

Assert that a function throws an error

expect(() => riskyFn()).();

4

Mock a module function and assert it was called once

const spy = vi.(myModule, 'fetchData');
await run();
expect(spy).toHaveBeenCalledTimes();
5

Run setup code before every test in a suite

(async () => {

await db.seed();

});

6

Assert that an array contains a specific item

expect(fruits).('banana');

7

Skip a flaky test without deleting it

it.('flaky test', () => { ... });

8

Assert that a mocked async function resolves with a specific value

vi.fn().mockResolvedValue('ok');

await expect(myFn()).resolves.('ok');

9

Restore all mocks after every test

(() => {

vi.restoreAllMocks();

});

10

Check the snapshot of a rendered React component

const { container } = render(<Button />);
expect(container).__();