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 404bbc1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
4 changes: 3 additions & 1 deletion lib/internal/test_runner/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ class SnapshotManager {
);
}

this.snapshots = context.exports;
for (const key in context.exports) {
this.snapshots[key] = templateEscape(context.exports[key]);
}
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 in `\\${foo}`\n', async (t) => {
t.assert.snapshot('`\\${foo}`\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 404bbc1

Please sign in to comment.