In a Jest 30 test file, when do describe callbacks run, and how do test.only, test.skip, test.todo and test.each change what runs?
answer
- collect first, run later
- registration must be synchronous
- focus stops at the file
- a placeholder takes no body
- one body, many rows
basics
~20 sJest runs every describe callback first, synchronously, to collect tests, then runs them. test.only focuses tests within that file, test.skip marks one skipped, test.todo records a body-less placeholder, and test.each generates a test per data row.
solid answer
~50 sA Jest test file runs in two phases. In the **collection** phase Jest executes every `describe` callback top to bottom, registering tests and hooks; only then does it run the tests, in the order they were collected. A `describe` callback must be synchronous: returning a promise fails the file with *Returning a Promise from "describe" is not supported*. Code written directly in a `describe` body therefore runs once, before any test, so per-test setup belongs in `beforeEach`. `test` (alias `it`) registers one test, with an optional timeout that defaults to 5 seconds. `test.only` and `describe.only` restrict the run to focused blocks **in that file**; other files still run. `test.skip` keeps the code but reports it skipped, `test.todo('title')` takes no callback, and `test.each(table)(title, fn)` registers one test per row, with `%s`, `%d` or `$field` in the title.
code
javascript · 24 linesconst { parsePrice } = require('./parsePrice');
describe('parsePrice', () => {
test.each([
['12', 12],
['0.5', 0.5],
])('parses %s as %d', (input, expected) => {
expect(parsePrice(input)).toBe(expected);
});
test.each`
input | message
${'abc'} | ${'invalid price'}
${'12,50'} | ${'invalid price'}
`('rejects "$input"', ({ input, message }) => {
expect(() => parsePrice(input)).toThrow(message);
});
test.skip('accepts a thousands separator', () => {
expect(parsePrice('1 200')).toBe(1200);
});
test.todo('strips a leading currency symbol');
});go deeper
Know that test and it are the same function, that describe groups tests, and what .only, .skip, .todo and .each do.
Explain the collection phase: describe bodies run first and synchronously, so setup there runs once and is shared, and focus with .only stays inside one file.
Trace order-dependent failures to state built in describe bodies, and keep committed .only markers and silent skips out of the suite, watching the skipped and todo counts.
Decide how the team tracks skipped and todo tests over time so they stay visible debt with an owner rather than permanently disabled coverage.
## The two building blocks Jest installs its structure functions as globals in every test file; they can also be imported explicitly from `@jest/globals`. - **`test(name, fn, timeout)`**, also available as **`it`**, registers one test. The optional third argument is a timeout in milliseconds; the default is 5 seconds. - **`describe(name, fn)`** groups related tests. It is optional, since tests can sit at the top level, and it nests. A test's full name joins the titles, so a test titled `parses 12 as 12` inside `describe('parsePrice')` is reported as `parsePrice parses 12 as 12`. ## Collection first, execution second A test file runs in two phases: 1. **Collection.** Jest executes every `describe` callback, outer to inner and top to bottom, and records each `test` and hook it meets. No test body runs yet. 2. **Execution.** By default Jest runs the collected tests serially, in the order they were encountered, finishing each before starting the next. That ordering has consequences worth knowing: - Code placed directly inside a `describe` body runs **once, during collection**, before any hook or test. An object created there is shared by every test in the block. The docs recommend doing setup and teardown in `beforeEach` and its siblings instead; the hook model itself is covered with Jest setup. - A `describe` callback must register its tests **synchronously**. An `async` callback returns a promise, and Jest fails the file with *Returning a Promise from "describe" is not supported. Tests must be defined synchronously.* - Calling `test` with only a title fails with *Missing second argument. It must be a callback function*, and the message suggests `test.todo` for a placeholder. - A `test` or `describe` declared inside a running test is rejected: tests cannot be nested. ## Focusing and skipping | Form | Aliases | Effect | |---|---|---| | `test.only` | `it.only`, `fit` | only the focused tests in **this file** run; the rest of the file is reported skipped | | `describe.only` | `fdescribe` | only that block, and other focused blocks, run in this file | | `test.skip` | `it.skip`, `xit`, `xtest` | the test is not run and is reported skipped | | `describe.skip` | `xdescribe` | the whole block is skipped | | `test.todo(name)` | `it.todo` | counted as a todo in the summary; it takes no callback | | `test.failing` | `it.failing` | passes when the body throws and fails when it does not | Points that come up in interviews: - **Focus is per file.** A `test.only` in one file does not stop other files from running. - A `.only` is a debugging aid; committing one silently skips the rest of that file. - `test.skip` keeps the code compiling and readable, which is better than commenting a test out. - `test.todo` given a callback throws *Todo must be called with only a description.* Use `test.skip` for an implemented test you do not want to run. - `test.failing` suits a test that reproduces a known bug: it stays green while the bug exists, turns red once the code is fixed, and the modifier is then removed. It is available with the default runner, jest-circus. ## Data-driven tests with .each `test.each` and `describe.each` write the body once and run it per row of data. There are two table forms: 1. **An array of rows.** Each inner array is spread into the callback's arguments; a one-dimensional array such as `[1, 2, 3]` is treated as `[[1], [2], [3]]`. The title takes `printf`-style placeholders: `%s` string, `%d` number, `%i` integer, `%p` pretty-formatted value, `%#` row index. Rows can also be objects, injected into the title as `$field`. 2. **A tagged template.** The first line names the columns separated by `|`, each later line supplies `${value}` cells, and the callback receives one object per row, with `$column` available in the title. Each generated test has its own title and its own pass or fail, so a failing row is named in the report. The modifiers combine: `test.only.each` and `test.skip.each` focus or skip a whole table. ## Reading the summary The run summary counts each kind separately, for example *Tests: 1 skipped, 1 todo, 4 passed, 6 total*. A growing skipped or todo count is visible there, which is one reason to prefer these forms over deleted or commented-out tests.
- Why does a fixture created directly in a `describe` body behave differently from one created in `beforeEach`?The `describe` body runs once, during collection, before any test, so its object is created a single time and every test in the block shares and can mutate it. `beforeEach` runs before each test during execution, so each test receives a fresh copy. Order-dependent failures often trace back to state built in a `describe` body.
- How do you give each `test.each` row a readable title when the rows are objects?Use `$field` in the title, for example `test.each([{ input: '12', expected: 12 }])('parses $input as $expected', ...)`; a key path such as `$row.input` reaches nested own properties, and `$#` injects the row index. The `printf` placeholders such as `%s` cannot be mixed with `$field` in the same title, apart from `%%`.
- Code in a describe body runs again before each test, like beforeEach.
- test.only in one file makes Jest skip every other test file in the run.
- An async describe callback lets you await data before defining tests.
- test.todo accepts a body and runs it later when the feature exists.
- test.skip deletes the test from the report, so nobody sees it.