PLAINFINCH learn by building

Testing cheat sheet

The 96 matchers and test terms you will use most, on one page. The everyday ones come first. Print it, or keep it open beside your editor.

Full reference

Everyday 46

.not
Flips any matcher so the test checks that something is not true.
.test.js file
A file name ending the runner looks for when it gathers tests to run.
accessible name
The label a screen reader announces for an element, used by role queries.
arrange, act, assert
A three part shape for tests: set things up, do one action, check the result.
assertion
A single check inside a test that compares a real result with an expected one.
async/await
Lets a test wait for a promise to finish before checking what came back.
beforeEach()
Runs setup code before every test so each one gets fresh data.
boundary value
An input right at the edge of a rule, where bugs like to hide.
coverage
A measure of how much of your code actually runs while the tests run.
describe()
Groups related tests under one heading so results read like an outline.
edge case
An unusual input, such as empty, zero or huge, that ordinary tests skip.
expect()
Wraps a value so you can check it with a matcher like toBe or toEqual.
Expected and Received
The two values a failure prints: what the test wanted and what it got.
failing test
A test that reports a mismatch, which tells you where to look next.
failure message
The text a runner shows when a test fails, naming the check and the values.
focused test
A test that checks one behavior, so its failure points at one cause.
getByLabelText()
Finds a form field by the label text a user would read next to it.
getByRole()
Finds an element by its role, like button or heading, the way users see it.
getByText()
Finds an element by the visible text inside it.
mock
A fake function that records how it was called so tests can check it.
mockReturnValue()
Sets what a mock function hands back each time it is called.
off-by-one
A bug where a count or limit is one too high or one too low.
red, green, refactor
Write a failing test, make it pass, then tidy the code while it stays green.
regression
A bug where something that used to work breaks after a change.
regression test
A test written for a fixed bug so the same bug cannot quietly return.
reproducing a bug
Making a bug happen on demand, ideally inside a failing test.
screen
The object holding Testing Library queries for the whole rendered page.
test
A small piece of code that runs your code and checks the result.
test isolation
Each test runs on its own, so order and other tests never matter.
test name
A description that says what behavior a test checks and what should happen.
test runner
The tool that finds your tests, runs them and reports what passed.
test suite
All the tests for a project, run together to show it still works.
test()
Defines one test with a name and a function that holds the checks.
test-first
Writing the test before the code, so you know what done looks like.
toBe()
Checks a value is exactly the same as another, best for numbers and strings.
toBeInTheDocument()
Checks an element exists on the rendered page.
toContain()
Checks an array holds an item or a string holds some text.
toEqual()
Checks two objects or arrays have the same contents, key by key.
toHaveBeenCalledWith()
Checks a mock function was called with the arguments you expect.
toHaveLength()
Checks how many items an array has or how long a string is.
toThrow()
Checks that calling a function throws an error, optionally with a message.
user.click()
Simulates a real user clicking an element, including focus and events.
user.type()
Simulates typing text into a field one key at a time.
userEvent.setup()
Creates a simulated user to drive clicks and typing in a test.
vi.fn()
Creates a mock function that records each call made to it.
Vitest
A fast JavaScript test runner with a Jest style way of writing tests.

Also useful 50

--coverage
A flag that makes the runner report which lines and branches ran.
.rejects
Unwraps a promise and checks the error it fails with.
.resolves
Unwraps a promise and checks the value it settles with.
afterEach()
Runs cleanup code after each test so the next one starts clean.
branch coverage
Tracks whether both sides of every if and else were run by tests.
bug report
A note describing what went wrong, which you turn into a failing test.
fake
A simple working stand-in for a real part, like an in-memory store.
fake Response
A hand-built object that looks like what fetch returns, for use in tests.
false pass
A test that goes green without really checking anything.
flaky test
A test that passes sometimes and fails other times with no code change.
getAllByRole()
Finds every element with a given role and returns them as a list.
it()
Another name for test(), handy when a name reads like a sentence.
logic in tests
Ifs and loops inside tests, which can hide bugs of their own.
mockRejectedValue()
Makes a mock return a promise that fails with the error you give.
mockResolvedValue()
Makes a mock return a promise that succeeds with the value you give.
mutation
Changing an object or array in place, which can surprise other code.
name option
A getByRole option that picks the element with a matching accessible name.
no assertion
A test with no expect inside, which passes but checks nothing.
over-mocking
Faking so much that a test only checks the fakes, not the real code.
query priority
The order to prefer queries: role and label first, test ids last.
queryBy
Queries that return null instead of throwing, used to check something is gone.
queryByRole()
Looks for an element by role and returns null when there is none.
refactoring
Reshaping code without changing what it does, with tests as a safety net.
role="alert"
Marks a message that screen readers announce at once, easy to find in tests.
shared state
Data reused across tests, which lets one test break another.
snapshot test
A test that saves output once and fails if it later changes.
specification
A set of test names that reads as a plain list of what the code does.
statement coverage
The share of code statements that ran at least once during tests.
test helper
A small function that builds data or does setup shared by several tests.
test-driven development
A habit of writing each test before the code that makes it pass.
test.each
Runs one test body over a table of inputs and expected results.
toBeCloseTo()
Checks a decimal number is near another, ignoring tiny rounding gaps.
toBeDisabled()
Checks a button or field cannot be used right now.
toBeEnabled()
Checks a button or field is ready for the user to use.
toBeVisible()
Checks an element can actually be seen, not hidden by styles.
toHaveBeenCalledTimes()
Checks how many times a mock function was called.
toHaveTextContent()
Checks the text inside an element matches what you expect.
toMatch()
Checks a string fits a pattern or contains a piece of text.
toMatchInlineSnapshot()
Saves a snapshot right inside the test file where you can read it.
toMatchObject()
Checks an object has the listed keys and values, ignoring any extras.
untested branch
A path through an if or switch that no test ever takes.
updating snapshots
Accepting a new snapshot on purpose after checking the change is right.
user.keyboard()
Simulates pressing keys such as Enter, Escape or Tab.
user.selectOptions()
Simulates picking one or more choices from a select menu.
vi.advanceTimersByTime()
Moves fake time forward so pending timers fire straight away.
vi.setSystemTime()
Pins the current date to a fixed moment so date code is predictable.
vi.spyOn()
Watches an existing method's calls, and can swap in a fake version.
vi.stubGlobal()
Replaces a global like fetch with your own version for the test.
vi.useFakeTimers()
Swaps real timers for fake ones you can move forward by hand.
watch mode
Reruns your tests every time you save a file.

PlainFinch Testing cheat sheet · plainfinch.com/testing/cheat-sheet/ · version 3.25.2