diff --git a/lib/snapshot-manager.js b/lib/snapshot-manager.js index 9181c1424..4973e5ed8 100644 --- a/lib/snapshot-manager.js +++ b/lib/snapshot-manager.js @@ -93,7 +93,9 @@ function formatEntry(snapshot, index) { concordance.formatDescriptor(concordance.deserialize(data), concordanceOptions) : ''; - return `> ${label}\n\n${indentString(description, 4)}`; + const blockquote = label.split(/\n/).map(line => '> ' + line).join('\n'); + + return `${blockquote}\n\n${indentString(description, 4)}`; } function combineEntries({blocks}) { diff --git a/test/snapshot-tests/fixtures/multiline-snapshot-label/package.json b/test/snapshot-tests/fixtures/multiline-snapshot-label/package.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/test/snapshot-tests/fixtures/multiline-snapshot-label/package.json @@ -0,0 +1 @@ +{} diff --git a/test/snapshot-tests/fixtures/multiline-snapshot-label/test.js b/test/snapshot-tests/fixtures/multiline-snapshot-label/test.js new file mode 100644 index 000000000..2c3e6b2c9 --- /dev/null +++ b/test/snapshot-tests/fixtures/multiline-snapshot-label/test.js @@ -0,0 +1,14 @@ +const test = require(process.env.TEST_AVA_IMPORT_FROM); + +const f = () => { + return [ + 'Hello', + 'World!' + ].join(', '); +}; + +test('snapshot with a multiline label', t => { + const result = f(); + const label = '```javascript\n' + f.toString() + '\n```'; + t.snapshot(result, label); +}); diff --git a/test/snapshot-tests/formatting.js b/test/snapshot-tests/formatting.js new file mode 100644 index 000000000..3585f4900 --- /dev/null +++ b/test/snapshot-tests/formatting.js @@ -0,0 +1,24 @@ +import fs from 'fs'; +import path from 'path'; + +import test from '@ava/test'; + +import {cwd, fixture} from '../helpers/exec.js'; +import {withTemporaryFixture} from '../helpers/with-temporary-fixture.js'; + +test('multiline snapshot label should be formatted correctly in the report', async t => { + await withTemporaryFixture(cwd('multiline-snapshot-label'), async cwd => { + // Run test fixture + await fixture(['--update-snapshots'], { + cwd, + env: { + AVA_FORCE_CI: 'not-ci' + } + }); + + // Assert report is unchanged + const reportPath = path.join(cwd, 'test.js.md'); + const report = fs.readFileSync(reportPath, {encoding: 'utf8'}); + t.snapshot(report, 'resulting snapshot report'); + }); +}); diff --git a/test/snapshot-tests/snapshots/formatting.js.md b/test/snapshot-tests/snapshots/formatting.js.md new file mode 100644 index 000000000..aa2d483b0 --- /dev/null +++ b/test/snapshot-tests/snapshots/formatting.js.md @@ -0,0 +1,29 @@ +# Snapshot report for `test/snapshot-tests/formatting.js` + +The actual snapshot is saved in `formatting.js.snap`. + +Generated by [AVA](https://avajs.dev). + +## multiline snapshot label should be formatted correctly in the report + +> resulting snapshot report + + `# Snapshot report for \`test.js\`␊ + ␊ + The actual snapshot is saved in \`test.js.snap\`.␊ + ␊ + Generated by [AVA](https://avajs.dev).␊ + ␊ + ## snapshot with a multiline label␊ + ␊ + > \`\`\`javascript␊ + > () => {␊ + > return [␊ + > 'Hello',␊ + > 'World!'␊ + > ].join(', ');␊ + > }␊ + > \`\`\`␊ + ␊ + 'Hello, World!'␊ + ` diff --git a/test/snapshot-tests/snapshots/formatting.js.snap b/test/snapshot-tests/snapshots/formatting.js.snap new file mode 100644 index 000000000..8382a9887 Binary files /dev/null and b/test/snapshot-tests/snapshots/formatting.js.snap differ