Skip to content

Support code block formatting for string snapshots#3449

Open
novemberborn wants to merge 11 commits into
mainfrom
string-snapshots-as-codeblocks
Open

Support code block formatting for string snapshots#3449
novemberborn wants to merge 11 commits into
mainfrom
string-snapshots-as-codeblocks

Conversation

@novemberborn

@novemberborn novemberborn commented May 14, 2026

Copy link
Copy Markdown
Member

Fixes #3441.

Adds an overload for t.snapshot():

t.snapshot(expected, {formatAsCodeBlock}, message?)

Only accepted when expected is a string. The string is stored verbatim and rendered as a fenced code block in the resulting .snap.md file. Set formatAsCodeBlock to true for a generic code block, or a non-empty language identifier string for syntax highlighting:

test('generates code', t => {
	const code = generate();
	t.snapshot(code, {formatAsCodeBlock: 'js'});
});

This requires a new binary snapshot version, but we retain compatibility with v3 and only write v4 if the snapshot contains these formatted strings.

sindresorhus added a commit to avajs/eslint-plugin-ava that referenced this pull request May 19, 2026
Closes #361

See avajs/ava#3449

Co-authored-by: Mark Wubben <mark@novemberborn.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support code block formatting for string snapshots

1 participant