Inline Snapshots
You'll learn how to keep small snapshots directly inside your test file for easier review and maintenance.
What Are Inline Snapshots?
Regular snapshots store their expected output in separate __snapshots__ files, which can make it harder to understand what a test expects at a glance. Inline snapshots solve this by embedding the snapshot value directly in your test code using toMatchInlineSnapshot(). When you first run the test, the matcher updates itself with the actual output.
How They Work
Instead of this:
test('formats user object', () => {
const user = formatUser({ name: 'Alice', age: 30 });
expect(user).toMatchSnapshot();
});
// Snapshot lives in __snapshots__/test.js.snap
You write this:
test('formats user object', () => {
const user = formatUser({ name: 'Alice', age: 30 });
expect(user).toMatchInlineSnapshot();
});
On first run, the test runner automatically updates your source code to:
test('formats user object', () => {
const user = formatUser({ name: 'Alice', age: 30 });
expect(user).toMatchInlineSnapshot(`
{
"displayName": "Alice",
"age": 30,
}
`);
});
When to Use Inline Snapshots
Use inline snapshots for small, readable outputs like configuration objects, simple API responses, or error messages. They're perfect when the snapshot fits comfortably in your test file and makes the test's intent clearer. For large outputs (rendered components, long HTML), stick with regular snapshots in separate files.
Key Takeaway: Inline snapshots with toMatchInlineSnapshot() embed expected values directly in your test code, making small outputs more visible and easier to review during code changes.