diff --git a/lib/internal/test_runner/snapshot.js b/lib/internal/test_runner/snapshot.js index 16a994c02a63928..1331d3ed9f33f17 100644 --- a/lib/internal/test_runner/snapshot.js +++ b/lib/internal/test_runner/snapshot.js @@ -147,6 +147,9 @@ class SnapshotManager { } this.snapshots = context.exports; + for (const [key, value] of Object.entries(this.snapshots)) { + this.snapshots[key] = templateEscape(value); + } this.loaded = true; } catch (err) { let msg = `Cannot read snapshot file '${this.snapshotFile}.'`; diff --git a/test/fixtures/test-runner/snapshots/unit.js b/test/fixtures/test-runner/snapshots/unit.js index 0718314239eedef..f45ddad0cef5a2c 100644 --- a/test/fixtures/test-runner/snapshots/unit.js +++ b/test/fixtures/test-runner/snapshots/unit.js @@ -22,3 +22,7 @@ test('`${foo}`', async (t) => { const options = { serializers: [() => { return '***'; }]}; t.assert.snapshot('snapshotted string', options); }); + +test('escapes', async (t) => { + t.assert.snapshot('\n'); +}); diff --git a/test/parallel/test-runner-snapshot-tests.js b/test/parallel/test-runner-snapshot-tests.js index ae4130190d6ee13..d3abf356821d567 100644 --- a/test/parallel/test-runner-snapshot-tests.js +++ b/test/parallel/test-runner-snapshot-tests.js @@ -296,9 +296,9 @@ test('t.assert.snapshot()', async (t) => { t.assert.strictEqual(child.code, 1); t.assert.strictEqual(child.signal, null); - t.assert.match(child.stdout, /# tests 3/); + t.assert.match(child.stdout, /# tests 4/); t.assert.match(child.stdout, /# pass 0/); - t.assert.match(child.stdout, /# fail 3/); + t.assert.match(child.stdout, /# fail 4/); t.assert.match(child.stdout, /Missing snapshots/); }); @@ -311,8 +311,8 @@ test('t.assert.snapshot()', async (t) => { t.assert.strictEqual(child.code, 0); t.assert.strictEqual(child.signal, null); - t.assert.match(child.stdout, /tests 3/); - t.assert.match(child.stdout, /pass 3/); + t.assert.match(child.stdout, /tests 4/); + t.assert.match(child.stdout, /pass 4/); t.assert.match(child.stdout, /fail 0/); }); @@ -325,8 +325,8 @@ test('t.assert.snapshot()', async (t) => { t.assert.strictEqual(child.code, 0); t.assert.strictEqual(child.signal, null); - t.assert.match(child.stdout, /tests 3/); - t.assert.match(child.stdout, /pass 3/); + t.assert.match(child.stdout, /tests 4/); + t.assert.match(child.stdout, /pass 4/); t.assert.match(child.stdout, /fail 0/); }); });