Skip to content

Commit 2929fc6

Browse files
Ceres6targos
authored andcommitted
test_runner: allow special characters in snapshot keys
Fixes: #56836 PR-URL: #57017 Reviewed-By: Pietro Marchini <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Chemi Atlow <[email protected]>
1 parent e3127b8 commit 2929fc6

File tree

3 files changed

+53
-1
lines changed

3 files changed

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

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

293+
functionescapeSnapshotKey(str){
294+
letresult=JSONStringify(str,null,2).slice(1,-1);
295+
result=StringPrototypeReplaceAll(result,'`','\\`');
296+
result=StringPrototypeReplaceAll(result,'${','\\${');
297+
returnresult;
298+
}
299+
293300
functiontemplateEscape(str){
294301
letresult=String(str);
295302
result=StringPrototypeReplaceAll(result,'\\','\\\\');
Lines changed: 19 additions & 0 deletions
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+
returnjoin(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‎

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

345+
test('special characters are allowed',async(t)=>{
346+
constfixture=fixtures.path(
347+
'test-runner','snapshots','special-character.js'
348+
);
349+
350+
awaitcommon.spawnPromisified(
351+
process.execPath,
352+
['--test-update-snapshots',fixture],
353+
{cwd: tmpdir.path},
354+
);
355+
356+
constchild=awaitcommon.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,/tests3/);
365+
t.assert.match(child.stdout,/pass3/);
366+
t.assert.match(child.stdout,/fail0/);
367+
});
368+
343369
test('snapshots from multiple files (isolation=none)',async(t)=>{
344370
tmpdir.refresh();
345371

0 commit comments

Comments
(0)