Skip to content

Commit 2945e44

Browse files
committed
test_runner: allow special characters in snapshot keys
Fixes: #56836
1 parent 1671921 commit 2945e44

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/internal/test_runner/snapshot.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class SnapshotFile {
7777
}
7878

7979
setSnapshot(id, value) {
80-
this.snapshots[templateEscape(id)] = value;
80+
this.snapshots[keyEscape(id)] = value;
8181
}
8282

8383
nextId(name) {
@@ -290,6 +290,13 @@ function validateFunctionArray(fns, name) {
290290
}
291291
}
292292

293+
function keyEscape(str) {
294+
let result = JSONStringify(str, null, 2).slice(1, -1);
295+
result = StringPrototypeReplaceAll(result, '`', '\\`');
296+
result = StringPrototypeReplaceAll(result, '${', '\\${');
297+
return result;
298+
}
299+
293300
function templateEscape(str) {
294301
let result = String(str);
295302
result = StringPrototypeReplaceAll(result, '\\', '\\\\');

test/parallel/test-runner-snapshot-tests.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ suite('SnapshotManager', () => {
158158

159159
file.setSnapshot('foo`${x}` 1', 'test');
160160
t.assert.strictEqual(file.getSnapshot('foo\\`\\${x}\\` 1'), 'test');
161+
file.setSnapshot('\r 1', 'test');
162+
t.assert.strictEqual(file.getSnapshot('\\r 1'), 'test');
161163
});
162164

163165
test('throws if snapshot file cannot be resolved', (t) => {

0 commit comments

Comments
 (0)