|
| 1 | +import { spawnPromisified } from '../common/index.mjs'; |
| 2 | +import * as fixtures from '../common/fixtures.mjs'; |
| 3 | +import tmpdir from '../common/tmpdir.js'; |
| 4 | +import assert from 'node:assert'; |
| 5 | +import path from 'node:path'; |
| 6 | +import { execPath } from 'node:process'; |
| 7 | +import { writeFile, readFile, unlink } from 'node:fs/promises'; |
| 8 | +import { describe, it } from 'node:test'; |
| 9 | + |
| 10 | +tmpdir.refresh(); |
| 11 | + |
| 12 | +function getSnapshotPath(filename) { |
| 13 | + const { name, dir } = path.parse(filename); |
| 14 | + return path.join(tmpdir.path, dir, `${name}.snapshot`); |
| 15 | +} |
| 16 | + |
| 17 | +async function spawnTmpfile(content = '', filename, extraFlags = []) { |
| 18 | + const header = filename.endsWith('.mjs') ? |
| 19 | + 'import assert from \'node:assert\';' : |
| 20 | + 'const assert = require(\'node:assert\');'; |
| 21 | + await writeFile(path.join(tmpdir.path, filename), `${header}\n${content}`); |
| 22 | + const { stdout, stderr, code } = await spawnPromisified( |
| 23 | + execPath, |
| 24 | + ['--no-warnings', ...extraFlags, filename], |
| 25 | + { cwd: tmpdir.path }); |
| 26 | + |
| 27 | + const snapshotPath = getSnapshotPath(filename); |
| 28 | + const snapshot = await readFile(snapshotPath, 'utf8').catch((err) => err); |
| 29 | + return { stdout, stderr, code, snapshot, snapshotPath, filename }; |
| 30 | +} |
| 31 | + |
| 32 | +async function spawnFixture(filename) { |
| 33 | + const { dir, base, name } = path.parse(filename); |
| 34 | + const { stdout, stderr, code } = await spawnPromisified(execPath, ['--no-warnings', base], { cwd: dir }); |
| 35 | + |
| 36 | + const snapshotPath = path.join(dir, `${name}.snapshot`); |
| 37 | + const snapshot = await readFile(snapshotPath, 'utf8').catch((err) => err); |
| 38 | + return { stdout, stderr, code, snapshot, snapshotPath }; |
| 39 | +} |
| 40 | + |
| 41 | +describe('assert.snapshot', { concurrency: true }, () => { |
| 42 | + it('should write snapshot', async () => { |
| 43 | + const { stderr, code, snapshot, snapshotPath } = await spawnFixture(fixtures.path('assert-snapshot/basic.mjs')); |
| 44 | + await unlink(snapshotPath); |
| 45 | + assert.strictEqual(stderr, ''); |
| 46 | + assert.strictEqual(code, 0); |
| 47 | + assert.match(snapshot, /^name:\r?\n'test'$/); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should write multiple snapshots', async () => { |
| 51 | + const { stderr, code, snapshot, snapshotPath } = await spawnFixture(fixtures.path('assert-snapshot/multiple.mjs')); |
| 52 | + await unlink(snapshotPath); |
| 53 | + assert.strictEqual(stderr, ''); |
| 54 | + assert.strictEqual(code, 0); |
| 55 | + assert.match(snapshot, /^name:\n'test'\r?\n#\*#\*#\*#\*#\*#\*#\*#\*#\*#\*#\*#\r?\nanother name:\r?\n'test'$/); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should succeed running multiple times', async () => { |
| 59 | + let result = await spawnFixture(fixtures.path('assert-snapshot/single.mjs'), false); |
| 60 | + assert.strictEqual(result.stderr, ''); |
| 61 | + assert.strictEqual(result.code, 0); |
| 62 | + assert.match(result.snapshot, /^snapshot:\r?\n'test'$/); |
| 63 | + |
| 64 | + result = await spawnFixture(fixtures.path('assert-snapshot/single.mjs')); |
| 65 | + await unlink(result.snapshotPath); |
| 66 | + assert.strictEqual(result.stderr, ''); |
| 67 | + assert.strictEqual(result.code, 0); |
| 68 | + assert.match(result.snapshot, /^snapshot:\r?\n'test'$/); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should fail when name is not provided', async () => { |
| 72 | + for (const name of [1, undefined, null, {}, function() {}]) { |
| 73 | + await assert.rejects(() => assert.snapshot('', name), { |
| 74 | + code: 'ERR_INVALID_ARG_TYPE', |
| 75 | + name: 'TypeError', |
| 76 | + message: /^The "name" argument must be of type string/ |
| 77 | + }); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + it('should fail when value does not match snapshot', async () => { |
| 82 | + const { code, stderr, snapshot } = await spawnFixture(fixtures.path('assert-snapshot/value-changed.mjs')); |
| 83 | + assert.match(stderr, /AssertionError \[ERR_ASSERTION\]/); |
| 84 | + assert.strictEqual(code, 1); |
| 85 | + assert.match(snapshot, /^snapshot:\r?\n'original'$/); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should fail when snapshot does not contain a named snapshot', async () => { |
| 89 | + const { code, stderr, snapshot } = await spawnFixture(fixtures.path('assert-snapshot/non-existing-name.mjs')); |
| 90 | + assert.match(stderr, /AssertionError \[ERR_ASSERTION\]/); |
| 91 | + assert.match(stderr, /Snapshot "non existing" does not exist/); |
| 92 | + assert.strictEqual(code, 1); |
| 93 | + assert.match(snapshot, /^another name:\r?\n'test'\r?\n#\*#\*#\*#\*#\*#\*#\*#\*#\*#\*#\*#\r?\nname:\r?\n'test'$/); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should snapshot a random replaced value', async () => { |
| 97 | + const originalSnapshot = await readFile(fixtures.path('assert-snapshot/random.snapshot'), 'utf8'); |
| 98 | + const { stderr, code, snapshot } = await spawnFixture(fixtures.path('assert-snapshot/random.mjs')); |
| 99 | + assert.strictEqual(stderr, ''); |
| 100 | + assert.strictEqual(code, 0); |
| 101 | + assert.strictEqual(snapshot, originalSnapshot); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should serialize values', async () => { |
| 105 | + const originalSnapshot = await readFile(fixtures.path('assert-snapshot/serialize.snapshot'), 'utf8'); |
| 106 | + const { stderr, code, snapshot } = await spawnFixture(fixtures.path('assert-snapshot/serialize.mjs')); |
| 107 | + assert.strictEqual(stderr, ''); |
| 108 | + assert.strictEqual(code, 0); |
| 109 | + assert.strictEqual(snapshot, originalSnapshot); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should override snapshot when passing --update-assert-snapshot', async () => { |
| 113 | + const filename = 'updated.mjs'; |
| 114 | + await writeFile(getSnapshotPath(filename), 'snapshot:\n\'test\''); |
| 115 | + const { stderr, code, snapshot } = await spawnTmpfile('await assert.snapshot(\'changed\', \'snapshot\');', |
| 116 | + filename, ['--update-assert-snapshot']); |
| 117 | + assert.strictEqual(stderr, ''); |
| 118 | + assert.strictEqual(code, 0); |
| 119 | + assert.match(snapshot, /^snapshot:\r?\n'changed'$/); |
| 120 | + }); |
| 121 | + |
| 122 | + it('snapshot file should have the name of the module - esm', async () => { |
| 123 | + const filename = 'name.mjs'; |
| 124 | + const { snapshotPath } = await spawnTmpfile('await assert.snapshot("test");', filename); |
| 125 | + assert.match(snapshotPath, /name\.snapshot$/); |
| 126 | + }); |
| 127 | + |
| 128 | + it('snapshot file should have the name of the module - common js', async () => { |
| 129 | + const filename = 'name.js'; |
| 130 | + const { snapshotPath } = await spawnTmpfile('assert.snapshot("test").then(() => process.exit());', filename); |
| 131 | + assert.match(snapshotPath, /name\.snapshot$/); |
| 132 | + }); |
| 133 | +}); |
0 commit comments