One page

Testing cheat sheet

What to keep at hand. If a line is not clear, go back to the matching lesson in the course.

Open the course: Testing: unit, integration, e2e

Vitest: the essentials

WhatHow
Group and testdescribe("cart", () => { it("sums", () => {}) })
Equality of values and objectsexpect(a).toBe(1); expect(obj).toEqual({ a: 1 })
Partial matchexpect(res).toMatchObject({ status: 'ok' })
Exceptionexpect(() => fn()).toThrow(RangeError)
Promise rejectedawait expect(p).rejects.toThrow('not found')
Table testit.each(cases)("$input", ({ input }) => {})
Run in CIvitest run

Test doubles in Vitest

WhatHow
Empty function with call historyconst fn = vi.fn()
Canned answervi.fn().mockResolvedValue(user)
Spy on a methodvi.spyOn(obj, 'send')
Check a callexpect(fn).toHaveBeenCalledWith('a', 1)
Fake timersvi.useFakeTimers(); vi.advanceTimersByTime(300)
Pin the datevi.setSystemTime(new Date('2026-01-01'))
Network via MSWsetupServer(http.get(url, () => HttpResponse.json({})))

Integration tests

WhatHow
Postgres in Testcontainersawait new PostgreSqlContainer('postgres:17').start()
Connection stringcontainer.getConnectionUri()
Test isolationBEGIN in beforeEach, ROLLBACK in afterEach
API requestawait request(app).post('/users').send(body).expect(201)
Any number in a fieldexpect(res.body.id).toEqual(expect.any(Number))

Playwright

WhatHow
Run and UI modepnpm exec playwright test --ui
Role locatorpage.getByRole('button', { name: 'Save' })
Field by labelpage.getByLabel('Email').fill('[email protected]')
Web-first assertionawait expect(locator).toBeVisible()
Custom fixturetest.extend<{ user: User }>({ user: async ({}, use) => {} })
Replacing the networkawait page.route('**/api/pay', (r) => r.fulfill({ json: {} }))
Trace for retriesuse: { trace: 'on-first-retry' }