Skip to content

Commit c590828

Browse files
JulianKniephoffaduh95
authored andcommitted
test_runner: fix escaping in snapshot tests
Snapshotted values are escaped after serialization. This happens when serializing a value for comparison when snapshots already exist, and also when updating them. That is, snapshots are escaped in the internal storage, but when written to disk, one "level" of escaping is removed. That escaping is never added back when reading the snapshots back. This makes even the simplest test trying to serialize a string with an escape code in it fail, like the one I added here. PR-URL: #53833 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 2638604 commit c590828

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

lib/internal/test_runner/snapshot.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ class SnapshotManager {
146146
);
147147
}
148148

149-
this.snapshots = context.exports;
149+
for (const key in context.exports) {
150+
this.snapshots[key] = templateEscape(context.exports[key]);
151+
}
150152
this.loaded = true;
151153
} catch (err) {
152154
let msg = `Cannot read snapshot file '${this.snapshotFile}.'`;

test/fixtures/test-runner/snapshots/unit.js

+4
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ test('`${foo}`', async (t) => {
2222
const options = { serializers: [() => { return '***'; }]};
2323
t.assert.snapshot('snapshotted string', options);
2424
});
25+
26+
test('escapes in `\\${foo}`\n', async (t) => {
27+
t.assert.snapshot('`\\${foo}`\n');
28+
});

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,9 @@ test('t.assert.snapshot()', async (t) => {
296296

297297
t.assert.strictEqual(child.code, 1);
298298
t.assert.strictEqual(child.signal, null);
299-
t.assert.match(child.stdout, /# tests 3/);
299+
t.assert.match(child.stdout, /# tests 4/);
300300
t.assert.match(child.stdout, /# pass 0/);
301-
t.assert.match(child.stdout, /# fail 3/);
301+
t.assert.match(child.stdout, /# fail 4/);
302302
t.assert.match(child.stdout, /Missing snapshots/);
303303
});
304304

@@ -311,8 +311,8 @@ test('t.assert.snapshot()', async (t) => {
311311

312312
t.assert.strictEqual(child.code, 0);
313313
t.assert.strictEqual(child.signal, null);
314-
t.assert.match(child.stdout, /tests 3/);
315-
t.assert.match(child.stdout, /pass 3/);
314+
t.assert.match(child.stdout, /tests 4/);
315+
t.assert.match(child.stdout, /pass 4/);
316316
t.assert.match(child.stdout, /fail 0/);
317317
});
318318

@@ -325,8 +325,8 @@ test('t.assert.snapshot()', async (t) => {
325325

326326
t.assert.strictEqual(child.code, 0);
327327
t.assert.strictEqual(child.signal, null);
328-
t.assert.match(child.stdout, /tests 3/);
329-
t.assert.match(child.stdout, /pass 3/);
328+
t.assert.match(child.stdout, /tests 4/);
329+
t.assert.match(child.stdout, /pass 4/);
330330
t.assert.match(child.stdout, /fail 0/);
331331
});
332332
});

0 commit comments

Comments
 (0)