Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert: don't serialize assert.snapshot input #44443

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -1512,6 +1512,8 @@ loading phase, it will always raise it as an uncaught exception.
added: v18.8.0
-->

> Stability: 1 - Experimental

Force updating snapshot files for [`assert.snapshot()`][]

### `--use-bundled-ca`, `--use-openssl-ca`
Expand Down
6 changes: 3 additions & 3 deletions lib/internal/assert/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ function getTarget() {
}

function serializeName(name) {
validateString(name, 'name');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick question: Is there a specific reason to remove this validation in here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved it to the callsite

return StringPrototypeReplace(`${name}`, kKeyDelimiter, '_');
}

Expand Down Expand Up @@ -109,12 +108,13 @@ async function getSnapshot() {
}


async function snapshot(input, name) {
async function snapshot(value, name) {
validateString(name, 'name');
validateString(value, 'value');
const snapshot = await getSnapshot();
counter = counter + 1;
name = serializeName(name);

const value = inspect(input);
if (snapshot === kInitialSnapshot) {
await writeSnapshot({ name, value });
} else if (snapshot.has(name)) {
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/assert-snapshot/non-existing-name.snapshot
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
another name:
'test'
test
#*#*#*#*#*#*#*#*#*#*#*#
name:
'test'
test
4 changes: 2 additions & 2 deletions test/fixtures/assert-snapshot/random.snapshot
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
random1:
'Random Value: *'
Random Value: *
#*#*#*#*#*#*#*#*#*#*#*#
random2:
'Random Value: *'
Random Value: *
11 changes: 0 additions & 11 deletions test/fixtures/assert-snapshot/serialize.mjs

This file was deleted.

11 changes: 0 additions & 11 deletions test/fixtures/assert-snapshot/serialize.snapshot

This file was deleted.

2 changes: 1 addition & 1 deletion test/fixtures/assert-snapshot/value-changed.snapshot
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
snapshot:
'original'
original
22 changes: 7 additions & 15 deletions test/parallel/test-assert-snapshot.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,28 @@ describe('assert.snapshot', { concurrency: true }, () => {
await unlink(snapshotPath);
assert.strictEqual(stderr, '');
assert.strictEqual(code, 0);
assert.match(snapshot, /^name:\r?\n'test'$/);
assert.match(snapshot, /^name:\r?\ntest$/);
});

it('should write multiple snapshots', async () => {
const { stderr, code, snapshot, snapshotPath } = await spawnFixture(fixtures.path('assert-snapshot/multiple.mjs'));
await unlink(snapshotPath);
assert.strictEqual(stderr, '');
assert.strictEqual(code, 0);
assert.match(snapshot, /^name:\n'test'\r?\n#\*#\*#\*#\*#\*#\*#\*#\*#\*#\*#\*#\r?\nanother name:\r?\n'test'$/);
assert.match(snapshot, /^name:\ntest\r?\n#\*#\*#\*#\*#\*#\*#\*#\*#\*#\*#\*#\r?\nanother name:\r?\ntest$/);
});

it('should succeed running multiple times', async () => {
let result = await spawnFixture(fixtures.path('assert-snapshot/single.mjs'), false);
assert.strictEqual(result.stderr, '');
assert.strictEqual(result.code, 0);
assert.match(result.snapshot, /^snapshot:\r?\n'test'$/);
assert.match(result.snapshot, /^snapshot:\r?\ntest$/);

result = await spawnFixture(fixtures.path('assert-snapshot/single.mjs'));
await unlink(result.snapshotPath);
assert.strictEqual(result.stderr, '');
assert.strictEqual(result.code, 0);
assert.match(result.snapshot, /^snapshot:\r?\n'test'$/);
assert.match(result.snapshot, /^snapshot:\r?\ntest$/);
});

it('should fail when name is not provided', async () => {
Expand All @@ -82,15 +82,15 @@ describe('assert.snapshot', { concurrency: true }, () => {
const { code, stderr, snapshot } = await spawnFixture(fixtures.path('assert-snapshot/value-changed.mjs'));
assert.match(stderr, /AssertionError \[ERR_ASSERTION\]/);
assert.strictEqual(code, 1);
assert.match(snapshot, /^snapshot:\r?\n'original'$/);
assert.match(snapshot, /^snapshot:\r?\noriginal$/);
});

it('should fail when snapshot does not contain a named snapshot', async () => {
const { code, stderr, snapshot } = await spawnFixture(fixtures.path('assert-snapshot/non-existing-name.mjs'));
assert.match(stderr, /AssertionError \[ERR_ASSERTION\]/);
assert.match(stderr, /Snapshot "non existing" does not exist/);
assert.strictEqual(code, 1);
assert.match(snapshot, /^another name:\r?\n'test'\r?\n#\*#\*#\*#\*#\*#\*#\*#\*#\*#\*#\*#\r?\nname:\r?\n'test'$/);
assert.match(snapshot, /^another name:\r?\ntest\r?\n#\*#\*#\*#\*#\*#\*#\*#\*#\*#\*#\*#\r?\nname:\r?\ntest$/);
});

it('should snapshot a random replaced value', async () => {
Expand All @@ -101,22 +101,14 @@ describe('assert.snapshot', { concurrency: true }, () => {
assert.strictEqual(snapshot, originalSnapshot);
});

it('should serialize values', async () => {
const originalSnapshot = await readFile(fixtures.path('assert-snapshot/serialize.snapshot'), 'utf8');
const { stderr, code, snapshot } = await spawnFixture(fixtures.path('assert-snapshot/serialize.mjs'));
assert.strictEqual(stderr, '');
assert.strictEqual(code, 0);
assert.strictEqual(snapshot, originalSnapshot);
});

it('should override snapshot when passing --update-assert-snapshot', async () => {
const filename = 'updated.mjs';
await writeFile(getSnapshotPath(filename), 'snapshot:\n\'test\'');
const { stderr, code, snapshot } = await spawnTmpfile('await assert.snapshot(\'changed\', \'snapshot\');',
filename, ['--update-assert-snapshot']);
assert.strictEqual(stderr, '');
assert.strictEqual(code, 0);
assert.match(snapshot, /^snapshot:\r?\n'changed'$/);
assert.match(snapshot, /^snapshot:\r?\nchanged$/);
});

it('snapshot file should have the name of the module - esm', async () => {
Expand Down