Skip to content

Commit 5a76af1

Browse files
Ceres6acidiney
authored andcommitted
test_runner: allow special characters in snapshot keys
Fixes: nodejs#56836 PR-URL: nodejs#57017 Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
1 parent 01df2c0 commit 5a76af1

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

lib/internal/test_runner/snapshot.js

+8-1
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[escapeSnapshotKey(id)] = value;
8181
}
8282

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

293+
function escapeSnapshotKey(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, '\\', '\\\\');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
const { snapshot, test } = require('node:test');
3+
const { basename, join } = require('node:path');
4+
5+
snapshot.setResolveSnapshotPath((testFile) => {
6+
return join(process.cwd(), `${basename(testFile)}.snapshot`);
7+
});
8+
9+
test('\r', (t) => {
10+
t.assert.snapshot({ key: 'value' });
11+
});
12+
13+
test(String.fromCharCode(55296), (t) => {
14+
t.assert.snapshot({ key: 'value' });
15+
});
16+
17+
test(String.fromCharCode(57343), (t) => {
18+
t.assert.snapshot({ key: 'value' });
19+
});

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

+26
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) => {
@@ -340,6 +342,30 @@ test('t.assert.snapshot()', async (t) => {
340342
});
341343
});
342344

345+
test('special characters are allowed', async (t) => {
346+
const fixture = fixtures.path(
347+
'test-runner', 'snapshots', 'special-character.js'
348+
);
349+
350+
await common.spawnPromisified(
351+
process.execPath,
352+
['--test-update-snapshots', fixture],
353+
{ cwd: tmpdir.path },
354+
);
355+
356+
const child = await common.spawnPromisified(
357+
process.execPath,
358+
[fixture],
359+
{ cwd: tmpdir.path },
360+
);
361+
362+
t.assert.strictEqual(child.code, 0);
363+
t.assert.strictEqual(child.signal, null);
364+
t.assert.match(child.stdout, /tests 3/);
365+
t.assert.match(child.stdout, /pass 3/);
366+
t.assert.match(child.stdout, /fail 0/);
367+
});
368+
343369
test('snapshots from multiple files (isolation=none)', async (t) => {
344370
tmpdir.refresh();
345371

0 commit comments

Comments
 (0)