In Jest 30, how does toMatchInlineSnapshot write its snapshot into the test file, and what stops Jest from writing it?
answer
- the test source is edited
- found by its call site
- a formatter is optional
- one call, one value
basics
~20 sOn the first run Jest serializes the value, finds the toMatchInlineSnapshot call in the test source and inserts the text as a template-literal argument. It refuses in CI mode, and fails when one call site would need more than one value.
solid answer
~50 s`toMatchInlineSnapshot()` with no argument behaves like a missing snapshot: Jest serializes the value, locates the call in the test file from its line and column, and rewrites the source so the call receives the text as a template literal. It formats the edit with the code formatter named by `prettierPath` when that module resolves, and otherwise prints it itself, which can re-indent the surrounding expression. Later runs compare against the literal, and `-u` or the watch-mode `u` key rewrites it in place. Writing is blocked in CI mode (`New snapshot was not written`), and a call reached more than once, from `test.each`, a loop or a helper shared by several tests, fails the file with `Jest: Multiple inline snapshots for the same call are not supported.` The error forms, such as `toThrowErrorMatchingInlineSnapshot()`, store the message, and in Jest 30 each `Error.cause` adds a `Cause:` line.
go deeper
Recall that toMatchInlineSnapshot() starts empty and Jest writes the serialized text into the test file as its argument on the first run.
Explain the call-site write-back, the optional formatter via prettierPath, property matchers first, and the CI and multiple-call failures.
Diagnose the multiple-inline-snapshot error from test.each, loops or shared helpers, and Jest 30's Cause: lines appearing in error snapshots after an upgrade.
Recognise that a runner editing test sources is a workflow cost: formatter alignment and review noise belong in the team's conventions.
## What an inline snapshot is `toMatchInlineSnapshot` is the inline twin of `toMatchSnapshot`. The comparison is identical; only the **storage** differs. Instead of an entry in `__snapshots__/<file>.snap`, the serialized text lives as the matcher's own argument, a template literal in the test source: ```javascript test('rejects a bad port', () => { expect(() => parsePort('eighty')).toThrowErrorMatchingInlineSnapshot( `"Invalid port: eighty"`, ); }); ``` ## How the write-back works 1. You write the call with **no snapshot argument**: `expect(out).toMatchInlineSnapshot()`. 2. The run treats it as a missing snapshot. Jest serializes the value, as it would for a file snapshot, and records the **call site**, the line and column of the matcher call in the test file. 3. After the file's tests finish, Jest parses the test source, finds the call expression at each recorded position and inserts or replaces the template-literal argument. 4. It writes the file back. When the module named by the **`prettierPath`** config key (default `'prettier'`) resolves, Jest hands the edited source to that formatter with the project's settings; when it does not, Jest prints the edit itself, which can re-indent or re-space the surrounding expression. 5. Later runs compare against the literal. `-u`, or `u` in watch mode, rewrites a failing literal in place. Property matchers go **first** and the snapshot second: after the first run, `toMatchInlineSnapshot({createdAt: expect.any(Date)})` carries the serialized text as a template literal in its second argument. ## What stops the write | Situation | Result | |---|---| | CI mode (`--ci` or detected) and the argument is missing | test fails with `New snapshot was not written` | | one call site reached more than once in the file | the file fails with `Jest: Multiple inline snapshots for the same call are not supported.` | | failing literal, no `-u` | an ordinary snapshot failure with a diff | The second row catches people out: - **`test.each`** runs one source call for several rows, so it fails even when every row produces the **same** value. - A **loop** inside one test does the same, because every iteration reaches the same call. - A **shared helper**, `const check = v => expect(v).toMatchInlineSnapshot()`, called from two tests, is one call site serving two snapshots. The fix is to give each value its own call, or to use `toMatchSnapshot`, whose keys come from the test name and a counter rather than a position in the source. ## Error snapshots inline - `toThrowErrorMatchingInlineSnapshot()` calls the function, catches what it throws and stores the **message string**, not the error object. - **In Jest 30**, when the thrown error has a `cause`, Jest walks the chain and appends one `Cause: <message>` line per cause, so a wrapped `ENOENT` is recorded as `"Config load failed\nCause: ENOENT"` across two lines. - Snapshotting the caught error object itself with `toMatchInlineSnapshot()` prints `[Error: Config load failed]`, without the message of the cause. ## Working with write-back day to day - **Start empty, run, read.** The usual loop is to write `toMatchInlineSnapshot()`, run the file once, and read the literal Jest inserted before committing it; the inserted text is the assertion. - **Expect formatting noise.** Without the formatter, Jest re-prints the edited call, so the first write may also change spacing in that expression; with it, the project's formatting rules apply. - **Do not hand-edit to make a test pass.** Editing the literal is allowed, but editing it to match broken output is the same rubber stamp as a blind update. - **Escaping is handled for you.** Backticks, backslashes and `${` sequences in the serialized text are escaped when Jest writes the template literal, which is why a stored regex shows doubled backslashes. ## Inline versus file, mechanically - **Same matching**: serialization, property matchers, serializers and the update flag behave the same. - **No orphaned entries**: an inline literal is deleted together with its test, so nothing is left behind in a `.snap` file to report as obsolete. - **Source edits during a run**: the runner modifies files you are editing, so save before running and expect formatting changes in the diff. - **Size**: a long literal sits in the middle of the test; large values are easier to review in a separate file. Which form a team should prefer for component output is a separate trade-off, covered with component snapshot practice; the mechanics above hold either way.
- Why does a Jest test.each table with toMatchInlineSnapshot() fail to write even when every row produces the same output?Inline snapshots are written by source position. Every row of `test.each` reaches the same call expression, so Jest has several snapshots for one call site and cannot decide which to write; it stops with `Jest: Multiple inline snapshots for the same call are not supported.` Use `toMatchSnapshot`, keyed by each row's test name, or write one call per case.
- What does toThrowErrorMatchingInlineSnapshot record in Jest 30 when the thrown error wraps another error as its cause?The top-level message followed by one `Cause: <message>` line for each error in the `cause` chain, as a single multi-line string. A non-error string cause is appended once and the walk stops. This was a breaking change in Jest 30, so suites upgraded from 29 see those lines appear in existing error snapshots.
- Inline snapshots need the code formatter installed or Jest cannot write them
- An inline snapshot inside test.each stores one value per row
- --ci still fills in an empty toMatchInlineSnapshot call
- Inline snapshots compare differently from .snap file snapshots
- toThrowErrorMatchingInlineSnapshot stores the whole error object
Assumes Jest 30, whose error-snapshot matchers append a Cause: line for each Error.cause in the chain; Jest 29 stored only the top-level message.