Skip to content

Commit 1d92b78

Browse files
Support multiline snapshot labels
Fixes #2769. Co-authored-by: Mark Wubben <[email protected]>
1 parent 11aaf2a commit 1d92b78

File tree

6 files changed

+71
-1
lines changed

6 files changed

+71
-1
lines changed

lib/snapshot-manager.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ function formatEntry(snapshot, index) {
9393
concordance.formatDescriptor(concordance.deserialize(data), concordanceOptions) :
9494
'<No Data>';
9595

96-
return `> ${label}\n\n${indentString(description, 4)}`;
96+
const blockquote = label.split(/\n/).map(line => '> ' + line).join('\n');
97+
98+
return `${blockquote}\n\n${indentString(description, 4)}`;
9799
}
98100

99101
function combineEntries({blocks}) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const test = require(process.env.TEST_AVA_IMPORT_FROM);
2+
3+
const f = () => {
4+
return [
5+
'Hello',
6+
'World!'
7+
].join(', ');
8+
};
9+
10+
test('snapshot with a multiline label', t => {
11+
const result = f();
12+
const label = '```javascript\n' + f.toString() + '\n```';
13+
t.snapshot(result, label);
14+
});

test/snapshot-tests/formatting.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
4+
import test from '@ava/test';
5+
6+
import {cwd, fixture} from '../helpers/exec.js';
7+
import {withTemporaryFixture} from '../helpers/with-temporary-fixture.js';
8+
9+
test('multiline snapshot label should be formatted correctly in the report', async t => {
10+
await withTemporaryFixture(cwd('multiline-snapshot-label'), async cwd => {
11+
// Run test fixture
12+
await fixture(['--update-snapshots'], {
13+
cwd,
14+
env: {
15+
AVA_FORCE_CI: 'not-ci'
16+
}
17+
});
18+
19+
// Assert report is unchanged
20+
const reportPath = path.join(cwd, 'test.js.md');
21+
const report = fs.readFileSync(reportPath, {encoding: 'utf8'});
22+
t.snapshot(report, 'resulting snapshot report');
23+
});
24+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Snapshot report for `test/snapshot-tests/formatting.js`
2+
3+
The actual snapshot is saved in `formatting.js.snap`.
4+
5+
Generated by [AVA](https://avajs.dev).
6+
7+
## multiline snapshot label should be formatted correctly in the report
8+
9+
> resulting snapshot report
10+
11+
`# Snapshot report for \`test.js\`␊
12+
13+
The actual snapshot is saved in \`test.js.snap\`.␊
14+
15+
Generated by [AVA](https://avajs.dev).␊
16+
17+
## snapshot with a multiline label␊
18+
19+
> \`\`\`javascript␊
20+
> () => {␊
21+
> return [␊
22+
> 'Hello',␊
23+
> 'World!'␊
24+
> ].join(', ');␊
25+
> }␊
26+
> \`\`\`␊
27+
28+
'Hello, World!'␊
29+
`
305 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)