Skip to content

Commit

Permalink
test: allow especial characters in snapshot keys
Browse files Browse the repository at this point in the history
Fixes: #56836
  • Loading branch information
Ceres6 committed Feb 12, 2025
1 parent 1671921 commit b91c14b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/internal/test_runner/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class SnapshotFile {
}

setSnapshot(id, value) {
this.snapshots[templateEscape(id)] = value;
this.snapshots[keyEscape(id)] = value;
}

nextId(name) {
Expand Down Expand Up @@ -290,6 +290,13 @@ function validateFunctionArray(fns, name) {
}
}

function keyEscape(str) {
let result = JSONStringify(str, null, 2).slice(1, -1);
result = StringPrototypeReplaceAll(result, '`', '\\`');
result = StringPrototypeReplaceAll(result, '${', '\\${');
return result;
}

function templateEscape(str) {
let result = String(str);
result = StringPrototypeReplaceAll(result, '\\', '\\\\');
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-runner-snapshot-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ suite('SnapshotManager', () => {

file.setSnapshot('foo`${x}` 1', 'test');
t.assert.strictEqual(file.getSnapshot('foo\\`\\${x}\\` 1'), 'test');
file.setSnapshot('\r 1', 'test');
t.assert.strictEqual(file.getSnapshot('\\r 1'), 'test');
});

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

0 comments on commit b91c14b

Please sign in to comment.