|
| 1 | +const test = require('@ava/test'); |
| 2 | +const exec = require('../helpers/exec'); |
| 3 | +const {testSnapshotPruning, withTemporaryFixture} = require('./helpers/macros'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +const macro = withTemporaryFixture(testSnapshotPruning); |
| 7 | + |
| 8 | +test('snapshots are removed when tests stop using them', macro, { |
| 9 | + cwd: exec.cwd('removal'), |
| 10 | + cli: ['--update-snapshots'], |
| 11 | + remove: true |
| 12 | +}); |
| 13 | + |
| 14 | +test('snapshots are removed from a snapshot directory', macro, { |
| 15 | + cwd: exec.cwd('snapshot-dir'), |
| 16 | + cli: ['--update-snapshots'], |
| 17 | + remove: true, |
| 18 | + snapshotPath: path.join('test', 'snapshots', 'test.js.snap'), |
| 19 | + reportPath: path.join('test', 'snapshots', 'test.js.md') |
| 20 | +}); |
| 21 | + |
| 22 | +test('snapshots are removed from a custom snapshotDir', macro, { |
| 23 | + cwd: exec.cwd('fixed-snapshot-dir'), |
| 24 | + cli: ['--update-snapshots'], |
| 25 | + remove: true, |
| 26 | + snapshotPath: path.join('fixedSnapshotDir', 'test.js.snap'), |
| 27 | + reportPath: path.join('fixedSnapshotDir', 'test.js.md') |
| 28 | +}); |
| 29 | + |
| 30 | +test('removing non-existent snapshots doesn\'t throw', async t => { |
| 31 | + // Execute fixture; this should try to unlink the nonexistent snapshots, and |
| 32 | + // should not throw |
| 33 | + const run = exec.fixture(['--update-snapshots'], { |
| 34 | + cwd: exec.cwd('no-snapshots'), |
| 35 | + env: { |
| 36 | + AVA_FORCE_CI: 'not-ci' |
| 37 | + } |
| 38 | + }); |
| 39 | + |
| 40 | + await t.notThrowsAsync(run); |
| 41 | +}); |
| 42 | + |
| 43 | +test('snapshots remain if not updating', macro, { |
| 44 | + cwd: exec.cwd('removal'), |
| 45 | + cli: [], |
| 46 | + remove: false |
| 47 | +}); |
| 48 | + |
| 49 | +test('snapshots remain if they are still used', macro, { |
| 50 | + cwd: exec.cwd('removal'), |
| 51 | + cli: ['--update-snapshots'], |
| 52 | + remove: false, |
| 53 | + env: { |
| 54 | + TEMPLATE: 'true' |
| 55 | + }, |
| 56 | + async checkRun(t, run) { |
| 57 | + await t.notThrowsAsync(run, 'Expected fixture not to throw'); |
| 58 | + const result = await run; |
| 59 | + t.snapshot(result.stats.passed, 'passed tests'); |
| 60 | + t.snapshot(result.stats.unsavedSnapshots, 'files where snapshots could not be updated'); |
| 61 | + } |
| 62 | +}); |
| 63 | + |
| 64 | +test('snapshots remain if tests run with --match', macro, { |
| 65 | + cwd: exec.cwd('removal'), |
| 66 | + cli: ['--update-snapshots', '--match=\'*snapshot*\''], |
| 67 | + remove: false, |
| 68 | + checkRun: async (t, run) => { |
| 69 | + const result = await t.throwsAsync(run, undefined, 'Expected fixture to throw'); |
| 70 | + t.snapshot(exec.cleanOutput(result.stderr), 'stderr'); |
| 71 | + } |
| 72 | +}); |
| 73 | + |
| 74 | +test('snapshots remain if tests selected by line numbers', macro, { |
| 75 | + cwd: exec.cwd('removal'), |
| 76 | + cli: ['test.js:3-12', '--update-snapshots'], |
| 77 | + remove: false, |
| 78 | + checkRun: async (t, run) => { |
| 79 | + const result = await t.throwsAsync(run, undefined, 'Expected fixture to throw'); |
| 80 | + t.snapshot(exec.cleanOutput(result.stderr), 'stderr'); |
| 81 | + } |
| 82 | +}); |
| 83 | + |
| 84 | +test('snapshots remain if using test.only', macro, { |
| 85 | + cwd: exec.cwd('only-test'), |
| 86 | + cli: ['--update-snapshots'], |
| 87 | + remove: false, |
| 88 | + checkRun: async (t, run) => { |
| 89 | + await t.notThrowsAsync(run, 'Expected fixture not to throw'); |
| 90 | + const result = await run; |
| 91 | + t.snapshot(result.stats.unsavedSnapshots, 'files where snapshots could not be updated'); |
| 92 | + } |
| 93 | +}); |
| 94 | + |
| 95 | +test('snapshots remain if tests are skipped', macro, { |
| 96 | + cwd: exec.cwd('skipped-tests'), |
| 97 | + cli: ['--update-snapshots'], |
| 98 | + remove: false, |
| 99 | + checkRun: async (t, run) => { |
| 100 | + await t.notThrowsAsync(run, 'Expected fixture not to throw'); |
| 101 | + const result = await run; |
| 102 | + t.snapshot(result.stats.unsavedSnapshots, 'files where snapshots could not be updated'); |
| 103 | + } |
| 104 | +}); |
| 105 | + |
| 106 | +test('snapshots remain if snapshot assertions are skipped', macro, { |
| 107 | + cwd: exec.cwd('skipped-snapshots'), |
| 108 | + cli: ['--update-snapshots'], |
| 109 | + remove: false, |
| 110 | + checkRun: async (t, run) => { |
| 111 | + const result = await t.throwsAsync(run, { |
| 112 | + message: /Snapshot assertions cannot be skipped when updating snapshots/ |
| 113 | + }, 'Expected fixture to throw'); |
| 114 | + t.snapshot(result.stats.unsavedSnapshots, 'files where snapshots could not be updated'); |
| 115 | + } |
| 116 | +}); |
| 117 | + |
| 118 | +test('snapshots remain if used in a discarded try()', macro, { |
| 119 | + cwd: exec.cwd('try'), |
| 120 | + cli: ['--update-snapshots'], |
| 121 | + remove: false |
| 122 | +}); |
| 123 | + |
| 124 | +test('snapshots remain if skipped in a discarded try()', macro, { |
| 125 | + cwd: exec.cwd('skipped-snapshots-in-try'), |
| 126 | + cli: ['--update-snapshots'], |
| 127 | + remove: false, |
| 128 | + checkRun: async (t, run) => { |
| 129 | + await t.notThrowsAsync(run, 'Expected fixture not to throw'); |
| 130 | + const result = await run; |
| 131 | + t.snapshot(result.stats.unsavedSnapshots, 'files where snapshots could not be updated'); |
| 132 | + } |
| 133 | +}); |
0 commit comments