-
Notifications
You must be signed in to change notification settings - Fork 30.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test_runner: add t.assert.fileSnapshot()
This commit adds a t.assert.fileSnapshot() API to the test runner. This is similar to how snapshot tests work in core, as well as userland options such as toMatchFileSnapshot().
- Loading branch information
Showing
6 changed files
with
229 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
const { test } = require('node:test'); | ||
|
||
test('snapshot file path is created', (t) => { | ||
t.assert.fileSnapshot({ baz: 9 }, './foo/bar/baz/1.json'); | ||
}); | ||
|
||
test('test with plan', (t) => { | ||
t.plan(2); | ||
t.assert.fileSnapshot({ foo: 1, bar: 2 }, '2.json'); | ||
t.assert.fileSnapshot(5, '3.txt'); | ||
}); | ||
|
||
test('custom serializers are supported', (t) => { | ||
t.assert.fileSnapshot({ foo: 1 }, '4.txt', { | ||
serializers: [ | ||
(value) => { return value + '424242'; }, | ||
(value) => { return JSON.stringify(value); }, | ||
] | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const fixtures = require('../common/fixtures'); | ||
const tmpdir = require('../common/tmpdir'); | ||
const { suite, test } = require('node:test'); | ||
|
||
tmpdir.refresh(); | ||
|
||
suite('t.assert.fileSnapshot() validation', () => { | ||
test('path must be a string', (t) => { | ||
t.assert.throws(() => { | ||
t.assert.fileSnapshot({}, 5); | ||
}, /The "path" argument must be of type string/); | ||
}); | ||
|
||
test('options must be an object', (t) => { | ||
t.assert.throws(() => { | ||
t.assert.fileSnapshot({}, '', null); | ||
}, /The "options" argument must be of type object/); | ||
}); | ||
|
||
test('options.serializers must be an array if present', (t) => { | ||
t.assert.throws(() => { | ||
t.assert.fileSnapshot({}, '', { serializers: 5 }); | ||
}, /The "options\.serializers" property must be an instance of Array/); | ||
}); | ||
|
||
test('options.serializers must only contain functions', (t) => { | ||
t.assert.throws(() => { | ||
t.assert.fileSnapshot({}, '', { serializers: [() => {}, ''] }); | ||
}, /The "options\.serializers\[1\]" property must be of type function/); | ||
}); | ||
}); | ||
|
||
suite('t.assert.fileSnapshot() update/read flow', () => { | ||
const fixture = fixtures.path( | ||
'test-runner', 'snapshots', 'file-snapshots.js' | ||
); | ||
|
||
test('fails prior to snapshot generation', async (t) => { | ||
const child = await common.spawnPromisified( | ||
process.execPath, | ||
[fixture], | ||
{ cwd: tmpdir.path }, | ||
); | ||
|
||
t.assert.strictEqual(child.code, 1); | ||
t.assert.strictEqual(child.signal, null); | ||
t.assert.match(child.stdout, /tests 3/); | ||
t.assert.match(child.stdout, /pass 0/); | ||
t.assert.match(child.stdout, /fail 3/); | ||
t.assert.match(child.stdout, /Missing snapshots can be generated/); | ||
}); | ||
|
||
test('passes when regenerating snapshots', async (t) => { | ||
const child = await common.spawnPromisified( | ||
process.execPath, | ||
['--test-update-snapshots', fixture], | ||
{ cwd: tmpdir.path }, | ||
); | ||
|
||
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, /fail 0/); | ||
}); | ||
|
||
test('passes when snapshots exist', async (t) => { | ||
const child = await common.spawnPromisified( | ||
process.execPath, | ||
[fixture], | ||
{ cwd: tmpdir.path }, | ||
); | ||
|
||
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, /fail 0/); | ||
}); | ||
}); |