From d01b5a632b6ac3670b67960028ce71c47ce4a644 Mon Sep 17 00:00:00 2001 From: hanityx Date: Wed, 16 Sep 2026 01:03:40 +0900 Subject: [PATCH] test_runner: escape carriage returns in snapshot values Signed-off-by: hanityx --- lib/internal/test_runner/snapshot.js | 1 + test/fixtures/test-runner/snapshots/special-character.js | 8 ++++++++ test/parallel/test-runner-snapshot-tests.js | 4 ++-- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/internal/test_runner/snapshot.js b/lib/internal/test_runner/snapshot.js index 158ec1d44216..be6893f7c176 100644 --- a/lib/internal/test_runner/snapshot.js +++ b/lib/internal/test_runner/snapshot.js @@ -301,6 +301,7 @@ function escapeSnapshotKey(str) { function templateEscape(str) { let result = String(str); result = StringPrototypeReplaceAll(result, '\\', '\\\\'); + result = StringPrototypeReplaceAll(result, '\r', '\\r'); result = StringPrototypeReplaceAll(result, '`', '\\`'); result = StringPrototypeReplaceAll(result, '${', '\\${'); return result; diff --git a/test/fixtures/test-runner/snapshots/special-character.js b/test/fixtures/test-runner/snapshots/special-character.js index 1c515fb0ba45..e44d6655561f 100644 --- a/test/fixtures/test-runner/snapshots/special-character.js +++ b/test/fixtures/test-runner/snapshots/special-character.js @@ -17,3 +17,11 @@ test(String.fromCharCode(55296), (t) => { test(String.fromCharCode(57343), (t) => { t.assert.snapshot({ key: 'value' }); }); + +test('carriage return in value', (t) => { + t.assert.snapshot('a\rb', { serializers: [String] }); +}); + +test('CRLF in value', (t) => { + t.assert.snapshot('a\r\nb', { serializers: [String] }); +}); diff --git a/test/parallel/test-runner-snapshot-tests.js b/test/parallel/test-runner-snapshot-tests.js index db0c4963613a..8c48f0b24038 100644 --- a/test/parallel/test-runner-snapshot-tests.js +++ b/test/parallel/test-runner-snapshot-tests.js @@ -361,8 +361,8 @@ test('special characters are allowed', 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 5/); + t.assert.match(child.stdout, /pass 5/); t.assert.match(child.stdout, /fail 0/); });