Skip to content

Commit

Permalink
test_runner: fix escaping in snapshot tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JulianKniephoff committed Jul 13, 2024
1 parent 38b7ce3 commit f22be60
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
3 changes: 3 additions & 0 deletions lib/internal/test_runner/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ class SnapshotManager {
}

this.snapshots = context.exports;
for (const [key, value] of Object.entries(this.snapshots)) {
this.snapshots[key] = templateEscape(value);
}
this.loaded = true;
} catch (err) {
let msg = `Cannot read snapshot file '${this.snapshotFile}.'`;
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/test-runner/snapshots/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ test('`${foo}`', async (t) => {
const options = { serializers: [() => { return '***'; }]};
t.assert.snapshot('snapshotted string', options);
});

test('escapes', async (t) => {
t.assert.snapshot('\n');
});
12 changes: 6 additions & 6 deletions test/parallel/test-runner-snapshot-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,9 @@ test('t.assert.snapshot()', async (t) => {

t.assert.strictEqual(child.code, 1);
t.assert.strictEqual(child.signal, null);
t.assert.match(child.stdout, /# tests 3/);
t.assert.match(child.stdout, /# tests 4/);
t.assert.match(child.stdout, /# pass 0/);
t.assert.match(child.stdout, /# fail 3/);
t.assert.match(child.stdout, /# fail 4/);
t.assert.match(child.stdout, /Missing snapshots/);
});

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

t.assert.strictEqual(child.code, 0);
t.assert.strictEqual(child.signal, null);
t.assert.match(child.stdout, /tests 3/);
t.assert.match(child.stdout, /pass 3/);
t.assert.match(child.stdout, /tests 4/);
t.assert.match(child.stdout, /pass 4/);
t.assert.match(child.stdout, /fail 0/);
});

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

t.assert.strictEqual(child.code, 0);
t.assert.strictEqual(child.signal, null);
t.assert.match(child.stdout, /tests 3/);
t.assert.match(child.stdout, /pass 3/);
t.assert.match(child.stdout, /tests 4/);
t.assert.match(child.stdout, /pass 4/);
t.assert.match(child.stdout, /fail 0/);
});
});

0 comments on commit f22be60

Please sign in to comment.