Testing Exercises
Fill in the blanks to test your knowledge.
Write a basic Vitest test that checks 1 + 1 equals 2
('adds numbers', () => {
(1 + 1).toBe(2);
});
Group related tests inside a describe block
('Math utils', () => {
it('adds', () => expect(add(1,2)).toBe(3));
});
Assert that a function throws an error
expect(() => riskyFn()).();
Mock a module function and assert it was called once
Run setup code before every test in a suite
(async () => {
await db.seed();
});
Assert that an array contains a specific item
expect(fruits).('banana');
Skip a flaky test without deleting it
it.('flaky test', () => { ... });
Assert that a mocked async function resolves with a specific value
vi.fn().mockResolvedValue('ok');
await expect(myFn()).resolves.('ok');
Restore all mocks after every test
(() => {
vi.restoreAllMocks();
});
Check the snapshot of a rendered React component