From bc722d50c61d93fce7f068cb70754330207bbbf5 Mon Sep 17 00:00:00 2001 From: ninevra Date: Sun, 6 Dec 2020 23:23:15 -0800 Subject: [PATCH 01/34] Test snapshot removal and edge cases Test with crashing file Test with failing tests Test with test.failing() Test with skipped hooks Test with esmodule Test with t.try() Test with a fixed snapshot location Test with a test directory --- test/snapshot-removal/crashing-file-esmod.js | 17 +++++ test/snapshot-removal/crashing-file.js | 12 ++++ test/snapshot-removal/crashing-test.js | 12 ++++ test/snapshot-removal/failing-assertion.js | 12 ++++ test/snapshot-removal/fixed-snapshot-dir.js | 12 ++++ .../fixtures/crashing-file-esmod/package.json | 3 + .../fixtures/crashing-file-esmod/test.js | 29 +++++++++ .../fixtures/crashing-file/package.json | 1 + .../fixtures/crashing-file/test.js | 29 +++++++++ .../fixtures/crashing-test/package.json | 1 + .../fixtures/crashing-test/test.js | 28 +++++++++ .../fixtures/failing-assertion/package.json | 1 + .../fixtures/failing-assertion/test.js | 27 ++++++++ .../fixtures/fixed-snapshot-dir/package.json | 5 ++ .../fixtures/fixed-snapshot-dir/test.js | 29 +++++++++ .../marked-failing-test-passes/package.json | 1 + .../marked-failing-test-passes/test.js | 27 ++++++++ .../fixtures/marked-failing-test/package.json | 1 + .../fixtures/marked-failing-test/test.js | 27 ++++++++ .../fixtures/only-test/package.json | 1 + .../fixtures/only-test/test.js | 27 ++++++++ .../fixtures/removal/package.json | 1 + .../snapshot-removal/fixtures/removal/test.js | 29 +++++++++ .../fixtures/skipped-hooks/package.json | 1 + .../fixtures/skipped-hooks/test.js | 27 ++++++++ .../fixtures/skipped-snapshots/package.json | 1 + .../fixtures/skipped-snapshots/test.js | 29 +++++++++ .../fixtures/skipped-tests/package.json | 1 + .../fixtures/skipped-tests/test.js | 29 +++++++++ .../fixtures/snapshot-dir/package.json | 1 + .../fixtures/snapshot-dir/test/test.js | 29 +++++++++ .../fixtures/try/package.json | 1 + test/snapshot-removal/fixtures/try/test.js | 27 ++++++++ test/snapshot-removal/helpers/macros.js | 62 +++++++++++++++++++ .../marked-failing-test-passes.js | 13 ++++ test/snapshot-removal/marked-failing-test.js | 9 +++ test/snapshot-removal/only-test.js | 9 +++ test/snapshot-removal/removal.js | 33 ++++++++++ test/snapshot-removal/skipped-hooks.js | 9 +++ test/snapshot-removal/skipped-snapshots.js | 18 ++++++ test/snapshot-removal/skipped-tests.js | 9 +++ test/snapshot-removal/snapshot-dir.js | 12 ++++ test/snapshot-removal/try.js | 10 +++ 43 files changed, 662 insertions(+) create mode 100644 test/snapshot-removal/crashing-file-esmod.js create mode 100644 test/snapshot-removal/crashing-file.js create mode 100644 test/snapshot-removal/crashing-test.js create mode 100644 test/snapshot-removal/failing-assertion.js create mode 100644 test/snapshot-removal/fixed-snapshot-dir.js create mode 100644 test/snapshot-removal/fixtures/crashing-file-esmod/package.json create mode 100644 test/snapshot-removal/fixtures/crashing-file-esmod/test.js create mode 100644 test/snapshot-removal/fixtures/crashing-file/package.json create mode 100644 test/snapshot-removal/fixtures/crashing-file/test.js create mode 100644 test/snapshot-removal/fixtures/crashing-test/package.json create mode 100644 test/snapshot-removal/fixtures/crashing-test/test.js create mode 100644 test/snapshot-removal/fixtures/failing-assertion/package.json create mode 100644 test/snapshot-removal/fixtures/failing-assertion/test.js create mode 100644 test/snapshot-removal/fixtures/fixed-snapshot-dir/package.json create mode 100644 test/snapshot-removal/fixtures/fixed-snapshot-dir/test.js create mode 100644 test/snapshot-removal/fixtures/marked-failing-test-passes/package.json create mode 100644 test/snapshot-removal/fixtures/marked-failing-test-passes/test.js create mode 100644 test/snapshot-removal/fixtures/marked-failing-test/package.json create mode 100644 test/snapshot-removal/fixtures/marked-failing-test/test.js create mode 100644 test/snapshot-removal/fixtures/only-test/package.json create mode 100644 test/snapshot-removal/fixtures/only-test/test.js create mode 100644 test/snapshot-removal/fixtures/removal/package.json create mode 100644 test/snapshot-removal/fixtures/removal/test.js create mode 100644 test/snapshot-removal/fixtures/skipped-hooks/package.json create mode 100644 test/snapshot-removal/fixtures/skipped-hooks/test.js create mode 100644 test/snapshot-removal/fixtures/skipped-snapshots/package.json create mode 100644 test/snapshot-removal/fixtures/skipped-snapshots/test.js create mode 100644 test/snapshot-removal/fixtures/skipped-tests/package.json create mode 100644 test/snapshot-removal/fixtures/skipped-tests/test.js create mode 100644 test/snapshot-removal/fixtures/snapshot-dir/package.json create mode 100644 test/snapshot-removal/fixtures/snapshot-dir/test/test.js create mode 100644 test/snapshot-removal/fixtures/try/package.json create mode 100644 test/snapshot-removal/fixtures/try/test.js create mode 100644 test/snapshot-removal/helpers/macros.js create mode 100644 test/snapshot-removal/marked-failing-test-passes.js create mode 100644 test/snapshot-removal/marked-failing-test.js create mode 100644 test/snapshot-removal/only-test.js create mode 100644 test/snapshot-removal/removal.js create mode 100644 test/snapshot-removal/skipped-hooks.js create mode 100644 test/snapshot-removal/skipped-snapshots.js create mode 100644 test/snapshot-removal/skipped-tests.js create mode 100644 test/snapshot-removal/snapshot-dir.js create mode 100644 test/snapshot-removal/try.js diff --git a/test/snapshot-removal/crashing-file-esmod.js b/test/snapshot-removal/crashing-file-esmod.js new file mode 100644 index 000000000..b70426774 --- /dev/null +++ b/test/snapshot-removal/crashing-file-esmod.js @@ -0,0 +1,17 @@ +const test = require('@ava/test'); +const exec = require('../helpers/exec'); +const path = require('path'); +const {testSnapshotPruning} = require('./helpers/macros'); + +const testIfESM = process.version.slice(1) >= '12.17' ? + test.serial : + test.serial.skip; + +testIfESM('snapshots remain if esmodule test file crashes during declaration', testSnapshotPruning, { + cwd: exec.cwd(path.parse(__filename).name), + cli: ['--update-snapshots'], + remove: false, + error: { + message: /.*Uncaught exception in test\.js.*Crashing during test declaration.*1 uncaught exception.*/s + } +}); diff --git a/test/snapshot-removal/crashing-file.js b/test/snapshot-removal/crashing-file.js new file mode 100644 index 000000000..8d8c9c6bb --- /dev/null +++ b/test/snapshot-removal/crashing-file.js @@ -0,0 +1,12 @@ +const test = require('@ava/test'); +const exec = require('../helpers/exec'); +const {testSnapshotPruning} = require('./helpers/macros'); + +test.serial('snapshots remain if test file crashes during declaration', testSnapshotPruning, { + cwd: exec.cwd('crashing-file'), + cli: ['--update-snapshots'], + remove: false, + error: { + message: /.*Uncaught exception in test\.js.*Crashing during test declaration.*1 uncaught exception.*/s + } +}); diff --git a/test/snapshot-removal/crashing-test.js b/test/snapshot-removal/crashing-test.js new file mode 100644 index 000000000..37a5f8f0f --- /dev/null +++ b/test/snapshot-removal/crashing-test.js @@ -0,0 +1,12 @@ +const test = require('@ava/test'); +const exec = require('../helpers/exec'); +const {testSnapshotPruning} = require('./helpers/macros'); + +test.serial('snapshots remain if tests crash', testSnapshotPruning, { + cwd: exec.cwd('crashing-test'), + cli: ['--update-snapshots'], + remove: false, + error: { + message: /.*Error thrown in test.*/ + } +}); diff --git a/test/snapshot-removal/failing-assertion.js b/test/snapshot-removal/failing-assertion.js new file mode 100644 index 000000000..b801ccbae --- /dev/null +++ b/test/snapshot-removal/failing-assertion.js @@ -0,0 +1,12 @@ +const test = require('@ava/test'); +const exec = require('../helpers/exec'); +const {testSnapshotPruning} = require('./helpers/macros'); + +test.serial('snapshots remain if tests fail', testSnapshotPruning, { + cwd: exec.cwd('failing-assertion'), + cli: ['--update-snapshots'], + remove: false, + error: { + message: /.*Value is not truthy.*/ + } +}); diff --git a/test/snapshot-removal/fixed-snapshot-dir.js b/test/snapshot-removal/fixed-snapshot-dir.js new file mode 100644 index 000000000..061734a50 --- /dev/null +++ b/test/snapshot-removal/fixed-snapshot-dir.js @@ -0,0 +1,12 @@ +const test = require('@ava/test'); +const exec = require('../helpers/exec'); +const {testSnapshotPruning} = require('./helpers/macros'); +const path = require('path'); + +test.serial('snapshots are removed from a custom snapshotDir', testSnapshotPruning, { + cwd: exec.cwd(path.parse(__filename).name), + cli: ['--update-snapshots'], + remove: true, + snapshotPath: path.join('fixedSnapshotDir', 'test.js.snap'), + reportPath: path.join('fixedSnapshotDir', 'test.js.md') +}); diff --git a/test/snapshot-removal/fixtures/crashing-file-esmod/package.json b/test/snapshot-removal/fixtures/crashing-file-esmod/package.json new file mode 100644 index 000000000..3dbc1ca59 --- /dev/null +++ b/test/snapshot-removal/fixtures/crashing-file-esmod/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/test/snapshot-removal/fixtures/crashing-file-esmod/test.js b/test/snapshot-removal/fixtures/crashing-file-esmod/test.js new file mode 100644 index 000000000..2a63bf4ec --- /dev/null +++ b/test/snapshot-removal/fixtures/crashing-file-esmod/test.js @@ -0,0 +1,29 @@ +/* eslint-disable capitalized-comments, ava/no-identical-title, no-unreachable */ + +import test from 'ava'; + +if (process.env.TEMPLATE) { + test('some snapshots', t => { + t.snapshot('foo'); + t.snapshot('bar'); + t.assert(true); + }); + + test('another snapshot', t => { + t.snapshot('baz'); + t.assert(true); + }); +} else { + test('some snapshots', t => { + // t.snapshot('foo'); + // t.snapshot('bar'); + t.assert(true); + }); + + throw new Error('Crashing during test declaration.'); + + test('another snapshot', t => { + // t.snapshot('baz'); + t.assert(true); + }); +} diff --git a/test/snapshot-removal/fixtures/crashing-file/package.json b/test/snapshot-removal/fixtures/crashing-file/package.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/test/snapshot-removal/fixtures/crashing-file/package.json @@ -0,0 +1 @@ +{} diff --git a/test/snapshot-removal/fixtures/crashing-file/test.js b/test/snapshot-removal/fixtures/crashing-file/test.js new file mode 100644 index 000000000..79e238d75 --- /dev/null +++ b/test/snapshot-removal/fixtures/crashing-file/test.js @@ -0,0 +1,29 @@ +/* eslint-disable capitalized-comments, ava/no-identical-title, no-unreachable */ + +const test = require('ava'); + +if (process.env.TEMPLATE) { + test('some snapshots', t => { + t.snapshot('foo'); + t.snapshot('bar'); + t.assert(true); + }); + + test('another snapshot', t => { + t.snapshot('baz'); + t.assert(true); + }); +} else { + test('some snapshots', t => { + // t.snapshot('foo'); + // t.snapshot('bar'); + t.assert(true); + }); + + throw new Error('Crashing during test declaration.'); + + test('another snapshot', t => { + // t.snapshot('baz'); + t.assert(true); + }); +} diff --git a/test/snapshot-removal/fixtures/crashing-test/package.json b/test/snapshot-removal/fixtures/crashing-test/package.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/test/snapshot-removal/fixtures/crashing-test/package.json @@ -0,0 +1 @@ +{} diff --git a/test/snapshot-removal/fixtures/crashing-test/test.js b/test/snapshot-removal/fixtures/crashing-test/test.js new file mode 100644 index 000000000..e63d9e316 --- /dev/null +++ b/test/snapshot-removal/fixtures/crashing-test/test.js @@ -0,0 +1,28 @@ +/* eslint-disable capitalized-comments, ava/no-identical-title */ + +const test = require('ava'); + +if (process.env.TEMPLATE) { + test('some snapshots', t => { + t.snapshot('foo'); + t.snapshot('bar'); + t.assert(true); + }); + + test('another snapshot', t => { + t.snapshot('baz'); + t.assert(true); + }); +} else { + test('some snapshots', t => { + // t.snapshot('foo'); + // t.snapshot('bar'); + t.assert(true); + }); + + test('another snapshot', t => { + // t.snapshot('baz'); + t.assert(true); + throw new Error('Crashing in test.'); + }); +} diff --git a/test/snapshot-removal/fixtures/failing-assertion/package.json b/test/snapshot-removal/fixtures/failing-assertion/package.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/test/snapshot-removal/fixtures/failing-assertion/package.json @@ -0,0 +1 @@ +{} diff --git a/test/snapshot-removal/fixtures/failing-assertion/test.js b/test/snapshot-removal/fixtures/failing-assertion/test.js new file mode 100644 index 000000000..167bc9894 --- /dev/null +++ b/test/snapshot-removal/fixtures/failing-assertion/test.js @@ -0,0 +1,27 @@ +/* eslint-disable capitalized-comments, ava/no-identical-title */ + +const test = require('ava'); + +if (process.env.TEMPLATE) { + test('some snapshots', t => { + t.snapshot('foo'); + t.snapshot('bar'); + t.assert(true); + }); + + test('another snapshot', t => { + t.snapshot('baz'); + t.assert(true); + }); +} else { + test('some snapshots', t => { + // t.snapshot('foo'); + // t.snapshot('bar'); + t.assert(true); + }); + + test('another snapshot', t => { + // t.snapshot('baz'); + t.assert(false); + }); +} diff --git a/test/snapshot-removal/fixtures/fixed-snapshot-dir/package.json b/test/snapshot-removal/fixtures/fixed-snapshot-dir/package.json new file mode 100644 index 000000000..6aa55350f --- /dev/null +++ b/test/snapshot-removal/fixtures/fixed-snapshot-dir/package.json @@ -0,0 +1,5 @@ +{ + "ava": { + "snapshotDir": "fixedSnapshotDir" + } +} diff --git a/test/snapshot-removal/fixtures/fixed-snapshot-dir/test.js b/test/snapshot-removal/fixtures/fixed-snapshot-dir/test.js new file mode 100644 index 000000000..e3999d0db --- /dev/null +++ b/test/snapshot-removal/fixtures/fixed-snapshot-dir/test.js @@ -0,0 +1,29 @@ +/* eslint-disable capitalized-comments, ava/no-identical-title */ + +if (process.env.TEMPLATE) { + const test = require('ava'); + + test('some snapshots', t => { + t.snapshot('foo'); + t.snapshot('bar'); + t.assert(true); + }); + + test('another snapshot', t => { + t.snapshot('baz'); + t.assert(true); + }); +} else { + const test = require('ava'); + + test('some snapshots', t => { + // t.snapshot('foo'); + // t.snapshot('bar'); + t.assert(true); + }); + + test('another snapshot', t => { + // t.snapshot('baz'); + t.assert(true); + }); +} diff --git a/test/snapshot-removal/fixtures/marked-failing-test-passes/package.json b/test/snapshot-removal/fixtures/marked-failing-test-passes/package.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/test/snapshot-removal/fixtures/marked-failing-test-passes/package.json @@ -0,0 +1 @@ +{} diff --git a/test/snapshot-removal/fixtures/marked-failing-test-passes/test.js b/test/snapshot-removal/fixtures/marked-failing-test-passes/test.js new file mode 100644 index 000000000..578bbb0ba --- /dev/null +++ b/test/snapshot-removal/fixtures/marked-failing-test-passes/test.js @@ -0,0 +1,27 @@ +/* eslint-disable capitalized-comments, ava/no-identical-title */ + +const test = require('ava'); + +if (process.env.TEMPLATE) { + test('some snapshots', t => { + t.snapshot('foo'); + t.snapshot('bar'); + t.assert(true); + }); + + test.failing('another snapshot', t => { + t.snapshot('baz'); + t.assert(false); + }); +} else { + test('some snapshots', t => { + // t.snapshot('foo'); + // t.snapshot('bar'); + t.assert(true); + }); + + test.failing('another snapshot', t => { + // t.snapshot('baz'); + t.assert(true); + }); +} diff --git a/test/snapshot-removal/fixtures/marked-failing-test/package.json b/test/snapshot-removal/fixtures/marked-failing-test/package.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/test/snapshot-removal/fixtures/marked-failing-test/package.json @@ -0,0 +1 @@ +{} diff --git a/test/snapshot-removal/fixtures/marked-failing-test/test.js b/test/snapshot-removal/fixtures/marked-failing-test/test.js new file mode 100644 index 000000000..bd58d4c9a --- /dev/null +++ b/test/snapshot-removal/fixtures/marked-failing-test/test.js @@ -0,0 +1,27 @@ +/* eslint-disable capitalized-comments, ava/no-identical-title */ + +const test = require('ava'); + +if (process.env.TEMPLATE) { + test('some snapshots', t => { + t.snapshot('foo'); + t.snapshot('bar'); + t.assert(true); + }); + + test.failing('another snapshot', t => { + t.snapshot('baz'); + t.assert(false); + }); +} else { + test('some snapshots', t => { + // t.snapshot('foo'); + // t.snapshot('bar'); + t.assert(true); + }); + + test.failing('another snapshot', t => { + // t.snapshot('baz'); + t.assert(false); + }); +} diff --git a/test/snapshot-removal/fixtures/only-test/package.json b/test/snapshot-removal/fixtures/only-test/package.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/test/snapshot-removal/fixtures/only-test/package.json @@ -0,0 +1 @@ +{} diff --git a/test/snapshot-removal/fixtures/only-test/test.js b/test/snapshot-removal/fixtures/only-test/test.js new file mode 100644 index 000000000..6a4f7e4f0 --- /dev/null +++ b/test/snapshot-removal/fixtures/only-test/test.js @@ -0,0 +1,27 @@ +/* eslint-disable capitalized-comments, ava/no-only-test, ava/no-identical-title */ + +const test = require('ava'); + +if (process.env.TEMPLATE) { + test('some snapshots', t => { + t.snapshot('foo'); + t.snapshot('bar'); + t.assert(true); + }); + + test('another snapshot', t => { + t.snapshot('baz'); + t.assert(true); + }); +} else { + test.only('some snapshots', t => { + // t.snapshot('foo'); + // t.snapshot('bar'); + t.assert(true); + }); + + test('another snapshot', t => { + // t.snapshot('baz'); + t.assert(true); + }); +} diff --git a/test/snapshot-removal/fixtures/removal/package.json b/test/snapshot-removal/fixtures/removal/package.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/test/snapshot-removal/fixtures/removal/package.json @@ -0,0 +1 @@ +{} diff --git a/test/snapshot-removal/fixtures/removal/test.js b/test/snapshot-removal/fixtures/removal/test.js new file mode 100644 index 000000000..e3999d0db --- /dev/null +++ b/test/snapshot-removal/fixtures/removal/test.js @@ -0,0 +1,29 @@ +/* eslint-disable capitalized-comments, ava/no-identical-title */ + +if (process.env.TEMPLATE) { + const test = require('ava'); + + test('some snapshots', t => { + t.snapshot('foo'); + t.snapshot('bar'); + t.assert(true); + }); + + test('another snapshot', t => { + t.snapshot('baz'); + t.assert(true); + }); +} else { + const test = require('ava'); + + test('some snapshots', t => { + // t.snapshot('foo'); + // t.snapshot('bar'); + t.assert(true); + }); + + test('another snapshot', t => { + // t.snapshot('baz'); + t.assert(true); + }); +} diff --git a/test/snapshot-removal/fixtures/skipped-hooks/package.json b/test/snapshot-removal/fixtures/skipped-hooks/package.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/test/snapshot-removal/fixtures/skipped-hooks/package.json @@ -0,0 +1 @@ +{} diff --git a/test/snapshot-removal/fixtures/skipped-hooks/test.js b/test/snapshot-removal/fixtures/skipped-hooks/test.js new file mode 100644 index 000000000..5cbdf2522 --- /dev/null +++ b/test/snapshot-removal/fixtures/skipped-hooks/test.js @@ -0,0 +1,27 @@ +/* eslint-disable capitalized-comments, ava/no-identical-title, no-unused-vars, ava/no-skip-test */ + +if (process.env.TEMPLATE) { + const test = require('ava'); + + test.afterEach.always(t => { + t.snapshot('Hello, world!'); + }); + + test('some snapshots', t => { + t.snapshot('foo'); + t.snapshot('bar'); + t.assert(true); + }); +} else { + const test = require('ava'); + + test.afterEach.always.skip(t => { + // t.snapshot('Hello, world!'); + }); + + test('some snapshots', t => { + // t.snapshot('foo'); + // t.snapshot('bar'); + t.assert(true); + }); +} diff --git a/test/snapshot-removal/fixtures/skipped-snapshots/package.json b/test/snapshot-removal/fixtures/skipped-snapshots/package.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/test/snapshot-removal/fixtures/skipped-snapshots/package.json @@ -0,0 +1 @@ +{} diff --git a/test/snapshot-removal/fixtures/skipped-snapshots/test.js b/test/snapshot-removal/fixtures/skipped-snapshots/test.js new file mode 100644 index 000000000..da5e27974 --- /dev/null +++ b/test/snapshot-removal/fixtures/skipped-snapshots/test.js @@ -0,0 +1,29 @@ +/* eslint-disable ava/no-skip-assert, capitalized-comments, ava/no-identical-title */ + +if (process.env.TEMPLATE) { + const test = require('ava'); + + test('some snapshots', t => { + t.snapshot('foo'); + t.snapshot('bar'); + t.assert(true); + }); + + test('another snapshot', t => { + t.snapshot('baz'); + t.assert(true); + }); +} else { + const test = require('ava'); + + test('some snapshots', t => { + t.snapshot.skip('foo'); + // t.snapshot('bar'); + t.assert(true); + }); + + test('another snapshot', t => { + // t.snapshot('baz'); + t.assert(true); + }); +} diff --git a/test/snapshot-removal/fixtures/skipped-tests/package.json b/test/snapshot-removal/fixtures/skipped-tests/package.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/test/snapshot-removal/fixtures/skipped-tests/package.json @@ -0,0 +1 @@ +{} diff --git a/test/snapshot-removal/fixtures/skipped-tests/test.js b/test/snapshot-removal/fixtures/skipped-tests/test.js new file mode 100644 index 000000000..86d9495f6 --- /dev/null +++ b/test/snapshot-removal/fixtures/skipped-tests/test.js @@ -0,0 +1,29 @@ +/* eslint-disable ava/no-skip-test, capitalized-comments, ava/no-identical-title */ + +if (process.env.TEMPLATE) { + const test = require('ava'); + + test('some snapshots', t => { + t.snapshot('foo'); + t.snapshot('bar'); + t.assert(true); + }); + + test('another snapshot', t => { + t.snapshot('baz'); + t.assert(true); + }); +} else { + const test = require('ava'); + + test.skip('some snapshots', t => { + t.snapshot('foo'); + t.snapshot('bar'); + t.assert(true); + }); + + test('another snapshot', t => { + // t.snapshot('baz'); + t.assert(true); + }); +} diff --git a/test/snapshot-removal/fixtures/snapshot-dir/package.json b/test/snapshot-removal/fixtures/snapshot-dir/package.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/test/snapshot-removal/fixtures/snapshot-dir/package.json @@ -0,0 +1 @@ +{} diff --git a/test/snapshot-removal/fixtures/snapshot-dir/test/test.js b/test/snapshot-removal/fixtures/snapshot-dir/test/test.js new file mode 100644 index 000000000..e3999d0db --- /dev/null +++ b/test/snapshot-removal/fixtures/snapshot-dir/test/test.js @@ -0,0 +1,29 @@ +/* eslint-disable capitalized-comments, ava/no-identical-title */ + +if (process.env.TEMPLATE) { + const test = require('ava'); + + test('some snapshots', t => { + t.snapshot('foo'); + t.snapshot('bar'); + t.assert(true); + }); + + test('another snapshot', t => { + t.snapshot('baz'); + t.assert(true); + }); +} else { + const test = require('ava'); + + test('some snapshots', t => { + // t.snapshot('foo'); + // t.snapshot('bar'); + t.assert(true); + }); + + test('another snapshot', t => { + // t.snapshot('baz'); + t.assert(true); + }); +} diff --git a/test/snapshot-removal/fixtures/try/package.json b/test/snapshot-removal/fixtures/try/package.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/test/snapshot-removal/fixtures/try/package.json @@ -0,0 +1 @@ +{} diff --git a/test/snapshot-removal/fixtures/try/test.js b/test/snapshot-removal/fixtures/try/test.js new file mode 100644 index 000000000..7efa546dd --- /dev/null +++ b/test/snapshot-removal/fixtures/try/test.js @@ -0,0 +1,27 @@ +/* eslint-disable ava/no-identical-title */ + +if (process.env.TEMPLATE) { + const test = require('ava'); + + test('snapshots in try', async t => { + const attempt = await t.try(tt => { + tt.snapshot('in try'); + }); + + attempt.commit(); + + t.assert(true); + }); +} else { + const test = require('ava'); + + test('snapshots in try', async t => { + const attempt = await t.try(tt => { + tt.snapshot('in try'); + }); + + attempt.discard(); + + t.assert(true); + }); +} diff --git a/test/snapshot-removal/helpers/macros.js b/test/snapshot-removal/helpers/macros.js new file mode 100644 index 000000000..6d8460009 --- /dev/null +++ b/test/snapshot-removal/helpers/macros.js @@ -0,0 +1,62 @@ +const fs = require('fs').promises; +const exec = require('../../helpers/exec'); +const path = require('path'); + +async function testSnapshotPruning(t, { + cwd, + ci = 'not-ci', + cli, + remove, + error = false, + snapshotPath = 'test.js.snap', + reportPath = 'test.js.md' +}) { + snapshotPath = path.join(cwd, snapshotPath); + reportPath = path.join(cwd, reportPath); + + t.teardown(async () => { + try { + await fs.unlink(snapshotPath); + await fs.unlink(reportPath); + } catch {} + }); + + // Execute fixture as template to generate snapshots + const templateResult = exec.fixture(['--update-snapshots'], { + cwd, + env: { + AVA_FORCE_CI: 'not-ci', + TEMPLATE: 'true' + } + }); + + await t.notThrowsAsync(templateResult, 'Template crashed - there\'s a bug in the test'); + + // Check that the snapshots were created + await t.notThrowsAsync(fs.access(snapshotPath), 'Template didn\'t create a snapshot - there\'s a bug in the test'); + await t.notThrowsAsync(fs.access(reportPath), 'Template didn\'t create a report - there\'s a bug in the test'); + + // Execute fixture as run + const runResult = exec.fixture(cli, { + cwd, + env: { + AVA_FORCE_CI: ci + } + }); + + await (error ? + t.throwsAsync(runResult, error === true ? undefined : error, 'Expected fixture to throw.') : + t.notThrowsAsync(runResult, 'Expected fixture not to throw.')); + + if (remove) { + // Assert files don't exist + await t.throwsAsync(fs.access(snapshotPath), {code: 'ENOENT'}, 'Expected snapshot to be removed'); + await t.throwsAsync(fs.access(reportPath), {code: 'ENOENT'}, 'Expected report to be remove'); + } else { + // Assert files exist + await t.notThrowsAsync(fs.access(snapshotPath), 'Expected snapshot not to be removed'); + await t.notThrowsAsync(fs.access(reportPath), 'Expected report not to be removed'); + } +} + +module.exports.testSnapshotPruning = testSnapshotPruning; diff --git a/test/snapshot-removal/marked-failing-test-passes.js b/test/snapshot-removal/marked-failing-test-passes.js new file mode 100644 index 000000000..1e4e54195 --- /dev/null +++ b/test/snapshot-removal/marked-failing-test-passes.js @@ -0,0 +1,13 @@ +const test = require('@ava/test'); +const exec = require('../helpers/exec'); +const {testSnapshotPruning} = require('./helpers/macros'); +const path = require('path'); + +test.serial('snapshots remain if any test.failing() passes', testSnapshotPruning, { + cwd: exec.cwd(path.parse(__filename).name), + cli: ['--update-snapshots'], + remove: false, + error: { + message: /.*Test was expected to fail, but succeeded.*/ + } +}); diff --git a/test/snapshot-removal/marked-failing-test.js b/test/snapshot-removal/marked-failing-test.js new file mode 100644 index 000000000..7027decc1 --- /dev/null +++ b/test/snapshot-removal/marked-failing-test.js @@ -0,0 +1,9 @@ +const test = require('@ava/test'); +const exec = require('../helpers/exec'); +const {testSnapshotPruning} = require('./helpers/macros'); + +test.serial('snapshots remain if any test.failing()', testSnapshotPruning, { + cwd: exec.cwd('marked-failing-test'), + cli: ['--update-snapshots'], + remove: false +}); diff --git a/test/snapshot-removal/only-test.js b/test/snapshot-removal/only-test.js new file mode 100644 index 000000000..cd54f8943 --- /dev/null +++ b/test/snapshot-removal/only-test.js @@ -0,0 +1,9 @@ +const test = require('@ava/test'); +const exec = require('../helpers/exec'); +const {testSnapshotPruning} = require('./helpers/macros'); + +test.serial('snapshots remain if using test.only', testSnapshotPruning, { + cwd: exec.cwd('only-test'), + cli: ['--update-snapshots'], + remove: false +}); diff --git a/test/snapshot-removal/removal.js b/test/snapshot-removal/removal.js new file mode 100644 index 000000000..fb2f0fbc1 --- /dev/null +++ b/test/snapshot-removal/removal.js @@ -0,0 +1,33 @@ +const test = require('@ava/test'); +const exec = require('../helpers/exec'); +const {testSnapshotPruning} = require('./helpers/macros'); + +test.serial('snapshots are removed when tests stop using them', testSnapshotPruning, { + cwd: exec.cwd('removal'), + cli: ['--update-snapshots'], + remove: true +}); + +test.serial('snapshots remain if not updating', testSnapshotPruning, { + cwd: exec.cwd('removal'), + cli: [], + remove: false +}); + +test.serial('snapshots remain if tests run with --match', testSnapshotPruning, { + cwd: exec.cwd('removal'), + cli: ['--update-snapshots', '--match=\'*snapshot*\''], + remove: false, + error: { + message: /.*Snapshots cannot be updated when matching specific tests.*/ + } +}); + +test.serial('snapshots remain if tests selected by line numbers', testSnapshotPruning, { + cwd: exec.cwd('removal'), + cli: ['test.js:3-12', '--update-snapshots'], + remove: false, + error: { + message: /.*Snapshots cannot be updated when selecting specific tests by their line number.*/ + } +}); diff --git a/test/snapshot-removal/skipped-hooks.js b/test/snapshot-removal/skipped-hooks.js new file mode 100644 index 000000000..294bf98ab --- /dev/null +++ b/test/snapshot-removal/skipped-hooks.js @@ -0,0 +1,9 @@ +const test = require('@ava/test'); +const exec = require('../helpers/exec'); +const {testSnapshotPruning} = require('./helpers/macros'); + +test.serial('snapshots remain if hooks are skipped', testSnapshotPruning, { + cwd: exec.cwd('skipped-hooks'), + cli: ['--update-snapshots'], + remove: false +}); diff --git a/test/snapshot-removal/skipped-snapshots.js b/test/snapshot-removal/skipped-snapshots.js new file mode 100644 index 000000000..6374d98a2 --- /dev/null +++ b/test/snapshot-removal/skipped-snapshots.js @@ -0,0 +1,18 @@ +const test = require('@ava/test'); +const exec = require('../helpers/exec'); +const {testSnapshotPruning} = require('./helpers/macros'); + +test.serial('snapshots remain if snapshot assertions are skipped (-u)', testSnapshotPruning, { + cwd: exec.cwd('skipped-snapshots'), + cli: ['--update-snapshots'], + remove: false, + error: { + message: /.*Snapshot assertions cannot be skipped when updating snapshots.*/ + } +}); + +test.serial('snapshots remain if snapshot assertions are skipped (!-u)', testSnapshotPruning, { + cwd: exec.cwd('skipped-snapshots'), + cli: [], + remove: false +}); diff --git a/test/snapshot-removal/skipped-tests.js b/test/snapshot-removal/skipped-tests.js new file mode 100644 index 000000000..abaa52f3e --- /dev/null +++ b/test/snapshot-removal/skipped-tests.js @@ -0,0 +1,9 @@ +const test = require('@ava/test'); +const exec = require('../helpers/exec'); +const {testSnapshotPruning} = require('./helpers/macros'); + +test.serial('snapshots remain if tests are skipped', testSnapshotPruning, { + cwd: exec.cwd('skipped-tests'), + cli: ['--update-snapshots'], + remove: false +}); diff --git a/test/snapshot-removal/snapshot-dir.js b/test/snapshot-removal/snapshot-dir.js new file mode 100644 index 000000000..ba1a1af3c --- /dev/null +++ b/test/snapshot-removal/snapshot-dir.js @@ -0,0 +1,12 @@ +const test = require('@ava/test'); +const exec = require('../helpers/exec'); +const {testSnapshotPruning} = require('./helpers/macros'); +const path = require('path'); + +test.serial('snapshots are removed from a snapshot directory', testSnapshotPruning, { + cwd: exec.cwd(path.parse(__filename).name), + cli: ['--update-snapshots'], + remove: true, + snapshotPath: path.join('test', 'snapshots', 'test.js.snap'), + reportPath: path.join('test', 'snapshots', 'test.js.md') +}); diff --git a/test/snapshot-removal/try.js b/test/snapshot-removal/try.js new file mode 100644 index 000000000..46f9fcb55 --- /dev/null +++ b/test/snapshot-removal/try.js @@ -0,0 +1,10 @@ +const test = require('@ava/test'); +const exec = require('../helpers/exec'); +const {testSnapshotPruning} = require('./helpers/macros'); +const path = require('path'); + +test.serial('snapshots remain if used in a discarded try()', testSnapshotPruning, { + cwd: exec.cwd(path.parse(__filename).name), + cli: ['--update-snapshots'], + remove: false +}); From b55285affdaa04ab7c91efe4b58aa6cfb1dd1d48 Mon Sep 17 00:00:00 2001 From: ninevra Date: Wed, 9 Dec 2020 17:44:07 -0800 Subject: [PATCH 02/34] no-push: disable crashing lint rule --- xo.config.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/xo.config.js b/xo.config.js index 49c546469..2c526049d 100644 --- a/xo.config.js +++ b/xo.config.js @@ -12,7 +12,8 @@ module.exports = { 'import/no-anonymous-default-export': 'off', 'import/no-unresolved': ['error', {commonjs: true}], 'no-use-extend-native/no-use-extend-native': 'off', - '@typescript-eslint/no-var-requires': 'off' + '@typescript-eslint/no-var-requires': 'off', + 'ava/no-statement-after-end': 'off' }, overrides: [ { From 643678b46f39c032d8281148c3fa05944f62d981 Mon Sep 17 00:00:00 2001 From: ninevra Date: Sun, 6 Dec 2020 15:48:03 -0800 Subject: [PATCH 03/34] Remove snapshot files when tests stop using them Don't prune snapshots if any test or hook failed Crashes can hide snapshot assertions. Assertion failures can prevent hooks from running, hiding snapshot assertions. Could be more precise - check whether any hook actually was skipped because of a test failure. Unclear benefit, more complex. Don't prune if any hooks were skipped Don't prune if any tests marked .failing() Don't prune if the test file crashes --- lib/runner.js | 38 ++++++++++++++++++++++++++++++++++---- lib/snapshot-manager.js | 33 ++++++++++++++++++++++++++++++++- lib/worker/subprocess.js | 9 +++++++-- 3 files changed, 73 insertions(+), 7 deletions(-) diff --git a/lib/runner.js b/lib/runner.js index fe240cb26..7994d30aa 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -24,6 +24,10 @@ class Runner extends Emittery { this.runOnlyExclusive = options.runOnlyExclusive === true; this.serial = options.serial === true; this.skippingTests = false; + this.skippingHooks = false; + this.hasKnownFailingTests = false; + // Set by lib/worker/subprocess.js if the test file fails to load + this.declarationFailed = false; this.snapshotDir = options.snapshotDir; this.updateSnapshots = options.updateSnapshots; @@ -45,6 +49,8 @@ class Runner extends Emittery { }; this.waitForReady = []; + this.anyFailure = false; + const uniqueTestTitles = new Set(); this.registerUniqueTitle = title => { if (uniqueTestTitles.has(title)) { @@ -146,6 +152,12 @@ class Runner extends Emittery { metadata: {...metadata} }; + if (metadata.type !== 'test') { + if (task.metadata.skipped) { + this.skippingHooks = true; + } + } + if (metadata.type === 'test') { if (this.match.length > 0) { // --match overrides .only() @@ -160,6 +172,10 @@ class Runner extends Emittery { this.runOnlyExclusive = true; } + if (task.metadata.failing) { + this.hasKnownFailingTests = true; + } + this.tasks[metadata.serial ? 'serial' : 'concurrent'].push(task); this.emit('stateChange', { type: 'declared-test', @@ -199,7 +215,7 @@ class Runner extends Emittery { return this.snapshots.compare(options); } - saveSnapshotState() { + async saveSnapshotState() { if (this.updateSnapshots && (this.runOnlyExclusive || this.skippingTests)) { return {cannotSave: true}; } @@ -209,9 +225,22 @@ class Runner extends Emittery { } if (this.updateSnapshots) { - // TODO: There may be unused snapshot files if no test caused the - // snapshots to be loaded. Prune them. But not if tests (including hooks!) - // were skipped. Perhaps emit a warning if this occurs? + if ( + !this.snapshots && + !this.anyFailure && + !this.skippingHooks && + !this.skippingTests && + !this.hasKnownFailingTests && + !this.runOnlyExclusive && + !this.checkSelectedByLineNumbers && + !this.declarationFailed + ) { + await snapshotManager.cleanSnapshots({ + file: this.file, + fixedLocation: this.snapshotDir, + projectDir: this.projectDir + }); + } } return {}; @@ -284,6 +313,7 @@ class Runner extends Emittery { const result = await runnable.run(); // If run() throws or rejects then the entire test run crashes, so // onRunComplete() doesn't *have* to be inside a finally. + this.anyFailure = this.anyFailure || !result.passed; this.onRunComplete(runnable); return result; } diff --git a/lib/snapshot-manager.js b/lib/snapshot-manager.js index 119476452..ac96d1dea 100644 --- a/lib/snapshot-manager.js +++ b/lib/snapshot-manager.js @@ -449,12 +449,43 @@ const determineSnapshotDir = mem(({file, fixedLocation, projectDir}) => { exports.determineSnapshotDir = determineSnapshotDir; -function load({file, fixedLocation, projectDir, recordNewSnapshots, updating}) { +function determineSnapshotPaths({file, fixedLocation, projectDir}) { const dir = determineSnapshotDir({file, fixedLocation, projectDir}); const relFile = path.relative(projectDir, resolveSourceFile(file)); const name = path.basename(relFile); const reportFile = `${name}.md`; const snapFile = `${name}.snap`; + + return { + dir, + relFile, + snapFile, + reportFile + }; +} + +// Remove snapshot and report if they exist +async function cleanSnapshots({file, fixedLocation, projectDir}) { + const {dir, snapFile, reportFile} = determineSnapshotPaths({file, fixedLocation, projectDir}); + + async function clean(file) { + try { + await fs.promises.unlink(file); + } catch (error) { + if (error.code !== 'ENOENT') { + throw error; + } + } + } + + await clean(path.join(dir, snapFile)); + await clean(path.join(dir, reportFile)); +} + +exports.cleanSnapshots = cleanSnapshots; + +function load({file, fixedLocation, projectDir, recordNewSnapshots, updating}) { + const {dir, relFile, snapFile, reportFile} = determineSnapshotPaths({file, fixedLocation, projectDir}); const snapPath = path.join(dir, snapFile); let appendOnly = !updating; diff --git a/lib/worker/subprocess.js b/lib/worker/subprocess.js index abd9aabab..36ae15f4d 100644 --- a/lib/worker/subprocess.js +++ b/lib/worker/subprocess.js @@ -93,7 +93,7 @@ ipc.options.then(async options => { runner.on('finish', async () => { try { - const {cannotSave, touchedFiles} = runner.saveSnapshotState(); + const {cannotSave, touchedFiles} = await runner.saveSnapshotState(); if (cannotSave) { ipc.send({type: 'snapshot-error'}); } else if (touchedFiles) { @@ -241,7 +241,12 @@ ipc.options.then(async options => { } } - await load(testPath); + try { + await load(testPath); + } catch (error) { + runner.declarationFailed = true; + throw error; + } if (accessedRunner) { // Unreference the IPC channel if the test file required AVA. This stops it From 5e9035cdec607a526c9375f9033ab20966ac22c6 Mon Sep 17 00:00:00 2001 From: ninevra Date: Fri, 11 Dec 2020 18:16:51 -0800 Subject: [PATCH 04/34] Test with watcher --- .../watcher-rerun-unlink/package.json | 1 + .../snapshots/watcher-rerun-unlink/test.js | 31 +++++++++++++ test-tap/integration/watcher.js | 46 +++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 test-tap/fixture/snapshots/watcher-rerun-unlink/package.json create mode 100644 test-tap/fixture/snapshots/watcher-rerun-unlink/test.js diff --git a/test-tap/fixture/snapshots/watcher-rerun-unlink/package.json b/test-tap/fixture/snapshots/watcher-rerun-unlink/package.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/test-tap/fixture/snapshots/watcher-rerun-unlink/package.json @@ -0,0 +1 @@ +{} diff --git a/test-tap/fixture/snapshots/watcher-rerun-unlink/test.js b/test-tap/fixture/snapshots/watcher-rerun-unlink/test.js new file mode 100644 index 000000000..1fcc6332d --- /dev/null +++ b/test-tap/fixture/snapshots/watcher-rerun-unlink/test.js @@ -0,0 +1,31 @@ +/* eslint-disable capitalized-comments */ + +const test = require('../../../..'); + +if (process.env.TEMPLATE) { + test('test title', t => { + t.snapshot({foo: 'bar'}); + + t.snapshot({answer: 42}); + + t.assert(true); + }); + + test('another test', t => { + t.snapshot(new Map()); + }); +} else { + test('test title', t => { + // t.snapshot({foo: 'bar'}); + + // t.snapshot({answer: 42}); + + t.assert(true); + }); + + test('another test', t => { + // t.snapshot(new Map()); + + t.assert(true); + }); +} diff --git a/test-tap/integration/watcher.js b/test-tap/integration/watcher.js index e30896f4c..1cf6cdf85 100644 --- a/test-tap/integration/watcher.js +++ b/test-tap/integration/watcher.js @@ -82,6 +82,52 @@ test('watcher does not rerun test files when they write snapshot files', t => { }); }); +test('watcher does not rerun test files when they unlink snapshot files', t => { + // Run fixture as template to generate snapshots + execCli( + ['--update-snapshots'], + { + dirname: 'fixture/snapshots/watcher-rerun-unlink', + env: {AVA_FORCE_CI: 'not-ci', TEMPLATE: 'true'} + }, + err => { + t.ifError(err); + + // Run fixture in watch mode; snapshots should be removed, and watcher should not rerun + let killed = false; + + const child = execCli( + ['--verbose', '--watch', '--update-snapshots', 'test.js'], + { + dirname: 'fixture/snapshots/watcher-rerun-unlink', + env: {AVA_FORCE_CI: 'not-ci'} + }, + err => { + t.ok(killed); + t.ifError(err); + t.end(); + } + ); + + let buffer = ''; + let passedFirst = false; + child.stdout.on('data', string => { + buffer += string; + if (buffer.includes('2 tests passed') && !passedFirst) { + buffer = ''; + passedFirst = true; + setTimeout(() => { + child.kill(); + killed = true; + }, 500); + } else if (passedFirst && !killed) { + t.is(buffer.replace(/\s/g, '').replace(END_MESSAGE.replace(/\s/g, ''), ''), ''); + } + }); + } + ); +}); + test('watcher does not rerun test files when ignored files change', t => { let killed = false; From 84a0433e6a06c762389a6e6b430b3aa79d9c134d Mon Sep 17 00:00:00 2001 From: ninevra Date: Fri, 11 Dec 2020 18:17:14 -0800 Subject: [PATCH 05/34] Report pruned snapshot files as touched --- lib/runner.js | 4 ++-- lib/snapshot-manager.js | 13 +++++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/runner.js b/lib/runner.js index 7994d30aa..33f47d59e 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -235,11 +235,11 @@ class Runner extends Emittery { !this.checkSelectedByLineNumbers && !this.declarationFailed ) { - await snapshotManager.cleanSnapshots({ + return {touchedFiles: await snapshotManager.cleanSnapshots({ file: this.file, fixedLocation: this.snapshotDir, projectDir: this.projectDir - }); + })}; } } diff --git a/lib/snapshot-manager.js b/lib/snapshot-manager.js index ac96d1dea..d1a63c5c1 100644 --- a/lib/snapshot-manager.js +++ b/lib/snapshot-manager.js @@ -471,15 +471,20 @@ async function cleanSnapshots({file, fixedLocation, projectDir}) { async function clean(file) { try { await fs.promises.unlink(file); + return [file]; } catch (error) { - if (error.code !== 'ENOENT') { - throw error; + if (error.code === 'ENOENT') { + return []; } + + throw error; } } - await clean(path.join(dir, snapFile)); - await clean(path.join(dir, reportFile)); + return [ + ...await clean(path.join(dir, snapFile)), + ...await clean(path.join(dir, reportFile)) + ]; } exports.cleanSnapshots = cleanSnapshots; From 375d82d6e955f2f10eb6812f4b7b092123154e44 Mon Sep 17 00:00:00 2001 From: ninevra Date: Sat, 12 Dec 2020 01:25:06 -0800 Subject: [PATCH 06/34] Refactor saveSnapshotState() --- lib/runner.js | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/lib/runner.js b/lib/runner.js index 33f47d59e..b94dd66c5 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -224,23 +224,21 @@ class Runner extends Emittery { return {touchedFiles: this.snapshots.save()}; } - if (this.updateSnapshots) { - if ( - !this.snapshots && - !this.anyFailure && - !this.skippingHooks && - !this.skippingTests && - !this.hasKnownFailingTests && - !this.runOnlyExclusive && - !this.checkSelectedByLineNumbers && - !this.declarationFailed - ) { - return {touchedFiles: await snapshotManager.cleanSnapshots({ - file: this.file, - fixedLocation: this.snapshotDir, - projectDir: this.projectDir - })}; - } + if ( + this.updateSnapshots && + !this.anyFailure && + !this.skippingHooks && + !this.skippingTests && + !this.hasKnownFailingTests && + !this.runOnlyExclusive && + !this.checkSelectedByLineNumbers && + !this.declarationFailed + ) { + return {touchedFiles: await snapshotManager.cleanSnapshots({ + file: this.file, + fixedLocation: this.snapshotDir, + projectDir: this.projectDir + })}; } return {}; From 9f2be0d6349b7aca1fdda59ae1a6f45788d591b7 Mon Sep 17 00:00:00 2001 From: ninevra Date: Mon, 14 Dec 2020 14:39:46 -0800 Subject: [PATCH 07/34] Apply same conditions to updating, pruning snapshots --- lib/runner.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/runner.js b/lib/runner.js index b94dd66c5..513870565 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -216,7 +216,18 @@ class Runner extends Emittery { } async saveSnapshotState() { - if (this.updateSnapshots && (this.runOnlyExclusive || this.skippingTests)) { + if ( + this.updateSnapshots && + ( + this.runOnlyExclusive || + this.skippingTests || + this.anyFailure || + this.skippingHooks || + this.hasKnownFailingTests || + this.checkSelectedByLineNumbers || + this.declarationFailed + ) + ) { return {cannotSave: true}; } @@ -224,16 +235,7 @@ class Runner extends Emittery { return {touchedFiles: this.snapshots.save()}; } - if ( - this.updateSnapshots && - !this.anyFailure && - !this.skippingHooks && - !this.skippingTests && - !this.hasKnownFailingTests && - !this.runOnlyExclusive && - !this.checkSelectedByLineNumbers && - !this.declarationFailed - ) { + if (this.updateSnapshots) { return {touchedFiles: await snapshotManager.cleanSnapshots({ file: this.file, fixedLocation: this.snapshotDir, From 2bf983f25328538a08fa14ef07df6954325dbc73 Mon Sep 17 00:00:00 2001 From: ninevra Date: Mon, 14 Dec 2020 16:42:21 -0800 Subject: [PATCH 08/34] Test that t.snapshot.skip() prevents updating snapshots --- .../fixtures/contains-skip-assertion.js | 10 +++++++ .../fixtures/contains-skip-assertion.js.md | 11 +++++++ .../fixtures/contains-skip-assertion.js.snap | Bin 0 -> 97 bytes test/snapshot-updates/snapshots/test.js.md | 28 ++++++++++++++++++ test/snapshot-updates/snapshots/test.js.snap | Bin 453 -> 524 bytes test/snapshot-updates/test.js | 7 +++++ 6 files changed, 56 insertions(+) create mode 100644 test/snapshot-updates/fixtures/contains-skip-assertion.js create mode 100644 test/snapshot-updates/fixtures/contains-skip-assertion.js.md create mode 100644 test/snapshot-updates/fixtures/contains-skip-assertion.js.snap diff --git a/test/snapshot-updates/fixtures/contains-skip-assertion.js b/test/snapshot-updates/fixtures/contains-skip-assertion.js new file mode 100644 index 000000000..960e34451 --- /dev/null +++ b/test/snapshot-updates/fixtures/contains-skip-assertion.js @@ -0,0 +1,10 @@ +const test = require('ava'); + +test('always failing snapshot', t => { + t.snapshot(Date.now()); +}); + +test('skipped assertion', t => { + t.snapshot.skip(Date.now()); // eslint-disable-line ava/no-skip-assert + t.pass(); +}); diff --git a/test/snapshot-updates/fixtures/contains-skip-assertion.js.md b/test/snapshot-updates/fixtures/contains-skip-assertion.js.md new file mode 100644 index 000000000..009f68b79 --- /dev/null +++ b/test/snapshot-updates/fixtures/contains-skip-assertion.js.md @@ -0,0 +1,11 @@ +# Snapshot report for `contains-skip-assertion.js` + +The actual snapshot is saved in `contains-skip-assertion.js.snap`. + +Generated by [AVA](https://avajs.dev). + +## always failing snapshot + +> Snapshot 1 + + 1607992742963 diff --git a/test/snapshot-updates/fixtures/contains-skip-assertion.js.snap b/test/snapshot-updates/fixtures/contains-skip-assertion.js.snap new file mode 100644 index 0000000000000000000000000000000000000000..4e5b373882185abd58e6697ef5803302b611f554 GIT binary patch literal 97 zcmZ<^b5sbwf=oE`=a8s-SXWW3}C<0KwJR9*Osz_3qVb8nk4~&5N0CWf=bpQYW literal 0 HcmV?d00001 diff --git a/test/snapshot-updates/snapshots/test.js.md b/test/snapshot-updates/snapshots/test.js.md index 2d85be886..a64f2b4ab 100644 --- a/test/snapshot-updates/snapshots/test.js.md +++ b/test/snapshot-updates/snapshots/test.js.md @@ -66,3 +66,31 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 'Snapshots cannot be updated when selecting specific tests by their line number.' + +## cannot update snapshots when skipping snapshot assertions + +> failed tests + + [ + { + file: 'contains-skip-assertion.js', + title: 'skipped assertion', + }, + ] + +> passed tests + + [ + { + file: 'contains-skip-assertion.js', + title: 'always failing snapshot', + }, + ] + +> files where snapshots could not be updated + + [ + { + file: 'contains-skip-assertion.js', + }, + ] diff --git a/test/snapshot-updates/snapshots/test.js.snap b/test/snapshot-updates/snapshots/test.js.snap index fd04d3f956cd3d8b10f2bd5f51ba46fb61780c7a..5b36884500faa1064f1fb67a9859c7b7415f3630 100644 GIT binary patch literal 524 zcmV+n0`vVrRzV-Db1S1h{THLjKqD^X))0D)K_juzkywvT&KA-Q43r|l-%dW>MvB|t3n z;b`&wHHJLT^CoN-3;wr<87x}N2*Kt|5PTMh{T!#iVq*IJubr{r^a@LXPG+#^2Podg z48cAu3=E7nJ}Yc?+I->m^80SL{4PFb28+tGLhuhLp25bzz|6o7b|WJzgCL`Sa9(0T zaYlYgu|jfUUS58QLQ<+iX+cV2NotBhc}8lULUC$NYH~?tUb;eYL27blT4u6BNop}r zeNv@DNk(dBkwQ*pUaCS~X>L+#ksgZSmIMvYO)N>yz%(0S3YWJw~wT5+D}( zaJ2aT8bhAvc@s8^1^?T_3>GbBgkW$F){uA*UngQdWEGxCo@>|0~Bv# zhF~8S1_ovZcCgKitPFyT{=s>P1;rWpCB+KKiFtYXB??KY3Z(@pi6yBi3gsE8c?!j; zIjPAdnR)37#RaL!nQ58H3MHwQTY&K6{3Nz2Shg>eLu^Ycm)GxLgd zi?cHe^s { const result = await t.throwsAsync(exec.fixture(['contains-skip.js:4', '-u'])); t.snapshot(exec.cleanOutput(result.stderr)); }); + +test('cannot update snapshots when skipping snapshot assertions', async t => { + const result = await t.throwsAsync(exec.fixture(['contains-skip-assertion.js', '-u'])); + t.snapshot(result.stats.failed, 'failed tests'); + t.snapshot(result.stats.passed, 'passed tests'); + t.snapshot(result.stats.unsavedSnapshots, 'files where snapshots could not be updated'); +}); From 6d577a7a87c287c4e704458961c3b11cb9ebce1e Mon Sep 17 00:00:00 2001 From: ninevra Date: Tue, 15 Dec 2020 00:24:13 -0800 Subject: [PATCH 09/34] Revert undesired behavior --- lib/runner.js | 23 +---------------------- lib/worker/subprocess.js | 7 +------ 2 files changed, 2 insertions(+), 28 deletions(-) diff --git a/lib/runner.js b/lib/runner.js index 513870565..3685fd8ed 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -24,10 +24,6 @@ class Runner extends Emittery { this.runOnlyExclusive = options.runOnlyExclusive === true; this.serial = options.serial === true; this.skippingTests = false; - this.skippingHooks = false; - this.hasKnownFailingTests = false; - // Set by lib/worker/subprocess.js if the test file fails to load - this.declarationFailed = false; this.snapshotDir = options.snapshotDir; this.updateSnapshots = options.updateSnapshots; @@ -49,8 +45,6 @@ class Runner extends Emittery { }; this.waitForReady = []; - this.anyFailure = false; - const uniqueTestTitles = new Set(); this.registerUniqueTitle = title => { if (uniqueTestTitles.has(title)) { @@ -152,12 +146,6 @@ class Runner extends Emittery { metadata: {...metadata} }; - if (metadata.type !== 'test') { - if (task.metadata.skipped) { - this.skippingHooks = true; - } - } - if (metadata.type === 'test') { if (this.match.length > 0) { // --match overrides .only() @@ -172,10 +160,6 @@ class Runner extends Emittery { this.runOnlyExclusive = true; } - if (task.metadata.failing) { - this.hasKnownFailingTests = true; - } - this.tasks[metadata.serial ? 'serial' : 'concurrent'].push(task); this.emit('stateChange', { type: 'declared-test', @@ -221,11 +205,7 @@ class Runner extends Emittery { ( this.runOnlyExclusive || this.skippingTests || - this.anyFailure || - this.skippingHooks || - this.hasKnownFailingTests || - this.checkSelectedByLineNumbers || - this.declarationFailed + this.checkSelectedByLineNumbers ) ) { return {cannotSave: true}; @@ -313,7 +293,6 @@ class Runner extends Emittery { const result = await runnable.run(); // If run() throws or rejects then the entire test run crashes, so // onRunComplete() doesn't *have* to be inside a finally. - this.anyFailure = this.anyFailure || !result.passed; this.onRunComplete(runnable); return result; } diff --git a/lib/worker/subprocess.js b/lib/worker/subprocess.js index 36ae15f4d..a40c281f2 100644 --- a/lib/worker/subprocess.js +++ b/lib/worker/subprocess.js @@ -241,12 +241,7 @@ ipc.options.then(async options => { } } - try { - await load(testPath); - } catch (error) { - runner.declarationFailed = true; - throw error; - } + await load(testPath); if (accessedRunner) { // Unreference the IPC channel if the test file required AVA. This stops it From 39d2860c02ff5b5e757960dfc93daa883e4e312c Mon Sep 17 00:00:00 2001 From: ninevra Date: Tue, 15 Dec 2020 00:05:52 -0800 Subject: [PATCH 10/34] Remove tests of undesired behavior --- test/snapshot-removal/crashing-file-esmod.js | 17 ----------- test/snapshot-removal/crashing-file.js | 12 -------- test/snapshot-removal/crashing-test.js | 12 -------- test/snapshot-removal/failing-assertion.js | 12 -------- .../fixtures/crashing-file-esmod/package.json | 3 -- .../fixtures/crashing-file-esmod/test.js | 29 ------------------- .../fixtures/crashing-file/package.json | 1 - .../fixtures/crashing-file/test.js | 29 ------------------- .../fixtures/crashing-test/package.json | 1 - .../fixtures/crashing-test/test.js | 28 ------------------ .../fixtures/failing-assertion/package.json | 1 - .../fixtures/failing-assertion/test.js | 27 ----------------- .../marked-failing-test-passes/package.json | 1 - .../marked-failing-test-passes/test.js | 27 ----------------- .../fixtures/marked-failing-test/package.json | 1 - .../fixtures/marked-failing-test/test.js | 27 ----------------- .../fixtures/skipped-hooks/package.json | 1 - .../fixtures/skipped-hooks/test.js | 27 ----------------- .../marked-failing-test-passes.js | 13 --------- test/snapshot-removal/marked-failing-test.js | 9 ------ test/snapshot-removal/skipped-hooks.js | 9 ------ 21 files changed, 287 deletions(-) delete mode 100644 test/snapshot-removal/crashing-file-esmod.js delete mode 100644 test/snapshot-removal/crashing-file.js delete mode 100644 test/snapshot-removal/crashing-test.js delete mode 100644 test/snapshot-removal/failing-assertion.js delete mode 100644 test/snapshot-removal/fixtures/crashing-file-esmod/package.json delete mode 100644 test/snapshot-removal/fixtures/crashing-file-esmod/test.js delete mode 100644 test/snapshot-removal/fixtures/crashing-file/package.json delete mode 100644 test/snapshot-removal/fixtures/crashing-file/test.js delete mode 100644 test/snapshot-removal/fixtures/crashing-test/package.json delete mode 100644 test/snapshot-removal/fixtures/crashing-test/test.js delete mode 100644 test/snapshot-removal/fixtures/failing-assertion/package.json delete mode 100644 test/snapshot-removal/fixtures/failing-assertion/test.js delete mode 100644 test/snapshot-removal/fixtures/marked-failing-test-passes/package.json delete mode 100644 test/snapshot-removal/fixtures/marked-failing-test-passes/test.js delete mode 100644 test/snapshot-removal/fixtures/marked-failing-test/package.json delete mode 100644 test/snapshot-removal/fixtures/marked-failing-test/test.js delete mode 100644 test/snapshot-removal/fixtures/skipped-hooks/package.json delete mode 100644 test/snapshot-removal/fixtures/skipped-hooks/test.js delete mode 100644 test/snapshot-removal/marked-failing-test-passes.js delete mode 100644 test/snapshot-removal/marked-failing-test.js delete mode 100644 test/snapshot-removal/skipped-hooks.js diff --git a/test/snapshot-removal/crashing-file-esmod.js b/test/snapshot-removal/crashing-file-esmod.js deleted file mode 100644 index b70426774..000000000 --- a/test/snapshot-removal/crashing-file-esmod.js +++ /dev/null @@ -1,17 +0,0 @@ -const test = require('@ava/test'); -const exec = require('../helpers/exec'); -const path = require('path'); -const {testSnapshotPruning} = require('./helpers/macros'); - -const testIfESM = process.version.slice(1) >= '12.17' ? - test.serial : - test.serial.skip; - -testIfESM('snapshots remain if esmodule test file crashes during declaration', testSnapshotPruning, { - cwd: exec.cwd(path.parse(__filename).name), - cli: ['--update-snapshots'], - remove: false, - error: { - message: /.*Uncaught exception in test\.js.*Crashing during test declaration.*1 uncaught exception.*/s - } -}); diff --git a/test/snapshot-removal/crashing-file.js b/test/snapshot-removal/crashing-file.js deleted file mode 100644 index 8d8c9c6bb..000000000 --- a/test/snapshot-removal/crashing-file.js +++ /dev/null @@ -1,12 +0,0 @@ -const test = require('@ava/test'); -const exec = require('../helpers/exec'); -const {testSnapshotPruning} = require('./helpers/macros'); - -test.serial('snapshots remain if test file crashes during declaration', testSnapshotPruning, { - cwd: exec.cwd('crashing-file'), - cli: ['--update-snapshots'], - remove: false, - error: { - message: /.*Uncaught exception in test\.js.*Crashing during test declaration.*1 uncaught exception.*/s - } -}); diff --git a/test/snapshot-removal/crashing-test.js b/test/snapshot-removal/crashing-test.js deleted file mode 100644 index 37a5f8f0f..000000000 --- a/test/snapshot-removal/crashing-test.js +++ /dev/null @@ -1,12 +0,0 @@ -const test = require('@ava/test'); -const exec = require('../helpers/exec'); -const {testSnapshotPruning} = require('./helpers/macros'); - -test.serial('snapshots remain if tests crash', testSnapshotPruning, { - cwd: exec.cwd('crashing-test'), - cli: ['--update-snapshots'], - remove: false, - error: { - message: /.*Error thrown in test.*/ - } -}); diff --git a/test/snapshot-removal/failing-assertion.js b/test/snapshot-removal/failing-assertion.js deleted file mode 100644 index b801ccbae..000000000 --- a/test/snapshot-removal/failing-assertion.js +++ /dev/null @@ -1,12 +0,0 @@ -const test = require('@ava/test'); -const exec = require('../helpers/exec'); -const {testSnapshotPruning} = require('./helpers/macros'); - -test.serial('snapshots remain if tests fail', testSnapshotPruning, { - cwd: exec.cwd('failing-assertion'), - cli: ['--update-snapshots'], - remove: false, - error: { - message: /.*Value is not truthy.*/ - } -}); diff --git a/test/snapshot-removal/fixtures/crashing-file-esmod/package.json b/test/snapshot-removal/fixtures/crashing-file-esmod/package.json deleted file mode 100644 index 3dbc1ca59..000000000 --- a/test/snapshot-removal/fixtures/crashing-file-esmod/package.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "type": "module" -} diff --git a/test/snapshot-removal/fixtures/crashing-file-esmod/test.js b/test/snapshot-removal/fixtures/crashing-file-esmod/test.js deleted file mode 100644 index 2a63bf4ec..000000000 --- a/test/snapshot-removal/fixtures/crashing-file-esmod/test.js +++ /dev/null @@ -1,29 +0,0 @@ -/* eslint-disable capitalized-comments, ava/no-identical-title, no-unreachable */ - -import test from 'ava'; - -if (process.env.TEMPLATE) { - test('some snapshots', t => { - t.snapshot('foo'); - t.snapshot('bar'); - t.assert(true); - }); - - test('another snapshot', t => { - t.snapshot('baz'); - t.assert(true); - }); -} else { - test('some snapshots', t => { - // t.snapshot('foo'); - // t.snapshot('bar'); - t.assert(true); - }); - - throw new Error('Crashing during test declaration.'); - - test('another snapshot', t => { - // t.snapshot('baz'); - t.assert(true); - }); -} diff --git a/test/snapshot-removal/fixtures/crashing-file/package.json b/test/snapshot-removal/fixtures/crashing-file/package.json deleted file mode 100644 index 0967ef424..000000000 --- a/test/snapshot-removal/fixtures/crashing-file/package.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/test/snapshot-removal/fixtures/crashing-file/test.js b/test/snapshot-removal/fixtures/crashing-file/test.js deleted file mode 100644 index 79e238d75..000000000 --- a/test/snapshot-removal/fixtures/crashing-file/test.js +++ /dev/null @@ -1,29 +0,0 @@ -/* eslint-disable capitalized-comments, ava/no-identical-title, no-unreachable */ - -const test = require('ava'); - -if (process.env.TEMPLATE) { - test('some snapshots', t => { - t.snapshot('foo'); - t.snapshot('bar'); - t.assert(true); - }); - - test('another snapshot', t => { - t.snapshot('baz'); - t.assert(true); - }); -} else { - test('some snapshots', t => { - // t.snapshot('foo'); - // t.snapshot('bar'); - t.assert(true); - }); - - throw new Error('Crashing during test declaration.'); - - test('another snapshot', t => { - // t.snapshot('baz'); - t.assert(true); - }); -} diff --git a/test/snapshot-removal/fixtures/crashing-test/package.json b/test/snapshot-removal/fixtures/crashing-test/package.json deleted file mode 100644 index 0967ef424..000000000 --- a/test/snapshot-removal/fixtures/crashing-test/package.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/test/snapshot-removal/fixtures/crashing-test/test.js b/test/snapshot-removal/fixtures/crashing-test/test.js deleted file mode 100644 index e63d9e316..000000000 --- a/test/snapshot-removal/fixtures/crashing-test/test.js +++ /dev/null @@ -1,28 +0,0 @@ -/* eslint-disable capitalized-comments, ava/no-identical-title */ - -const test = require('ava'); - -if (process.env.TEMPLATE) { - test('some snapshots', t => { - t.snapshot('foo'); - t.snapshot('bar'); - t.assert(true); - }); - - test('another snapshot', t => { - t.snapshot('baz'); - t.assert(true); - }); -} else { - test('some snapshots', t => { - // t.snapshot('foo'); - // t.snapshot('bar'); - t.assert(true); - }); - - test('another snapshot', t => { - // t.snapshot('baz'); - t.assert(true); - throw new Error('Crashing in test.'); - }); -} diff --git a/test/snapshot-removal/fixtures/failing-assertion/package.json b/test/snapshot-removal/fixtures/failing-assertion/package.json deleted file mode 100644 index 0967ef424..000000000 --- a/test/snapshot-removal/fixtures/failing-assertion/package.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/test/snapshot-removal/fixtures/failing-assertion/test.js b/test/snapshot-removal/fixtures/failing-assertion/test.js deleted file mode 100644 index 167bc9894..000000000 --- a/test/snapshot-removal/fixtures/failing-assertion/test.js +++ /dev/null @@ -1,27 +0,0 @@ -/* eslint-disable capitalized-comments, ava/no-identical-title */ - -const test = require('ava'); - -if (process.env.TEMPLATE) { - test('some snapshots', t => { - t.snapshot('foo'); - t.snapshot('bar'); - t.assert(true); - }); - - test('another snapshot', t => { - t.snapshot('baz'); - t.assert(true); - }); -} else { - test('some snapshots', t => { - // t.snapshot('foo'); - // t.snapshot('bar'); - t.assert(true); - }); - - test('another snapshot', t => { - // t.snapshot('baz'); - t.assert(false); - }); -} diff --git a/test/snapshot-removal/fixtures/marked-failing-test-passes/package.json b/test/snapshot-removal/fixtures/marked-failing-test-passes/package.json deleted file mode 100644 index 0967ef424..000000000 --- a/test/snapshot-removal/fixtures/marked-failing-test-passes/package.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/test/snapshot-removal/fixtures/marked-failing-test-passes/test.js b/test/snapshot-removal/fixtures/marked-failing-test-passes/test.js deleted file mode 100644 index 578bbb0ba..000000000 --- a/test/snapshot-removal/fixtures/marked-failing-test-passes/test.js +++ /dev/null @@ -1,27 +0,0 @@ -/* eslint-disable capitalized-comments, ava/no-identical-title */ - -const test = require('ava'); - -if (process.env.TEMPLATE) { - test('some snapshots', t => { - t.snapshot('foo'); - t.snapshot('bar'); - t.assert(true); - }); - - test.failing('another snapshot', t => { - t.snapshot('baz'); - t.assert(false); - }); -} else { - test('some snapshots', t => { - // t.snapshot('foo'); - // t.snapshot('bar'); - t.assert(true); - }); - - test.failing('another snapshot', t => { - // t.snapshot('baz'); - t.assert(true); - }); -} diff --git a/test/snapshot-removal/fixtures/marked-failing-test/package.json b/test/snapshot-removal/fixtures/marked-failing-test/package.json deleted file mode 100644 index 0967ef424..000000000 --- a/test/snapshot-removal/fixtures/marked-failing-test/package.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/test/snapshot-removal/fixtures/marked-failing-test/test.js b/test/snapshot-removal/fixtures/marked-failing-test/test.js deleted file mode 100644 index bd58d4c9a..000000000 --- a/test/snapshot-removal/fixtures/marked-failing-test/test.js +++ /dev/null @@ -1,27 +0,0 @@ -/* eslint-disable capitalized-comments, ava/no-identical-title */ - -const test = require('ava'); - -if (process.env.TEMPLATE) { - test('some snapshots', t => { - t.snapshot('foo'); - t.snapshot('bar'); - t.assert(true); - }); - - test.failing('another snapshot', t => { - t.snapshot('baz'); - t.assert(false); - }); -} else { - test('some snapshots', t => { - // t.snapshot('foo'); - // t.snapshot('bar'); - t.assert(true); - }); - - test.failing('another snapshot', t => { - // t.snapshot('baz'); - t.assert(false); - }); -} diff --git a/test/snapshot-removal/fixtures/skipped-hooks/package.json b/test/snapshot-removal/fixtures/skipped-hooks/package.json deleted file mode 100644 index 0967ef424..000000000 --- a/test/snapshot-removal/fixtures/skipped-hooks/package.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/test/snapshot-removal/fixtures/skipped-hooks/test.js b/test/snapshot-removal/fixtures/skipped-hooks/test.js deleted file mode 100644 index 5cbdf2522..000000000 --- a/test/snapshot-removal/fixtures/skipped-hooks/test.js +++ /dev/null @@ -1,27 +0,0 @@ -/* eslint-disable capitalized-comments, ava/no-identical-title, no-unused-vars, ava/no-skip-test */ - -if (process.env.TEMPLATE) { - const test = require('ava'); - - test.afterEach.always(t => { - t.snapshot('Hello, world!'); - }); - - test('some snapshots', t => { - t.snapshot('foo'); - t.snapshot('bar'); - t.assert(true); - }); -} else { - const test = require('ava'); - - test.afterEach.always.skip(t => { - // t.snapshot('Hello, world!'); - }); - - test('some snapshots', t => { - // t.snapshot('foo'); - // t.snapshot('bar'); - t.assert(true); - }); -} diff --git a/test/snapshot-removal/marked-failing-test-passes.js b/test/snapshot-removal/marked-failing-test-passes.js deleted file mode 100644 index 1e4e54195..000000000 --- a/test/snapshot-removal/marked-failing-test-passes.js +++ /dev/null @@ -1,13 +0,0 @@ -const test = require('@ava/test'); -const exec = require('../helpers/exec'); -const {testSnapshotPruning} = require('./helpers/macros'); -const path = require('path'); - -test.serial('snapshots remain if any test.failing() passes', testSnapshotPruning, { - cwd: exec.cwd(path.parse(__filename).name), - cli: ['--update-snapshots'], - remove: false, - error: { - message: /.*Test was expected to fail, but succeeded.*/ - } -}); diff --git a/test/snapshot-removal/marked-failing-test.js b/test/snapshot-removal/marked-failing-test.js deleted file mode 100644 index 7027decc1..000000000 --- a/test/snapshot-removal/marked-failing-test.js +++ /dev/null @@ -1,9 +0,0 @@ -const test = require('@ava/test'); -const exec = require('../helpers/exec'); -const {testSnapshotPruning} = require('./helpers/macros'); - -test.serial('snapshots remain if any test.failing()', testSnapshotPruning, { - cwd: exec.cwd('marked-failing-test'), - cli: ['--update-snapshots'], - remove: false -}); diff --git a/test/snapshot-removal/skipped-hooks.js b/test/snapshot-removal/skipped-hooks.js deleted file mode 100644 index 294bf98ab..000000000 --- a/test/snapshot-removal/skipped-hooks.js +++ /dev/null @@ -1,9 +0,0 @@ -const test = require('@ava/test'); -const exec = require('../helpers/exec'); -const {testSnapshotPruning} = require('./helpers/macros'); - -test.serial('snapshots remain if hooks are skipped', testSnapshotPruning, { - cwd: exec.cwd('skipped-hooks'), - cli: ['--update-snapshots'], - remove: false -}); From daa87ff365e26448027ea25b9f4ba9cd39927f86 Mon Sep 17 00:00:00 2001 From: ninevra Date: Tue, 15 Dec 2020 14:56:25 -0800 Subject: [PATCH 11/34] Revert "no-push: disable crashing lint rule" This reverts commit b55285affdaa04ab7c91efe4b58aa6cfb1dd1d48. --- xo.config.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/xo.config.js b/xo.config.js index 2c526049d..49c546469 100644 --- a/xo.config.js +++ b/xo.config.js @@ -12,8 +12,7 @@ module.exports = { 'import/no-anonymous-default-export': 'off', 'import/no-unresolved': ['error', {commonjs: true}], 'no-use-extend-native/no-use-extend-native': 'off', - '@typescript-eslint/no-var-requires': 'off', - 'ava/no-statement-after-end': 'off' + '@typescript-eslint/no-var-requires': 'off' }, overrides: [ { From 5a3f3bbc1f2c3579b2b4a7ed4d3fdbef3396a86e Mon Sep 17 00:00:00 2001 From: ninevra Date: Tue, 15 Dec 2020 22:24:06 -0800 Subject: [PATCH 12/34] Reimplement snapshot.skip() check as runner callback --- lib/runner.js | 11 ++++++++++- lib/test.js | 4 ++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/runner.js b/lib/runner.js index 3685fd8ed..1e2c75f03 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -29,6 +29,8 @@ class Runner extends Emittery { this.activeRunnables = new Set(); this.boundCompareTestSnapshot = this.compareTestSnapshot.bind(this); + this.skippedSnapshots = false; + this.boundSkipSnapshot = this.skipSnapshot.bind(this); this.interrupted = false; this.snapshots = null; this.nextTaskIndex = 0; @@ -199,13 +201,18 @@ class Runner extends Emittery { return this.snapshots.compare(options); } + skipSnapshot() { + this.skippedSnapshots = true; + } + async saveSnapshotState() { if ( this.updateSnapshots && ( this.runOnlyExclusive || this.skippingTests || - this.checkSelectedByLineNumbers + this.checkSelectedByLineNumbers || + this.skippedSnapshots ) ) { return {cannotSave: true}; @@ -306,6 +313,7 @@ class Runner extends Emittery { task.implementation : t => task.implementation.apply(null, [t].concat(task.args)), compareTestSnapshot: this.boundCompareTestSnapshot, + skipSnapshot: this.boundSkipSnapshot, updateSnapshots: this.updateSnapshots, metadata: {...task.metadata, associatedTaskIndex}, powerAssert: this.powerAssert, @@ -358,6 +366,7 @@ class Runner extends Emittery { task.implementation : t => task.implementation.apply(null, [t].concat(task.args)), compareTestSnapshot: this.boundCompareTestSnapshot, + skipSnapshot: this.boundSkipSnapshot, updateSnapshots: this.updateSnapshots, metadata: task.metadata, powerAssert: this.powerAssert, diff --git a/lib/test.js b/lib/test.js index b0814654e..604b80dc8 100644 --- a/lib/test.js +++ b/lib/test.js @@ -248,7 +248,11 @@ class Test { return result; }; + const skipSnapshotHook = options.skipSnapshot || (() => {}); + this.skipSnapshot = () => { + skipSnapshotHook(); + if (options.updateSnapshots) { this.addFailedAssertion(new Error('Snapshot assertions cannot be skipped when updating snapshots')); } else { From 2eaad192810b93e691658cca152c2cd376c9fc7f Mon Sep 17 00:00:00 2001 From: ninevra Date: Tue, 15 Dec 2020 22:24:58 -0800 Subject: [PATCH 13/34] Rename skippedSnapshots to skippingSnapshots For consistency with skippingTests --- lib/runner.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/runner.js b/lib/runner.js index 1e2c75f03..85f80ee58 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -29,7 +29,7 @@ class Runner extends Emittery { this.activeRunnables = new Set(); this.boundCompareTestSnapshot = this.compareTestSnapshot.bind(this); - this.skippedSnapshots = false; + this.skippingSnapshots = false; this.boundSkipSnapshot = this.skipSnapshot.bind(this); this.interrupted = false; this.snapshots = null; @@ -202,7 +202,7 @@ class Runner extends Emittery { } skipSnapshot() { - this.skippedSnapshots = true; + this.skippingSnapshots = true; } async saveSnapshotState() { @@ -212,7 +212,7 @@ class Runner extends Emittery { this.runOnlyExclusive || this.skippingTests || this.checkSelectedByLineNumbers || - this.skippedSnapshots + this.skippingSnapshots ) ) { return {cannotSave: true}; From b1171faf7914a3cf883ac43486cd15773d500cde Mon Sep 17 00:00:00 2001 From: ninevra Date: Tue, 15 Dec 2020 22:33:42 -0800 Subject: [PATCH 14/34] Test with snapshot.skip() in a discarded try() --- .../skipped-snapshots-in-try/package.json | 1 + .../fixtures/skipped-snapshots-in-try/test.js | 27 +++++++++++++++++++ .../skipped-snapshots-in-try.js | 10 +++++++ 3 files changed, 38 insertions(+) create mode 100644 test/snapshot-removal/fixtures/skipped-snapshots-in-try/package.json create mode 100644 test/snapshot-removal/fixtures/skipped-snapshots-in-try/test.js create mode 100644 test/snapshot-removal/skipped-snapshots-in-try.js diff --git a/test/snapshot-removal/fixtures/skipped-snapshots-in-try/package.json b/test/snapshot-removal/fixtures/skipped-snapshots-in-try/package.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/test/snapshot-removal/fixtures/skipped-snapshots-in-try/package.json @@ -0,0 +1 @@ +{} diff --git a/test/snapshot-removal/fixtures/skipped-snapshots-in-try/test.js b/test/snapshot-removal/fixtures/skipped-snapshots-in-try/test.js new file mode 100644 index 000000000..358c8f33d --- /dev/null +++ b/test/snapshot-removal/fixtures/skipped-snapshots-in-try/test.js @@ -0,0 +1,27 @@ +/* eslint-disable ava/no-identical-title */ + +if (process.env.TEMPLATE) { + const test = require('ava'); + + test('skipped snapshots in try', async t => { + const attempt = await t.try(tt => { + tt.snapshot('in try'); + }); + + attempt.commit(); + + t.assert(true); + }); +} else { + const test = require('ava'); + + test('skipped snapshots in try', async t => { + const attempt = await t.try(tt => { + tt.snapshot.skip('in try'); + }); + + attempt.discard(); + + t.assert(true); + }); +} diff --git a/test/snapshot-removal/skipped-snapshots-in-try.js b/test/snapshot-removal/skipped-snapshots-in-try.js new file mode 100644 index 000000000..fab93ba2e --- /dev/null +++ b/test/snapshot-removal/skipped-snapshots-in-try.js @@ -0,0 +1,10 @@ +const test = require('@ava/test'); +const exec = require('../helpers/exec'); +const {testSnapshotPruning} = require('./helpers/macros'); +const path = require('path'); + +test.serial('snapshots remain if skipped in a discarded try()', testSnapshotPruning, { + cwd: exec.cwd(path.parse(__filename).name), + cli: ['--update-snapshots'], + remove: false +}); From 1e151ec6955a927849cfde3c839a9c59077dcad3 Mon Sep 17 00:00:00 2001 From: ninevra Date: Wed, 16 Dec 2020 00:09:29 -0800 Subject: [PATCH 15/34] Prefer snapshotting test results where practical --- test/snapshot-removal/helpers/macros.js | 12 ++++++------ test/snapshot-removal/only-test.js | 7 ++++++- test/snapshot-removal/removal.js | 10 ++++++---- .../skipped-snapshots-in-try.js | 7 ++++++- test/snapshot-removal/skipped-snapshots.js | 7 +++++-- test/snapshot-removal/skipped-tests.js | 7 ++++++- .../snapshot-removal/snapshots/only-test.js.md | 15 +++++++++++++++ .../snapshots/only-test.js.snap | Bin 0 -> 180 bytes test/snapshot-removal/snapshots/removal.js.md | 17 +++++++++++++++++ .../snapshot-removal/snapshots/removal.js.snap | Bin 0 -> 209 bytes .../snapshots/skipped-snapshots-in-try.js.md | 15 +++++++++++++++ .../snapshots/skipped-snapshots-in-try.js.snap | Bin 0 -> 179 bytes .../snapshots/skipped-snapshots.js.md | 15 +++++++++++++++ .../snapshots/skipped-snapshots.js.snap | Bin 0 -> 179 bytes .../snapshots/skipped-tests.js.md | 15 +++++++++++++++ .../snapshots/skipped-tests.js.snap | Bin 0 -> 180 bytes 16 files changed, 112 insertions(+), 15 deletions(-) create mode 100644 test/snapshot-removal/snapshots/only-test.js.md create mode 100644 test/snapshot-removal/snapshots/only-test.js.snap create mode 100644 test/snapshot-removal/snapshots/removal.js.md create mode 100644 test/snapshot-removal/snapshots/removal.js.snap create mode 100644 test/snapshot-removal/snapshots/skipped-snapshots-in-try.js.md create mode 100644 test/snapshot-removal/snapshots/skipped-snapshots-in-try.js.snap create mode 100644 test/snapshot-removal/snapshots/skipped-snapshots.js.md create mode 100644 test/snapshot-removal/snapshots/skipped-snapshots.js.snap create mode 100644 test/snapshot-removal/snapshots/skipped-tests.js.md create mode 100644 test/snapshot-removal/snapshots/skipped-tests.js.snap diff --git a/test/snapshot-removal/helpers/macros.js b/test/snapshot-removal/helpers/macros.js index 6d8460009..e9d5de4de 100644 --- a/test/snapshot-removal/helpers/macros.js +++ b/test/snapshot-removal/helpers/macros.js @@ -7,9 +7,11 @@ async function testSnapshotPruning(t, { ci = 'not-ci', cli, remove, - error = false, snapshotPath = 'test.js.snap', - reportPath = 'test.js.md' + reportPath = 'test.js.md', + checkRun = async (t, run) => { + await t.notThrowsAsync(run, 'Expected fixture not to throw'); + } }) { snapshotPath = path.join(cwd, snapshotPath); reportPath = path.join(cwd, reportPath); @@ -37,16 +39,14 @@ async function testSnapshotPruning(t, { await t.notThrowsAsync(fs.access(reportPath), 'Template didn\'t create a report - there\'s a bug in the test'); // Execute fixture as run - const runResult = exec.fixture(cli, { + const run = exec.fixture(cli, { cwd, env: { AVA_FORCE_CI: ci } }); - await (error ? - t.throwsAsync(runResult, error === true ? undefined : error, 'Expected fixture to throw.') : - t.notThrowsAsync(runResult, 'Expected fixture not to throw.')); + await checkRun(t, run); if (remove) { // Assert files don't exist diff --git a/test/snapshot-removal/only-test.js b/test/snapshot-removal/only-test.js index cd54f8943..80b461f7c 100644 --- a/test/snapshot-removal/only-test.js +++ b/test/snapshot-removal/only-test.js @@ -5,5 +5,10 @@ const {testSnapshotPruning} = require('./helpers/macros'); test.serial('snapshots remain if using test.only', testSnapshotPruning, { cwd: exec.cwd('only-test'), cli: ['--update-snapshots'], - remove: false + remove: false, + checkRun: async (t, run) => { + await t.notThrowsAsync(run, 'Expected fixture not to throw'); + const result = await run; + t.snapshot(result.stats.unsavedSnapshots, 'files where snapshots could not be updated'); + } }); diff --git a/test/snapshot-removal/removal.js b/test/snapshot-removal/removal.js index fb2f0fbc1..9483d66c4 100644 --- a/test/snapshot-removal/removal.js +++ b/test/snapshot-removal/removal.js @@ -18,8 +18,9 @@ test.serial('snapshots remain if tests run with --match', testSnapshotPruning, { cwd: exec.cwd('removal'), cli: ['--update-snapshots', '--match=\'*snapshot*\''], remove: false, - error: { - message: /.*Snapshots cannot be updated when matching specific tests.*/ + checkRun: async (t, run) => { + const result = await t.throwsAsync(run, undefined, 'Expected fixture to throw'); + t.snapshot(exec.cleanOutput(result.stderr), 'stderr'); } }); @@ -27,7 +28,8 @@ test.serial('snapshots remain if tests selected by line numbers', testSnapshotPr cwd: exec.cwd('removal'), cli: ['test.js:3-12', '--update-snapshots'], remove: false, - error: { - message: /.*Snapshots cannot be updated when selecting specific tests by their line number.*/ + checkRun: async (t, run) => { + const result = await t.throwsAsync(run, undefined, 'Expected fixture to throw'); + t.snapshot(exec.cleanOutput(result.stderr), 'stderr'); } }); diff --git a/test/snapshot-removal/skipped-snapshots-in-try.js b/test/snapshot-removal/skipped-snapshots-in-try.js index fab93ba2e..ad9ded111 100644 --- a/test/snapshot-removal/skipped-snapshots-in-try.js +++ b/test/snapshot-removal/skipped-snapshots-in-try.js @@ -6,5 +6,10 @@ const path = require('path'); test.serial('snapshots remain if skipped in a discarded try()', testSnapshotPruning, { cwd: exec.cwd(path.parse(__filename).name), cli: ['--update-snapshots'], - remove: false + remove: false, + checkRun: async (t, run) => { + await t.notThrowsAsync(run, 'Expected fixture not to throw'); + const result = await run; + t.snapshot(result.stats.unsavedSnapshots, 'files where snapshots could not be updated'); + } }); diff --git a/test/snapshot-removal/skipped-snapshots.js b/test/snapshot-removal/skipped-snapshots.js index 6374d98a2..74bb94c5f 100644 --- a/test/snapshot-removal/skipped-snapshots.js +++ b/test/snapshot-removal/skipped-snapshots.js @@ -6,8 +6,11 @@ test.serial('snapshots remain if snapshot assertions are skipped (-u)', testSnap cwd: exec.cwd('skipped-snapshots'), cli: ['--update-snapshots'], remove: false, - error: { - message: /.*Snapshot assertions cannot be skipped when updating snapshots.*/ + checkRun: async (t, run) => { + const result = await t.throwsAsync(run, { + message: /Snapshot assertions cannot be skipped when updating snapshots/ + }, 'Expected fixture to throw'); + t.snapshot(result.stats.unsavedSnapshots, 'files where snapshots could not be updated'); } }); diff --git a/test/snapshot-removal/skipped-tests.js b/test/snapshot-removal/skipped-tests.js index abaa52f3e..bcb1c1117 100644 --- a/test/snapshot-removal/skipped-tests.js +++ b/test/snapshot-removal/skipped-tests.js @@ -5,5 +5,10 @@ const {testSnapshotPruning} = require('./helpers/macros'); test.serial('snapshots remain if tests are skipped', testSnapshotPruning, { cwd: exec.cwd('skipped-tests'), cli: ['--update-snapshots'], - remove: false + remove: false, + checkRun: async (t, run) => { + await t.notThrowsAsync(run, 'Expected fixture not to throw'); + const result = await run; + t.snapshot(result.stats.unsavedSnapshots, 'files where snapshots could not be updated'); + } }); diff --git a/test/snapshot-removal/snapshots/only-test.js.md b/test/snapshot-removal/snapshots/only-test.js.md new file mode 100644 index 000000000..a017467c2 --- /dev/null +++ b/test/snapshot-removal/snapshots/only-test.js.md @@ -0,0 +1,15 @@ +# Snapshot report for `test/snapshot-removal/only-test.js` + +The actual snapshot is saved in `only-test.js.snap`. + +Generated by [AVA](https://avajs.dev). + +## snapshots remain if using test.only + +> files where snapshots could not be updated + + [ + { + file: 'test.js', + }, + ] diff --git a/test/snapshot-removal/snapshots/only-test.js.snap b/test/snapshot-removal/snapshots/only-test.js.snap new file mode 100644 index 0000000000000000000000000000000000000000..9d9f82c2fcbe5d1aea83fe09f8ae322e6bd01c9c GIT binary patch literal 180 zcmZ<^b5sbzq8b(RW literal 0 HcmV?d00001 diff --git a/test/snapshot-removal/snapshots/removal.js.md b/test/snapshot-removal/snapshots/removal.js.md new file mode 100644 index 000000000..fd7c387e7 --- /dev/null +++ b/test/snapshot-removal/snapshots/removal.js.md @@ -0,0 +1,17 @@ +# Snapshot report for `test/snapshot-removal/removal.js` + +The actual snapshot is saved in `removal.js.snap`. + +Generated by [AVA](https://avajs.dev). + +## snapshots remain if tests run with --match + +> stderr + + 'Snapshots cannot be updated when matching specific tests.' + +## snapshots remain if tests selected by line numbers + +> stderr + + 'Snapshots cannot be updated when selecting specific tests by their line number.' diff --git a/test/snapshot-removal/snapshots/removal.js.snap b/test/snapshot-removal/snapshots/removal.js.snap new file mode 100644 index 0000000000000000000000000000000000000000..bd0cf20f12bdd819d612ad6c81e01552a3fd2aa2 GIT binary patch literal 209 zcmV;?051PQRzV>_v)ydZ<85qH$OMsY} zfgP-hk(EJ^(K0wMv7k63zob|pIWaFUzeFJ^RiU&XC9xzmMWH++HBTWou_QSoGcR4C zxF9t-Gc7Y&p(M4qq*xEd7=MDs6sP8-CYNBEqmWdoP?C|FS)`DYnU|`NSDKrYTBHX6 LWnU`Y-T(jqirZKC literal 0 HcmV?d00001 diff --git a/test/snapshot-removal/snapshots/skipped-snapshots-in-try.js.md b/test/snapshot-removal/snapshots/skipped-snapshots-in-try.js.md new file mode 100644 index 000000000..f409ba616 --- /dev/null +++ b/test/snapshot-removal/snapshots/skipped-snapshots-in-try.js.md @@ -0,0 +1,15 @@ +# Snapshot report for `test/snapshot-removal/skipped-snapshots-in-try.js` + +The actual snapshot is saved in `skipped-snapshots-in-try.js.snap`. + +Generated by [AVA](https://avajs.dev). + +## snapshots remain if skipped in a discarded try() + +> files where snapshots could not be updated + + [ + { + file: 'test.js', + }, + ] diff --git a/test/snapshot-removal/snapshots/skipped-snapshots-in-try.js.snap b/test/snapshot-removal/snapshots/skipped-snapshots-in-try.js.snap new file mode 100644 index 0000000000000000000000000000000000000000..47a7546e25322dce9c015ed81ea6c736b17f3c4d GIT binary patch literal 179 zcmV;k08IZuRzVgQl<(Teeq#uh200000000A1 zU|?WiWbk(4jL|GE_V_tVeOaZC!vaPI5arz{thG$jHFR&B$m2*OUyTGhv$8{FAa$lS>$x;7kDl0R~21ur_|68YU4& hW(G!9K}MFe%$!shhrJ}VxI{0j7y$DMj>fG30020_Nb~>z literal 0 HcmV?d00001 diff --git a/test/snapshot-removal/snapshots/skipped-snapshots.js.md b/test/snapshot-removal/snapshots/skipped-snapshots.js.md new file mode 100644 index 000000000..04f0c7ce8 --- /dev/null +++ b/test/snapshot-removal/snapshots/skipped-snapshots.js.md @@ -0,0 +1,15 @@ +# Snapshot report for `test/snapshot-removal/skipped-snapshots.js` + +The actual snapshot is saved in `skipped-snapshots.js.snap`. + +Generated by [AVA](https://avajs.dev). + +## snapshots remain if snapshot assertions are skipped (-u) + +> files where snapshots could not be updated + + [ + { + file: 'test.js', + }, + ] diff --git a/test/snapshot-removal/snapshots/skipped-snapshots.js.snap b/test/snapshot-removal/snapshots/skipped-snapshots.js.snap new file mode 100644 index 0000000000000000000000000000000000000000..da4f7d8b775b297a960387636660efb00f3f4717 GIT binary patch literal 179 zcmZ<^b5sb^-kjciM>ZwcBrW+U?oXNz*#M84UA@`4isgF|68J?as eO+7l9Gc8vd@Kt+e6?Vpj88RfjiQc#t=n?=Ju|#43 literal 0 HcmV?d00001 diff --git a/test/snapshot-removal/snapshots/skipped-tests.js.md b/test/snapshot-removal/snapshots/skipped-tests.js.md new file mode 100644 index 000000000..9a67b4ea5 --- /dev/null +++ b/test/snapshot-removal/snapshots/skipped-tests.js.md @@ -0,0 +1,15 @@ +# Snapshot report for `test/snapshot-removal/skipped-tests.js` + +The actual snapshot is saved in `skipped-tests.js.snap`. + +Generated by [AVA](https://avajs.dev). + +## snapshots remain if tests are skipped + +> files where snapshots could not be updated + + [ + { + file: 'test.js', + }, + ] diff --git a/test/snapshot-removal/snapshots/skipped-tests.js.snap b/test/snapshot-removal/snapshots/skipped-tests.js.snap new file mode 100644 index 0000000000000000000000000000000000000000..dd4bc7cd13918573b35565428838b435aec45e55 GIT binary patch literal 180 zcmZ<^b5sbm^5@rLP3#HPPU6=i0)&eN&17U&cJP&`Sg literal 0 HcmV?d00001 From f4a4ab0bccd504da2b87438f253019869d9c047e Mon Sep 17 00:00:00 2001 From: ninevra Date: Wed, 16 Dec 2020 00:33:52 -0800 Subject: [PATCH 16/34] Unit-test skipSnapshot() hooks --- test-tap/test.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/test-tap/test.js b/test-tap/test.js index 4b648d990..58f7dbc33 100644 --- a/test-tap/test.js +++ b/test-tap/test.js @@ -696,6 +696,36 @@ test('snapshot assertion can be skipped', t => { }); }); +// Snapshots reused from test/assert.js +test('snapshot assertions call options.skipSnapshot when skipped', async t => { + const projectDir = path.join(__dirname, 'fixture'); + const manager = snapshotManager.load({ + file: path.join(projectDir, 'assert.js'), + projectDir, + fixedLocation: null, + updating: false + }); + + const skipSnapshot = sinon.spy(); + + const test = new Test({ + compareTestSnapshot: options => manager.compare(options), + skipSnapshot, + updateSnapshots: false, + metadata: {}, + title: 'passes', + fn(t) { + t.snapshot.skip({not: {a: 'match'}}); + t.snapshot.skip({not: {b: 'match'}}); + t.snapshot(React.createElement(HelloMessage, {name: 'Sindre'})); + } + }); + + await test.run(); + + t.true(skipSnapshot.calledTwice); +}); + test('snapshot assertion cannot be skipped when updating snapshots', t => { return new Test({ updateSnapshots: true, From 8efaf26af928ef054035fc5afd63fdada6eb7ee8 Mon Sep 17 00:00:00 2001 From: ninevra Date: Wed, 16 Dec 2020 00:37:37 -0800 Subject: [PATCH 17/34] Refactor test fixtures --- test/snapshot-removal/fixtures/fixed-snapshot-dir/test.js | 8 ++++---- test/snapshot-removal/fixtures/only-test/test.js | 8 ++++---- test/snapshot-removal/fixtures/removal/test.js | 8 ++++---- .../fixtures/skipped-snapshots-in-try/test.js | 4 ++-- test/snapshot-removal/fixtures/skipped-snapshots/test.js | 8 ++++---- test/snapshot-removal/fixtures/skipped-tests/test.js | 8 ++++---- test/snapshot-removal/fixtures/snapshot-dir/test/test.js | 8 ++++---- test/snapshot-removal/fixtures/try/test.js | 4 ++-- 8 files changed, 28 insertions(+), 28 deletions(-) diff --git a/test/snapshot-removal/fixtures/fixed-snapshot-dir/test.js b/test/snapshot-removal/fixtures/fixed-snapshot-dir/test.js index e3999d0db..2ec6c3f83 100644 --- a/test/snapshot-removal/fixtures/fixed-snapshot-dir/test.js +++ b/test/snapshot-removal/fixtures/fixed-snapshot-dir/test.js @@ -6,12 +6,12 @@ if (process.env.TEMPLATE) { test('some snapshots', t => { t.snapshot('foo'); t.snapshot('bar'); - t.assert(true); + t.pass(); }); test('another snapshot', t => { t.snapshot('baz'); - t.assert(true); + t.pass(); }); } else { const test = require('ava'); @@ -19,11 +19,11 @@ if (process.env.TEMPLATE) { test('some snapshots', t => { // t.snapshot('foo'); // t.snapshot('bar'); - t.assert(true); + t.pass(); }); test('another snapshot', t => { // t.snapshot('baz'); - t.assert(true); + t.pass(); }); } diff --git a/test/snapshot-removal/fixtures/only-test/test.js b/test/snapshot-removal/fixtures/only-test/test.js index 6a4f7e4f0..bfa11062d 100644 --- a/test/snapshot-removal/fixtures/only-test/test.js +++ b/test/snapshot-removal/fixtures/only-test/test.js @@ -6,22 +6,22 @@ if (process.env.TEMPLATE) { test('some snapshots', t => { t.snapshot('foo'); t.snapshot('bar'); - t.assert(true); + t.pass(); }); test('another snapshot', t => { t.snapshot('baz'); - t.assert(true); + t.pass(); }); } else { test.only('some snapshots', t => { // t.snapshot('foo'); // t.snapshot('bar'); - t.assert(true); + t.pass(); }); test('another snapshot', t => { // t.snapshot('baz'); - t.assert(true); + t.pass(); }); } diff --git a/test/snapshot-removal/fixtures/removal/test.js b/test/snapshot-removal/fixtures/removal/test.js index e3999d0db..2ec6c3f83 100644 --- a/test/snapshot-removal/fixtures/removal/test.js +++ b/test/snapshot-removal/fixtures/removal/test.js @@ -6,12 +6,12 @@ if (process.env.TEMPLATE) { test('some snapshots', t => { t.snapshot('foo'); t.snapshot('bar'); - t.assert(true); + t.pass(); }); test('another snapshot', t => { t.snapshot('baz'); - t.assert(true); + t.pass(); }); } else { const test = require('ava'); @@ -19,11 +19,11 @@ if (process.env.TEMPLATE) { test('some snapshots', t => { // t.snapshot('foo'); // t.snapshot('bar'); - t.assert(true); + t.pass(); }); test('another snapshot', t => { // t.snapshot('baz'); - t.assert(true); + t.pass(); }); } diff --git a/test/snapshot-removal/fixtures/skipped-snapshots-in-try/test.js b/test/snapshot-removal/fixtures/skipped-snapshots-in-try/test.js index 358c8f33d..4c3feccaa 100644 --- a/test/snapshot-removal/fixtures/skipped-snapshots-in-try/test.js +++ b/test/snapshot-removal/fixtures/skipped-snapshots-in-try/test.js @@ -10,7 +10,7 @@ if (process.env.TEMPLATE) { attempt.commit(); - t.assert(true); + t.pass(); }); } else { const test = require('ava'); @@ -22,6 +22,6 @@ if (process.env.TEMPLATE) { attempt.discard(); - t.assert(true); + t.pass(); }); } diff --git a/test/snapshot-removal/fixtures/skipped-snapshots/test.js b/test/snapshot-removal/fixtures/skipped-snapshots/test.js index da5e27974..70af06151 100644 --- a/test/snapshot-removal/fixtures/skipped-snapshots/test.js +++ b/test/snapshot-removal/fixtures/skipped-snapshots/test.js @@ -6,12 +6,12 @@ if (process.env.TEMPLATE) { test('some snapshots', t => { t.snapshot('foo'); t.snapshot('bar'); - t.assert(true); + t.pass(); }); test('another snapshot', t => { t.snapshot('baz'); - t.assert(true); + t.pass(); }); } else { const test = require('ava'); @@ -19,11 +19,11 @@ if (process.env.TEMPLATE) { test('some snapshots', t => { t.snapshot.skip('foo'); // t.snapshot('bar'); - t.assert(true); + t.pass(); }); test('another snapshot', t => { // t.snapshot('baz'); - t.assert(true); + t.pass(); }); } diff --git a/test/snapshot-removal/fixtures/skipped-tests/test.js b/test/snapshot-removal/fixtures/skipped-tests/test.js index 86d9495f6..265a90cc7 100644 --- a/test/snapshot-removal/fixtures/skipped-tests/test.js +++ b/test/snapshot-removal/fixtures/skipped-tests/test.js @@ -6,12 +6,12 @@ if (process.env.TEMPLATE) { test('some snapshots', t => { t.snapshot('foo'); t.snapshot('bar'); - t.assert(true); + t.pass(); }); test('another snapshot', t => { t.snapshot('baz'); - t.assert(true); + t.pass(); }); } else { const test = require('ava'); @@ -19,11 +19,11 @@ if (process.env.TEMPLATE) { test.skip('some snapshots', t => { t.snapshot('foo'); t.snapshot('bar'); - t.assert(true); + t.pass(); }); test('another snapshot', t => { // t.snapshot('baz'); - t.assert(true); + t.pass(); }); } diff --git a/test/snapshot-removal/fixtures/snapshot-dir/test/test.js b/test/snapshot-removal/fixtures/snapshot-dir/test/test.js index e3999d0db..2ec6c3f83 100644 --- a/test/snapshot-removal/fixtures/snapshot-dir/test/test.js +++ b/test/snapshot-removal/fixtures/snapshot-dir/test/test.js @@ -6,12 +6,12 @@ if (process.env.TEMPLATE) { test('some snapshots', t => { t.snapshot('foo'); t.snapshot('bar'); - t.assert(true); + t.pass(); }); test('another snapshot', t => { t.snapshot('baz'); - t.assert(true); + t.pass(); }); } else { const test = require('ava'); @@ -19,11 +19,11 @@ if (process.env.TEMPLATE) { test('some snapshots', t => { // t.snapshot('foo'); // t.snapshot('bar'); - t.assert(true); + t.pass(); }); test('another snapshot', t => { // t.snapshot('baz'); - t.assert(true); + t.pass(); }); } diff --git a/test/snapshot-removal/fixtures/try/test.js b/test/snapshot-removal/fixtures/try/test.js index 7efa546dd..2a36578b0 100644 --- a/test/snapshot-removal/fixtures/try/test.js +++ b/test/snapshot-removal/fixtures/try/test.js @@ -10,7 +10,7 @@ if (process.env.TEMPLATE) { attempt.commit(); - t.assert(true); + t.pass(); }); } else { const test = require('ava'); @@ -22,6 +22,6 @@ if (process.env.TEMPLATE) { attempt.discard(); - t.assert(true); + t.pass(); }); } From 50578d6dc7e804b5acc36ae6abcabe2f7255dbfa Mon Sep 17 00:00:00 2001 From: ninevra Date: Wed, 16 Dec 2020 01:29:57 -0800 Subject: [PATCH 18/34] Force not-ci to fix a CI-dependent test failure The test in 2bf983f, of t.snapshot.skip() preventing snapshot updates, behaves differently in CI than locally. --- test/snapshot-updates/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/snapshot-updates/test.js b/test/snapshot-updates/test.js index c4b96b7eb..b93fb7120 100644 --- a/test/snapshot-updates/test.js +++ b/test/snapshot-updates/test.js @@ -26,7 +26,7 @@ test('cannot update snapshots when selecting tests by line number', async t => { }); test('cannot update snapshots when skipping snapshot assertions', async t => { - const result = await t.throwsAsync(exec.fixture(['contains-skip-assertion.js', '-u'])); + const result = await t.throwsAsync(exec.fixture(['contains-skip-assertion.js', '-u'], {env: {AVA_FORCE_CI: 'not-ci'}})); t.snapshot(result.stats.failed, 'failed tests'); t.snapshot(result.stats.passed, 'passed tests'); t.snapshot(result.stats.unsavedSnapshots, 'files where snapshots could not be updated'); From ef354e2556876b9835c9ff2b6e76cb1d12d999b5 Mon Sep 17 00:00:00 2001 From: ninevra Date: Thu, 17 Dec 2020 15:56:00 -0800 Subject: [PATCH 19/34] Revert "Rename skippedSnapshots to skippingSnapshots" This reverts commit 2eaad192810b93e691658cca152c2cd376c9fc7f. skippedSnapshots better reflects the field's meaning, since it records whether any snapshot assertion has yet been skipped. This is different from skippingTests, which records whether any test will be skipped. --- lib/runner.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/runner.js b/lib/runner.js index 85f80ee58..1e2c75f03 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -29,7 +29,7 @@ class Runner extends Emittery { this.activeRunnables = new Set(); this.boundCompareTestSnapshot = this.compareTestSnapshot.bind(this); - this.skippingSnapshots = false; + this.skippedSnapshots = false; this.boundSkipSnapshot = this.skipSnapshot.bind(this); this.interrupted = false; this.snapshots = null; @@ -202,7 +202,7 @@ class Runner extends Emittery { } skipSnapshot() { - this.skippingSnapshots = true; + this.skippedSnapshots = true; } async saveSnapshotState() { @@ -212,7 +212,7 @@ class Runner extends Emittery { this.runOnlyExclusive || this.skippingTests || this.checkSelectedByLineNumbers || - this.skippingSnapshots + this.skippedSnapshots ) ) { return {cannotSave: true}; From 2ef3a5373fab9fbd3d35555507f22f0ce3b17672 Mon Sep 17 00:00:00 2001 From: ninevra Date: Thu, 17 Dec 2020 19:31:47 -0800 Subject: [PATCH 20/34] Test that absent snapshots don't throw This is the most common case: a test that didn't use snapshots before and doesn't now. --- .../fixtures/no-snapshots/package.json | 1 + .../fixtures/no-snapshots/test.js | 5 +++++ test/snapshot-removal/no-snapshots.js | 16 ++++++++++++++++ 3 files changed, 22 insertions(+) create mode 100644 test/snapshot-removal/fixtures/no-snapshots/package.json create mode 100644 test/snapshot-removal/fixtures/no-snapshots/test.js create mode 100644 test/snapshot-removal/no-snapshots.js diff --git a/test/snapshot-removal/fixtures/no-snapshots/package.json b/test/snapshot-removal/fixtures/no-snapshots/package.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/test/snapshot-removal/fixtures/no-snapshots/package.json @@ -0,0 +1 @@ +{} diff --git a/test/snapshot-removal/fixtures/no-snapshots/test.js b/test/snapshot-removal/fixtures/no-snapshots/test.js new file mode 100644 index 000000000..4f9782f12 --- /dev/null +++ b/test/snapshot-removal/fixtures/no-snapshots/test.js @@ -0,0 +1,5 @@ +const test = require('ava'); + +test('without snapshots', t => { + t.pass(); +}); diff --git a/test/snapshot-removal/no-snapshots.js b/test/snapshot-removal/no-snapshots.js new file mode 100644 index 000000000..e6d0f69bb --- /dev/null +++ b/test/snapshot-removal/no-snapshots.js @@ -0,0 +1,16 @@ +const test = require('@ava/test'); +const exec = require('../helpers/exec'); +const path = require('path'); + +test('removing non-existent snapshots doesn\'t throw', async t => { + // Execute fixture; this should try to unlink the nonexistent snapshots, and + // should not throw + const run = exec.fixture(['--update-snapshots'], { + cwd: exec.cwd(path.parse(__filename).name), + env: { + AVA_FORCE_CI: 'not-ci' + } + }); + + await t.notThrowsAsync(run); +}); From f25dc5216fe35809bf70597901a66773c78570d8 Mon Sep 17 00:00:00 2001 From: ninevra Date: Thu, 17 Dec 2020 23:07:23 -0800 Subject: [PATCH 21/34] Remove duplicate line number selection check Line number selection is and was done by throwing an error if both `--update-snapshots` and a line number range are passed. A second check was added at Runner.saveSnapshotState() in this PR. This duplicate check is not reachable in integration testing, and its value is negligible or negative. --- lib/runner.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/runner.js b/lib/runner.js index 1e2c75f03..4118b6637 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -211,7 +211,6 @@ class Runner extends Emittery { ( this.runOnlyExclusive || this.skippingTests || - this.checkSelectedByLineNumbers || this.skippedSnapshots ) ) { From 701896390512287059217130fbff99d8eebfc570 Mon Sep 17 00:00:00 2001 From: ninevra Date: Fri, 18 Dec 2020 12:50:49 -0800 Subject: [PATCH 22/34] Use temporary copies of test fixtures Allows tests to run concurrently. Promotes fs-extra to top-level devdependency. --- package.json | 1 + test/snapshot-removal/fixed-snapshot-dir.js | 4 +-- .../fixtures/fixed-snapshot-dir/test.js | 5 ++-- .../fixtures/only-test/test.js | 2 +- .../snapshot-removal/fixtures/removal/test.js | 6 ++-- .../fixtures/skipped-snapshots-in-try/test.js | 6 ++-- .../fixtures/skipped-snapshots/test.js | 6 ++-- .../fixtures/skipped-tests/test.js | 6 ++-- .../fixtures/snapshot-dir/test/test.js | 6 ++-- test/snapshot-removal/fixtures/try/test.js | 6 ++-- test/snapshot-removal/helpers/macros.js | 28 +++++++++++++++++-- test/snapshot-removal/only-test.js | 4 +-- test/snapshot-removal/removal.js | 10 +++---- .../skipped-snapshots-in-try.js | 4 +-- test/snapshot-removal/skipped-snapshots.js | 6 ++-- test/snapshot-removal/skipped-tests.js | 4 +-- test/snapshot-removal/snapshot-dir.js | 4 +-- test/snapshot-removal/try.js | 4 +-- 18 files changed, 62 insertions(+), 50 deletions(-) diff --git a/package.json b/package.json index 63963d8a8..0f2c31d91 100644 --- a/package.json +++ b/package.json @@ -123,6 +123,7 @@ "delay": "^4.4.0", "esm": "^3.2.25", "execa": "^4.1.0", + "fs-extra": "^9.0.1", "get-stream": "^6.0.0", "it-first": "^1.0.4", "proxyquire": "^2.1.3", diff --git a/test/snapshot-removal/fixed-snapshot-dir.js b/test/snapshot-removal/fixed-snapshot-dir.js index 061734a50..34f34bcb6 100644 --- a/test/snapshot-removal/fixed-snapshot-dir.js +++ b/test/snapshot-removal/fixed-snapshot-dir.js @@ -1,9 +1,9 @@ const test = require('@ava/test'); const exec = require('../helpers/exec'); -const {testSnapshotPruning} = require('./helpers/macros'); +const {testSnapshotPruningSafe} = require('./helpers/macros'); const path = require('path'); -test.serial('snapshots are removed from a custom snapshotDir', testSnapshotPruning, { +test('snapshots are removed from a custom snapshotDir', testSnapshotPruningSafe, { cwd: exec.cwd(path.parse(__filename).name), cli: ['--update-snapshots'], remove: true, diff --git a/test/snapshot-removal/fixtures/fixed-snapshot-dir/test.js b/test/snapshot-removal/fixtures/fixed-snapshot-dir/test.js index 2ec6c3f83..9f9e075e4 100644 --- a/test/snapshot-removal/fixtures/fixed-snapshot-dir/test.js +++ b/test/snapshot-removal/fixtures/fixed-snapshot-dir/test.js @@ -1,7 +1,8 @@ /* eslint-disable capitalized-comments, ava/no-identical-title */ +const test = require(process.env.AVA_PATH || 'ava'); + if (process.env.TEMPLATE) { - const test = require('ava'); test('some snapshots', t => { t.snapshot('foo'); @@ -14,8 +15,6 @@ if (process.env.TEMPLATE) { t.pass(); }); } else { - const test = require('ava'); - test('some snapshots', t => { // t.snapshot('foo'); // t.snapshot('bar'); diff --git a/test/snapshot-removal/fixtures/only-test/test.js b/test/snapshot-removal/fixtures/only-test/test.js index bfa11062d..48a161913 100644 --- a/test/snapshot-removal/fixtures/only-test/test.js +++ b/test/snapshot-removal/fixtures/only-test/test.js @@ -1,6 +1,6 @@ /* eslint-disable capitalized-comments, ava/no-only-test, ava/no-identical-title */ -const test = require('ava'); +const test = require(process.env.AVA_PATH || 'ava'); if (process.env.TEMPLATE) { test('some snapshots', t => { diff --git a/test/snapshot-removal/fixtures/removal/test.js b/test/snapshot-removal/fixtures/removal/test.js index 2ec6c3f83..1f7d5d9a9 100644 --- a/test/snapshot-removal/fixtures/removal/test.js +++ b/test/snapshot-removal/fixtures/removal/test.js @@ -1,8 +1,8 @@ /* eslint-disable capitalized-comments, ava/no-identical-title */ -if (process.env.TEMPLATE) { - const test = require('ava'); +const test = require(process.env.AVA_PATH || 'ava'); +if (process.env.TEMPLATE) { test('some snapshots', t => { t.snapshot('foo'); t.snapshot('bar'); @@ -14,8 +14,6 @@ if (process.env.TEMPLATE) { t.pass(); }); } else { - const test = require('ava'); - test('some snapshots', t => { // t.snapshot('foo'); // t.snapshot('bar'); diff --git a/test/snapshot-removal/fixtures/skipped-snapshots-in-try/test.js b/test/snapshot-removal/fixtures/skipped-snapshots-in-try/test.js index 4c3feccaa..65b425654 100644 --- a/test/snapshot-removal/fixtures/skipped-snapshots-in-try/test.js +++ b/test/snapshot-removal/fixtures/skipped-snapshots-in-try/test.js @@ -1,8 +1,8 @@ /* eslint-disable ava/no-identical-title */ -if (process.env.TEMPLATE) { - const test = require('ava'); +const test = require(process.env.AVA_PATH || 'ava'); +if (process.env.TEMPLATE) { test('skipped snapshots in try', async t => { const attempt = await t.try(tt => { tt.snapshot('in try'); @@ -13,8 +13,6 @@ if (process.env.TEMPLATE) { t.pass(); }); } else { - const test = require('ava'); - test('skipped snapshots in try', async t => { const attempt = await t.try(tt => { tt.snapshot.skip('in try'); diff --git a/test/snapshot-removal/fixtures/skipped-snapshots/test.js b/test/snapshot-removal/fixtures/skipped-snapshots/test.js index 70af06151..154a773f9 100644 --- a/test/snapshot-removal/fixtures/skipped-snapshots/test.js +++ b/test/snapshot-removal/fixtures/skipped-snapshots/test.js @@ -1,8 +1,8 @@ /* eslint-disable ava/no-skip-assert, capitalized-comments, ava/no-identical-title */ -if (process.env.TEMPLATE) { - const test = require('ava'); +const test = require(process.env.AVA_PATH || 'ava'); +if (process.env.TEMPLATE) { test('some snapshots', t => { t.snapshot('foo'); t.snapshot('bar'); @@ -14,8 +14,6 @@ if (process.env.TEMPLATE) { t.pass(); }); } else { - const test = require('ava'); - test('some snapshots', t => { t.snapshot.skip('foo'); // t.snapshot('bar'); diff --git a/test/snapshot-removal/fixtures/skipped-tests/test.js b/test/snapshot-removal/fixtures/skipped-tests/test.js index 265a90cc7..6dd84da3d 100644 --- a/test/snapshot-removal/fixtures/skipped-tests/test.js +++ b/test/snapshot-removal/fixtures/skipped-tests/test.js @@ -1,8 +1,8 @@ /* eslint-disable ava/no-skip-test, capitalized-comments, ava/no-identical-title */ -if (process.env.TEMPLATE) { - const test = require('ava'); +const test = require(process.env.AVA_PATH || 'ava'); +if (process.env.TEMPLATE) { test('some snapshots', t => { t.snapshot('foo'); t.snapshot('bar'); @@ -14,8 +14,6 @@ if (process.env.TEMPLATE) { t.pass(); }); } else { - const test = require('ava'); - test.skip('some snapshots', t => { t.snapshot('foo'); t.snapshot('bar'); diff --git a/test/snapshot-removal/fixtures/snapshot-dir/test/test.js b/test/snapshot-removal/fixtures/snapshot-dir/test/test.js index 2ec6c3f83..1f7d5d9a9 100644 --- a/test/snapshot-removal/fixtures/snapshot-dir/test/test.js +++ b/test/snapshot-removal/fixtures/snapshot-dir/test/test.js @@ -1,8 +1,8 @@ /* eslint-disable capitalized-comments, ava/no-identical-title */ -if (process.env.TEMPLATE) { - const test = require('ava'); +const test = require(process.env.AVA_PATH || 'ava'); +if (process.env.TEMPLATE) { test('some snapshots', t => { t.snapshot('foo'); t.snapshot('bar'); @@ -14,8 +14,6 @@ if (process.env.TEMPLATE) { t.pass(); }); } else { - const test = require('ava'); - test('some snapshots', t => { // t.snapshot('foo'); // t.snapshot('bar'); diff --git a/test/snapshot-removal/fixtures/try/test.js b/test/snapshot-removal/fixtures/try/test.js index 2a36578b0..c0bced761 100644 --- a/test/snapshot-removal/fixtures/try/test.js +++ b/test/snapshot-removal/fixtures/try/test.js @@ -1,8 +1,8 @@ /* eslint-disable ava/no-identical-title */ -if (process.env.TEMPLATE) { - const test = require('ava'); +const test = require(process.env.AVA_PATH || 'ava'); +if (process.env.TEMPLATE) { test('snapshots in try', async t => { const attempt = await t.try(tt => { tt.snapshot('in try'); @@ -13,8 +13,6 @@ if (process.env.TEMPLATE) { t.pass(); }); } else { - const test = require('ava'); - test('snapshots in try', async t => { const attempt = await t.try(tt => { tt.snapshot('in try'); diff --git a/test/snapshot-removal/helpers/macros.js b/test/snapshot-removal/helpers/macros.js index e9d5de4de..9634d0429 100644 --- a/test/snapshot-removal/helpers/macros.js +++ b/test/snapshot-removal/helpers/macros.js @@ -1,10 +1,32 @@ const fs = require('fs').promises; const exec = require('../../helpers/exec'); const path = require('path'); +const tempy = require('tempy'); +const fse = require('fs-extra'); + +function withTemporaryFixture(macro) { + const avaPath = path.resolve(path.join(__dirname, '..', '..', '..')); + + return async (t, {cwd, env, ...options}) => { + await tempy.directory.task(async temporary => { + await fse.copy(cwd, temporary); + await macro(t, { + cwd: temporary, + env: { + AVA_PATH: avaPath, + ...env + }, + ...options + }); + }); + }; +} + +module.exports.testSnapshotPruningSafe = withTemporaryFixture(testSnapshotPruning); async function testSnapshotPruning(t, { cwd, - ci = 'not-ci', + env, cli, remove, snapshotPath = 'test.js.snap', @@ -27,6 +49,7 @@ async function testSnapshotPruning(t, { const templateResult = exec.fixture(['--update-snapshots'], { cwd, env: { + ...env, AVA_FORCE_CI: 'not-ci', TEMPLATE: 'true' } @@ -42,7 +65,8 @@ async function testSnapshotPruning(t, { const run = exec.fixture(cli, { cwd, env: { - AVA_FORCE_CI: ci + AVA_FORCE_CI: 'not-ci', + ...env } }); diff --git a/test/snapshot-removal/only-test.js b/test/snapshot-removal/only-test.js index 80b461f7c..5f9fbf375 100644 --- a/test/snapshot-removal/only-test.js +++ b/test/snapshot-removal/only-test.js @@ -1,8 +1,8 @@ const test = require('@ava/test'); const exec = require('../helpers/exec'); -const {testSnapshotPruning} = require('./helpers/macros'); +const {testSnapshotPruningSafe} = require('./helpers/macros'); -test.serial('snapshots remain if using test.only', testSnapshotPruning, { +test('snapshots remain if using test.only', testSnapshotPruningSafe, { cwd: exec.cwd('only-test'), cli: ['--update-snapshots'], remove: false, diff --git a/test/snapshot-removal/removal.js b/test/snapshot-removal/removal.js index 9483d66c4..0ef7562fc 100644 --- a/test/snapshot-removal/removal.js +++ b/test/snapshot-removal/removal.js @@ -1,20 +1,20 @@ const test = require('@ava/test'); const exec = require('../helpers/exec'); -const {testSnapshotPruning} = require('./helpers/macros'); +const {testSnapshotPruningSafe} = require('./helpers/macros'); -test.serial('snapshots are removed when tests stop using them', testSnapshotPruning, { +test('snapshots are removed when tests stop using them', testSnapshotPruningSafe, { cwd: exec.cwd('removal'), cli: ['--update-snapshots'], remove: true }); -test.serial('snapshots remain if not updating', testSnapshotPruning, { +test('snapshots remain if not updating', testSnapshotPruningSafe, { cwd: exec.cwd('removal'), cli: [], remove: false }); -test.serial('snapshots remain if tests run with --match', testSnapshotPruning, { +test('snapshots remain if tests run with --match', testSnapshotPruningSafe, { cwd: exec.cwd('removal'), cli: ['--update-snapshots', '--match=\'*snapshot*\''], remove: false, @@ -24,7 +24,7 @@ test.serial('snapshots remain if tests run with --match', testSnapshotPruning, { } }); -test.serial('snapshots remain if tests selected by line numbers', testSnapshotPruning, { +test('snapshots remain if tests selected by line numbers', testSnapshotPruningSafe, { cwd: exec.cwd('removal'), cli: ['test.js:3-12', '--update-snapshots'], remove: false, diff --git a/test/snapshot-removal/skipped-snapshots-in-try.js b/test/snapshot-removal/skipped-snapshots-in-try.js index ad9ded111..e107b68d8 100644 --- a/test/snapshot-removal/skipped-snapshots-in-try.js +++ b/test/snapshot-removal/skipped-snapshots-in-try.js @@ -1,9 +1,9 @@ const test = require('@ava/test'); const exec = require('../helpers/exec'); -const {testSnapshotPruning} = require('./helpers/macros'); +const {testSnapshotPruningSafe} = require('./helpers/macros'); const path = require('path'); -test.serial('snapshots remain if skipped in a discarded try()', testSnapshotPruning, { +test('snapshots remain if skipped in a discarded try()', testSnapshotPruningSafe, { cwd: exec.cwd(path.parse(__filename).name), cli: ['--update-snapshots'], remove: false, diff --git a/test/snapshot-removal/skipped-snapshots.js b/test/snapshot-removal/skipped-snapshots.js index 74bb94c5f..42f78d713 100644 --- a/test/snapshot-removal/skipped-snapshots.js +++ b/test/snapshot-removal/skipped-snapshots.js @@ -1,8 +1,8 @@ const test = require('@ava/test'); const exec = require('../helpers/exec'); -const {testSnapshotPruning} = require('./helpers/macros'); +const {testSnapshotPruningSafe} = require('./helpers/macros'); -test.serial('snapshots remain if snapshot assertions are skipped (-u)', testSnapshotPruning, { +test('snapshots remain if snapshot assertions are skipped (-u)', testSnapshotPruningSafe, { cwd: exec.cwd('skipped-snapshots'), cli: ['--update-snapshots'], remove: false, @@ -14,7 +14,7 @@ test.serial('snapshots remain if snapshot assertions are skipped (-u)', testSnap } }); -test.serial('snapshots remain if snapshot assertions are skipped (!-u)', testSnapshotPruning, { +test('snapshots remain if snapshot assertions are skipped (!-u)', testSnapshotPruningSafe, { cwd: exec.cwd('skipped-snapshots'), cli: [], remove: false diff --git a/test/snapshot-removal/skipped-tests.js b/test/snapshot-removal/skipped-tests.js index bcb1c1117..8fcb04cf8 100644 --- a/test/snapshot-removal/skipped-tests.js +++ b/test/snapshot-removal/skipped-tests.js @@ -1,8 +1,8 @@ const test = require('@ava/test'); const exec = require('../helpers/exec'); -const {testSnapshotPruning} = require('./helpers/macros'); +const {testSnapshotPruningSafe} = require('./helpers/macros'); -test.serial('snapshots remain if tests are skipped', testSnapshotPruning, { +test('snapshots remain if tests are skipped', testSnapshotPruningSafe, { cwd: exec.cwd('skipped-tests'), cli: ['--update-snapshots'], remove: false, diff --git a/test/snapshot-removal/snapshot-dir.js b/test/snapshot-removal/snapshot-dir.js index ba1a1af3c..ac93e12eb 100644 --- a/test/snapshot-removal/snapshot-dir.js +++ b/test/snapshot-removal/snapshot-dir.js @@ -1,9 +1,9 @@ const test = require('@ava/test'); const exec = require('../helpers/exec'); -const {testSnapshotPruning} = require('./helpers/macros'); +const {testSnapshotPruningSafe} = require('./helpers/macros'); const path = require('path'); -test.serial('snapshots are removed from a snapshot directory', testSnapshotPruning, { +test('snapshots are removed from a snapshot directory', testSnapshotPruningSafe, { cwd: exec.cwd(path.parse(__filename).name), cli: ['--update-snapshots'], remove: true, diff --git a/test/snapshot-removal/try.js b/test/snapshot-removal/try.js index 46f9fcb55..78d7a879c 100644 --- a/test/snapshot-removal/try.js +++ b/test/snapshot-removal/try.js @@ -1,9 +1,9 @@ const test = require('@ava/test'); const exec = require('../helpers/exec'); -const {testSnapshotPruning} = require('./helpers/macros'); +const {testSnapshotPruningSafe} = require('./helpers/macros'); const path = require('path'); -test.serial('snapshots remain if used in a discarded try()', testSnapshotPruning, { +test('snapshots remain if used in a discarded try()', testSnapshotPruningSafe, { cwd: exec.cwd(path.parse(__filename).name), cli: ['--update-snapshots'], remove: false From bc3adf976bcad6eccfbc405f34bf230db4b06cf1 Mon Sep 17 00:00:00 2001 From: ninevra Date: Fri, 18 Dec 2020 15:16:17 -0800 Subject: [PATCH 23/34] Clean up, refactor test fixtures --- test/snapshot-removal/fixtures/fixed-snapshot-dir/test.js | 6 ------ test/snapshot-removal/fixtures/only-test/test.js | 5 ----- test/snapshot-removal/fixtures/removal/test.js | 5 ----- .../fixtures/skipped-snapshots-in-try/test.js | 2 -- test/snapshot-removal/fixtures/skipped-snapshots/test.js | 4 ---- test/snapshot-removal/fixtures/skipped-tests/test.js | 5 ----- test/snapshot-removal/fixtures/snapshot-dir/test/test.js | 5 ----- test/snapshot-removal/fixtures/try/test.js | 2 -- 8 files changed, 34 deletions(-) diff --git a/test/snapshot-removal/fixtures/fixed-snapshot-dir/test.js b/test/snapshot-removal/fixtures/fixed-snapshot-dir/test.js index 9f9e075e4..54da975d0 100644 --- a/test/snapshot-removal/fixtures/fixed-snapshot-dir/test.js +++ b/test/snapshot-removal/fixtures/fixed-snapshot-dir/test.js @@ -1,9 +1,6 @@ -/* eslint-disable capitalized-comments, ava/no-identical-title */ - const test = require(process.env.AVA_PATH || 'ava'); if (process.env.TEMPLATE) { - test('some snapshots', t => { t.snapshot('foo'); t.snapshot('bar'); @@ -16,13 +13,10 @@ if (process.env.TEMPLATE) { }); } else { test('some snapshots', t => { - // t.snapshot('foo'); - // t.snapshot('bar'); t.pass(); }); test('another snapshot', t => { - // t.snapshot('baz'); t.pass(); }); } diff --git a/test/snapshot-removal/fixtures/only-test/test.js b/test/snapshot-removal/fixtures/only-test/test.js index 48a161913..e31f803ba 100644 --- a/test/snapshot-removal/fixtures/only-test/test.js +++ b/test/snapshot-removal/fixtures/only-test/test.js @@ -1,5 +1,3 @@ -/* eslint-disable capitalized-comments, ava/no-only-test, ava/no-identical-title */ - const test = require(process.env.AVA_PATH || 'ava'); if (process.env.TEMPLATE) { @@ -15,13 +13,10 @@ if (process.env.TEMPLATE) { }); } else { test.only('some snapshots', t => { - // t.snapshot('foo'); - // t.snapshot('bar'); t.pass(); }); test('another snapshot', t => { - // t.snapshot('baz'); t.pass(); }); } diff --git a/test/snapshot-removal/fixtures/removal/test.js b/test/snapshot-removal/fixtures/removal/test.js index 1f7d5d9a9..54da975d0 100644 --- a/test/snapshot-removal/fixtures/removal/test.js +++ b/test/snapshot-removal/fixtures/removal/test.js @@ -1,5 +1,3 @@ -/* eslint-disable capitalized-comments, ava/no-identical-title */ - const test = require(process.env.AVA_PATH || 'ava'); if (process.env.TEMPLATE) { @@ -15,13 +13,10 @@ if (process.env.TEMPLATE) { }); } else { test('some snapshots', t => { - // t.snapshot('foo'); - // t.snapshot('bar'); t.pass(); }); test('another snapshot', t => { - // t.snapshot('baz'); t.pass(); }); } diff --git a/test/snapshot-removal/fixtures/skipped-snapshots-in-try/test.js b/test/snapshot-removal/fixtures/skipped-snapshots-in-try/test.js index 65b425654..10391d317 100644 --- a/test/snapshot-removal/fixtures/skipped-snapshots-in-try/test.js +++ b/test/snapshot-removal/fixtures/skipped-snapshots-in-try/test.js @@ -1,5 +1,3 @@ -/* eslint-disable ava/no-identical-title */ - const test = require(process.env.AVA_PATH || 'ava'); if (process.env.TEMPLATE) { diff --git a/test/snapshot-removal/fixtures/skipped-snapshots/test.js b/test/snapshot-removal/fixtures/skipped-snapshots/test.js index 154a773f9..1f7925638 100644 --- a/test/snapshot-removal/fixtures/skipped-snapshots/test.js +++ b/test/snapshot-removal/fixtures/skipped-snapshots/test.js @@ -1,5 +1,3 @@ -/* eslint-disable ava/no-skip-assert, capitalized-comments, ava/no-identical-title */ - const test = require(process.env.AVA_PATH || 'ava'); if (process.env.TEMPLATE) { @@ -16,12 +14,10 @@ if (process.env.TEMPLATE) { } else { test('some snapshots', t => { t.snapshot.skip('foo'); - // t.snapshot('bar'); t.pass(); }); test('another snapshot', t => { - // t.snapshot('baz'); t.pass(); }); } diff --git a/test/snapshot-removal/fixtures/skipped-tests/test.js b/test/snapshot-removal/fixtures/skipped-tests/test.js index 6dd84da3d..cc3251e42 100644 --- a/test/snapshot-removal/fixtures/skipped-tests/test.js +++ b/test/snapshot-removal/fixtures/skipped-tests/test.js @@ -1,5 +1,3 @@ -/* eslint-disable ava/no-skip-test, capitalized-comments, ava/no-identical-title */ - const test = require(process.env.AVA_PATH || 'ava'); if (process.env.TEMPLATE) { @@ -15,13 +13,10 @@ if (process.env.TEMPLATE) { }); } else { test.skip('some snapshots', t => { - t.snapshot('foo'); - t.snapshot('bar'); t.pass(); }); test('another snapshot', t => { - // t.snapshot('baz'); t.pass(); }); } diff --git a/test/snapshot-removal/fixtures/snapshot-dir/test/test.js b/test/snapshot-removal/fixtures/snapshot-dir/test/test.js index 1f7d5d9a9..54da975d0 100644 --- a/test/snapshot-removal/fixtures/snapshot-dir/test/test.js +++ b/test/snapshot-removal/fixtures/snapshot-dir/test/test.js @@ -1,5 +1,3 @@ -/* eslint-disable capitalized-comments, ava/no-identical-title */ - const test = require(process.env.AVA_PATH || 'ava'); if (process.env.TEMPLATE) { @@ -15,13 +13,10 @@ if (process.env.TEMPLATE) { }); } else { test('some snapshots', t => { - // t.snapshot('foo'); - // t.snapshot('bar'); t.pass(); }); test('another snapshot', t => { - // t.snapshot('baz'); t.pass(); }); } diff --git a/test/snapshot-removal/fixtures/try/test.js b/test/snapshot-removal/fixtures/try/test.js index c0bced761..07efe59d7 100644 --- a/test/snapshot-removal/fixtures/try/test.js +++ b/test/snapshot-removal/fixtures/try/test.js @@ -1,5 +1,3 @@ -/* eslint-disable ava/no-identical-title */ - const test = require(process.env.AVA_PATH || 'ava'); if (process.env.TEMPLATE) { From bf7eeafb3ede9c5a5e9d2736081d61e9caae472c Mon Sep 17 00:00:00 2001 From: ninevra Date: Fri, 18 Dec 2020 18:33:06 -0800 Subject: [PATCH 24/34] Consolidate tests in one file Tests were previously split to allow concurrency among fixtures and serial execution in individual fixtures. This is no longer necessary, since tests no longer need exclusive access to their fixtures. --- test/snapshot-removal/fixed-snapshot-dir.js | 12 -- test/snapshot-removal/no-snapshots.js | 16 --- test/snapshot-removal/only-test.js | 14 -- test/snapshot-removal/removal.js | 35 ----- .../skipped-snapshots-in-try.js | 15 --- test/snapshot-removal/skipped-snapshots.js | 21 --- test/snapshot-removal/skipped-tests.js | 14 -- test/snapshot-removal/snapshot-dir.js | 12 -- .../snapshots/only-test.js.md | 15 --- .../snapshots/only-test.js.snap | Bin 180 -> 0 bytes test/snapshot-removal/snapshots/removal.js.md | 17 --- .../snapshots/removal.js.snap | Bin 209 -> 0 bytes .../snapshots/skipped-snapshots-in-try.js.md | 15 --- .../skipped-snapshots-in-try.js.snap | Bin 179 -> 0 bytes .../snapshots/skipped-snapshots.js.md | 15 --- .../snapshots/skipped-snapshots.js.snap | Bin 179 -> 0 bytes .../snapshots/skipped-tests.js.md | 15 --- .../snapshots/skipped-tests.js.snap | Bin 180 -> 0 bytes test/snapshot-removal/snapshots/test.js.md | 57 ++++++++ test/snapshot-removal/snapshots/test.js.snap | Bin 0 -> 393 bytes test/snapshot-removal/test.js | 122 ++++++++++++++++++ test/snapshot-removal/try.js | 10 -- 22 files changed, 179 insertions(+), 226 deletions(-) delete mode 100644 test/snapshot-removal/fixed-snapshot-dir.js delete mode 100644 test/snapshot-removal/no-snapshots.js delete mode 100644 test/snapshot-removal/only-test.js delete mode 100644 test/snapshot-removal/removal.js delete mode 100644 test/snapshot-removal/skipped-snapshots-in-try.js delete mode 100644 test/snapshot-removal/skipped-snapshots.js delete mode 100644 test/snapshot-removal/skipped-tests.js delete mode 100644 test/snapshot-removal/snapshot-dir.js delete mode 100644 test/snapshot-removal/snapshots/only-test.js.md delete mode 100644 test/snapshot-removal/snapshots/only-test.js.snap delete mode 100644 test/snapshot-removal/snapshots/removal.js.md delete mode 100644 test/snapshot-removal/snapshots/removal.js.snap delete mode 100644 test/snapshot-removal/snapshots/skipped-snapshots-in-try.js.md delete mode 100644 test/snapshot-removal/snapshots/skipped-snapshots-in-try.js.snap delete mode 100644 test/snapshot-removal/snapshots/skipped-snapshots.js.md delete mode 100644 test/snapshot-removal/snapshots/skipped-snapshots.js.snap delete mode 100644 test/snapshot-removal/snapshots/skipped-tests.js.md delete mode 100644 test/snapshot-removal/snapshots/skipped-tests.js.snap create mode 100644 test/snapshot-removal/snapshots/test.js.md create mode 100644 test/snapshot-removal/snapshots/test.js.snap create mode 100644 test/snapshot-removal/test.js delete mode 100644 test/snapshot-removal/try.js diff --git a/test/snapshot-removal/fixed-snapshot-dir.js b/test/snapshot-removal/fixed-snapshot-dir.js deleted file mode 100644 index 34f34bcb6..000000000 --- a/test/snapshot-removal/fixed-snapshot-dir.js +++ /dev/null @@ -1,12 +0,0 @@ -const test = require('@ava/test'); -const exec = require('../helpers/exec'); -const {testSnapshotPruningSafe} = require('./helpers/macros'); -const path = require('path'); - -test('snapshots are removed from a custom snapshotDir', testSnapshotPruningSafe, { - cwd: exec.cwd(path.parse(__filename).name), - cli: ['--update-snapshots'], - remove: true, - snapshotPath: path.join('fixedSnapshotDir', 'test.js.snap'), - reportPath: path.join('fixedSnapshotDir', 'test.js.md') -}); diff --git a/test/snapshot-removal/no-snapshots.js b/test/snapshot-removal/no-snapshots.js deleted file mode 100644 index e6d0f69bb..000000000 --- a/test/snapshot-removal/no-snapshots.js +++ /dev/null @@ -1,16 +0,0 @@ -const test = require('@ava/test'); -const exec = require('../helpers/exec'); -const path = require('path'); - -test('removing non-existent snapshots doesn\'t throw', async t => { - // Execute fixture; this should try to unlink the nonexistent snapshots, and - // should not throw - const run = exec.fixture(['--update-snapshots'], { - cwd: exec.cwd(path.parse(__filename).name), - env: { - AVA_FORCE_CI: 'not-ci' - } - }); - - await t.notThrowsAsync(run); -}); diff --git a/test/snapshot-removal/only-test.js b/test/snapshot-removal/only-test.js deleted file mode 100644 index 5f9fbf375..000000000 --- a/test/snapshot-removal/only-test.js +++ /dev/null @@ -1,14 +0,0 @@ -const test = require('@ava/test'); -const exec = require('../helpers/exec'); -const {testSnapshotPruningSafe} = require('./helpers/macros'); - -test('snapshots remain if using test.only', testSnapshotPruningSafe, { - cwd: exec.cwd('only-test'), - cli: ['--update-snapshots'], - remove: false, - checkRun: async (t, run) => { - await t.notThrowsAsync(run, 'Expected fixture not to throw'); - const result = await run; - t.snapshot(result.stats.unsavedSnapshots, 'files where snapshots could not be updated'); - } -}); diff --git a/test/snapshot-removal/removal.js b/test/snapshot-removal/removal.js deleted file mode 100644 index 0ef7562fc..000000000 --- a/test/snapshot-removal/removal.js +++ /dev/null @@ -1,35 +0,0 @@ -const test = require('@ava/test'); -const exec = require('../helpers/exec'); -const {testSnapshotPruningSafe} = require('./helpers/macros'); - -test('snapshots are removed when tests stop using them', testSnapshotPruningSafe, { - cwd: exec.cwd('removal'), - cli: ['--update-snapshots'], - remove: true -}); - -test('snapshots remain if not updating', testSnapshotPruningSafe, { - cwd: exec.cwd('removal'), - cli: [], - remove: false -}); - -test('snapshots remain if tests run with --match', testSnapshotPruningSafe, { - cwd: exec.cwd('removal'), - cli: ['--update-snapshots', '--match=\'*snapshot*\''], - remove: false, - checkRun: async (t, run) => { - const result = await t.throwsAsync(run, undefined, 'Expected fixture to throw'); - t.snapshot(exec.cleanOutput(result.stderr), 'stderr'); - } -}); - -test('snapshots remain if tests selected by line numbers', testSnapshotPruningSafe, { - cwd: exec.cwd('removal'), - cli: ['test.js:3-12', '--update-snapshots'], - remove: false, - checkRun: async (t, run) => { - const result = await t.throwsAsync(run, undefined, 'Expected fixture to throw'); - t.snapshot(exec.cleanOutput(result.stderr), 'stderr'); - } -}); diff --git a/test/snapshot-removal/skipped-snapshots-in-try.js b/test/snapshot-removal/skipped-snapshots-in-try.js deleted file mode 100644 index e107b68d8..000000000 --- a/test/snapshot-removal/skipped-snapshots-in-try.js +++ /dev/null @@ -1,15 +0,0 @@ -const test = require('@ava/test'); -const exec = require('../helpers/exec'); -const {testSnapshotPruningSafe} = require('./helpers/macros'); -const path = require('path'); - -test('snapshots remain if skipped in a discarded try()', testSnapshotPruningSafe, { - cwd: exec.cwd(path.parse(__filename).name), - cli: ['--update-snapshots'], - remove: false, - checkRun: async (t, run) => { - await t.notThrowsAsync(run, 'Expected fixture not to throw'); - const result = await run; - t.snapshot(result.stats.unsavedSnapshots, 'files where snapshots could not be updated'); - } -}); diff --git a/test/snapshot-removal/skipped-snapshots.js b/test/snapshot-removal/skipped-snapshots.js deleted file mode 100644 index 42f78d713..000000000 --- a/test/snapshot-removal/skipped-snapshots.js +++ /dev/null @@ -1,21 +0,0 @@ -const test = require('@ava/test'); -const exec = require('../helpers/exec'); -const {testSnapshotPruningSafe} = require('./helpers/macros'); - -test('snapshots remain if snapshot assertions are skipped (-u)', testSnapshotPruningSafe, { - cwd: exec.cwd('skipped-snapshots'), - cli: ['--update-snapshots'], - remove: false, - checkRun: async (t, run) => { - const result = await t.throwsAsync(run, { - message: /Snapshot assertions cannot be skipped when updating snapshots/ - }, 'Expected fixture to throw'); - t.snapshot(result.stats.unsavedSnapshots, 'files where snapshots could not be updated'); - } -}); - -test('snapshots remain if snapshot assertions are skipped (!-u)', testSnapshotPruningSafe, { - cwd: exec.cwd('skipped-snapshots'), - cli: [], - remove: false -}); diff --git a/test/snapshot-removal/skipped-tests.js b/test/snapshot-removal/skipped-tests.js deleted file mode 100644 index 8fcb04cf8..000000000 --- a/test/snapshot-removal/skipped-tests.js +++ /dev/null @@ -1,14 +0,0 @@ -const test = require('@ava/test'); -const exec = require('../helpers/exec'); -const {testSnapshotPruningSafe} = require('./helpers/macros'); - -test('snapshots remain if tests are skipped', testSnapshotPruningSafe, { - cwd: exec.cwd('skipped-tests'), - cli: ['--update-snapshots'], - remove: false, - checkRun: async (t, run) => { - await t.notThrowsAsync(run, 'Expected fixture not to throw'); - const result = await run; - t.snapshot(result.stats.unsavedSnapshots, 'files where snapshots could not be updated'); - } -}); diff --git a/test/snapshot-removal/snapshot-dir.js b/test/snapshot-removal/snapshot-dir.js deleted file mode 100644 index ac93e12eb..000000000 --- a/test/snapshot-removal/snapshot-dir.js +++ /dev/null @@ -1,12 +0,0 @@ -const test = require('@ava/test'); -const exec = require('../helpers/exec'); -const {testSnapshotPruningSafe} = require('./helpers/macros'); -const path = require('path'); - -test('snapshots are removed from a snapshot directory', testSnapshotPruningSafe, { - cwd: exec.cwd(path.parse(__filename).name), - cli: ['--update-snapshots'], - remove: true, - snapshotPath: path.join('test', 'snapshots', 'test.js.snap'), - reportPath: path.join('test', 'snapshots', 'test.js.md') -}); diff --git a/test/snapshot-removal/snapshots/only-test.js.md b/test/snapshot-removal/snapshots/only-test.js.md deleted file mode 100644 index a017467c2..000000000 --- a/test/snapshot-removal/snapshots/only-test.js.md +++ /dev/null @@ -1,15 +0,0 @@ -# Snapshot report for `test/snapshot-removal/only-test.js` - -The actual snapshot is saved in `only-test.js.snap`. - -Generated by [AVA](https://avajs.dev). - -## snapshots remain if using test.only - -> files where snapshots could not be updated - - [ - { - file: 'test.js', - }, - ] diff --git a/test/snapshot-removal/snapshots/only-test.js.snap b/test/snapshot-removal/snapshots/only-test.js.snap deleted file mode 100644 index 9d9f82c2fcbe5d1aea83fe09f8ae322e6bd01c9c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 180 zcmZ<^b5sbzq8b(RW diff --git a/test/snapshot-removal/snapshots/removal.js.md b/test/snapshot-removal/snapshots/removal.js.md deleted file mode 100644 index fd7c387e7..000000000 --- a/test/snapshot-removal/snapshots/removal.js.md +++ /dev/null @@ -1,17 +0,0 @@ -# Snapshot report for `test/snapshot-removal/removal.js` - -The actual snapshot is saved in `removal.js.snap`. - -Generated by [AVA](https://avajs.dev). - -## snapshots remain if tests run with --match - -> stderr - - 'Snapshots cannot be updated when matching specific tests.' - -## snapshots remain if tests selected by line numbers - -> stderr - - 'Snapshots cannot be updated when selecting specific tests by their line number.' diff --git a/test/snapshot-removal/snapshots/removal.js.snap b/test/snapshot-removal/snapshots/removal.js.snap deleted file mode 100644 index bd0cf20f12bdd819d612ad6c81e01552a3fd2aa2..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 209 zcmV;?051PQRzV>_v)ydZ<85qH$OMsY} zfgP-hk(EJ^(K0wMv7k63zob|pIWaFUzeFJ^RiU&XC9xzmMWH++HBTWou_QSoGcR4C zxF9t-Gc7Y&p(M4qq*xEd7=MDs6sP8-CYNBEqmWdoP?C|FS)`DYnU|`NSDKrYTBHX6 LWnU`Y-T(jqirZKC diff --git a/test/snapshot-removal/snapshots/skipped-snapshots-in-try.js.md b/test/snapshot-removal/snapshots/skipped-snapshots-in-try.js.md deleted file mode 100644 index f409ba616..000000000 --- a/test/snapshot-removal/snapshots/skipped-snapshots-in-try.js.md +++ /dev/null @@ -1,15 +0,0 @@ -# Snapshot report for `test/snapshot-removal/skipped-snapshots-in-try.js` - -The actual snapshot is saved in `skipped-snapshots-in-try.js.snap`. - -Generated by [AVA](https://avajs.dev). - -## snapshots remain if skipped in a discarded try() - -> files where snapshots could not be updated - - [ - { - file: 'test.js', - }, - ] diff --git a/test/snapshot-removal/snapshots/skipped-snapshots-in-try.js.snap b/test/snapshot-removal/snapshots/skipped-snapshots-in-try.js.snap deleted file mode 100644 index 47a7546e25322dce9c015ed81ea6c736b17f3c4d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 179 zcmV;k08IZuRzVgQl<(Teeq#uh200000000A1 zU|?WiWbk(4jL|GE_V_tVeOaZC!vaPI5arz{thG$jHFR&B$m2*OUyTGhv$8{FAa$lS>$x;7kDl0R~21ur_|68YU4& hW(G!9K}MFe%$!shhrJ}VxI{0j7y$DMj>fG30020_Nb~>z diff --git a/test/snapshot-removal/snapshots/skipped-snapshots.js.md b/test/snapshot-removal/snapshots/skipped-snapshots.js.md deleted file mode 100644 index 04f0c7ce8..000000000 --- a/test/snapshot-removal/snapshots/skipped-snapshots.js.md +++ /dev/null @@ -1,15 +0,0 @@ -# Snapshot report for `test/snapshot-removal/skipped-snapshots.js` - -The actual snapshot is saved in `skipped-snapshots.js.snap`. - -Generated by [AVA](https://avajs.dev). - -## snapshots remain if snapshot assertions are skipped (-u) - -> files where snapshots could not be updated - - [ - { - file: 'test.js', - }, - ] diff --git a/test/snapshot-removal/snapshots/skipped-snapshots.js.snap b/test/snapshot-removal/snapshots/skipped-snapshots.js.snap deleted file mode 100644 index da4f7d8b775b297a960387636660efb00f3f4717..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 179 zcmZ<^b5sb^-kjciM>ZwcBrW+U?oXNz*#M84UA@`4isgF|68J?as eO+7l9Gc8vd@Kt+e6?Vpj88RfjiQc#t=n?=Ju|#43 diff --git a/test/snapshot-removal/snapshots/skipped-tests.js.md b/test/snapshot-removal/snapshots/skipped-tests.js.md deleted file mode 100644 index 9a67b4ea5..000000000 --- a/test/snapshot-removal/snapshots/skipped-tests.js.md +++ /dev/null @@ -1,15 +0,0 @@ -# Snapshot report for `test/snapshot-removal/skipped-tests.js` - -The actual snapshot is saved in `skipped-tests.js.snap`. - -Generated by [AVA](https://avajs.dev). - -## snapshots remain if tests are skipped - -> files where snapshots could not be updated - - [ - { - file: 'test.js', - }, - ] diff --git a/test/snapshot-removal/snapshots/skipped-tests.js.snap b/test/snapshot-removal/snapshots/skipped-tests.js.snap deleted file mode 100644 index dd4bc7cd13918573b35565428838b435aec45e55..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 180 zcmZ<^b5sbm^5@rLP3#HPPU6=i0)&eN&17U&cJP&`Sg diff --git a/test/snapshot-removal/snapshots/test.js.md b/test/snapshot-removal/snapshots/test.js.md new file mode 100644 index 000000000..82ab6f81d --- /dev/null +++ b/test/snapshot-removal/snapshots/test.js.md @@ -0,0 +1,57 @@ +# Snapshot report for `test/snapshot-removal/test.js` + +The actual snapshot is saved in `test.js.snap`. + +Generated by [AVA](https://avajs.dev). + +## snapshots remain if skipped in a discarded try() + +> files where snapshots could not be updated + + [ + { + file: 'test.js', + }, + ] + +## snapshots remain if snapshot assertions are skipped (-u) + +> files where snapshots could not be updated + + [ + { + file: 'test.js', + }, + ] + +## snapshots remain if tests are skipped + +> files where snapshots could not be updated + + [ + { + file: 'test.js', + }, + ] + +## snapshots remain if tests run with --match + +> stderr + + 'Snapshots cannot be updated when matching specific tests.' + +## snapshots remain if tests selected by line numbers + +> stderr + + 'Snapshots cannot be updated when selecting specific tests by their line number.' + +## snapshots remain if using test.only + +> files where snapshots could not be updated + + [ + { + file: 'test.js', + }, + ] diff --git a/test/snapshot-removal/snapshots/test.js.snap b/test/snapshot-removal/snapshots/test.js.snap new file mode 100644 index 0000000000000000000000000000000000000000..123f0ba154934f609c66d21d9fbf83bec86998b6 GIT binary patch literal 393 zcmV;40e1dDRzVz;wK56zdWE0olI?T}&wi|zwrZzs+e&EjH@ zpR?4LRr)wAV1$S=F)++I(7i9@&vLb$KF3dI7q(nu1dHAPVrB+*uxl7u83Y+EgYyy# ziZk*{iWQO*^YZdb6p~UEN()jFOHxx5$}>{)6mk%(iMseQj;^&GLscbQj1H9 z^-zrQCumG@YEEi$38py;NtFsE8L62?3OSj1sS0_exk;%-2*WuTnRI}(5fBSAvI#P> zIu;crRx&ceSp59_42)b1jEoG7+>DGia81cTIuoXe%|9s%XgMPjoGBn6z`)20*2WK1 n!z9AU4D^T~BTHIlPAZJU4sxJgR`IA~hJ<4PTV>z@dIJCeh(M`i literal 0 HcmV?d00001 diff --git a/test/snapshot-removal/test.js b/test/snapshot-removal/test.js new file mode 100644 index 000000000..15bcc9472 --- /dev/null +++ b/test/snapshot-removal/test.js @@ -0,0 +1,122 @@ +const test = require('@ava/test'); +const exec = require('../helpers/exec'); +const {testSnapshotPruningSafe} = require('./helpers/macros'); +const path = require('path'); + +test('snapshots are removed when tests stop using them', testSnapshotPruningSafe, { + cwd: exec.cwd('removal'), + cli: ['--update-snapshots'], + remove: true +}); + +test('snapshots are removed from a snapshot directory', testSnapshotPruningSafe, { + cwd: exec.cwd('snapshot-dir'), + cli: ['--update-snapshots'], + remove: true, + snapshotPath: path.join('test', 'snapshots', 'test.js.snap'), + reportPath: path.join('test', 'snapshots', 'test.js.md') +}); + +test('snapshots are removed from a custom snapshotDir', testSnapshotPruningSafe, { + cwd: exec.cwd('fixed-snapshot-dir'), + cli: ['--update-snapshots'], + remove: true, + snapshotPath: path.join('fixedSnapshotDir', 'test.js.snap'), + reportPath: path.join('fixedSnapshotDir', 'test.js.md') +}); + +test('removing non-existent snapshots doesn\'t throw', async t => { + // Execute fixture; this should try to unlink the nonexistent snapshots, and + // should not throw + const run = exec.fixture(['--update-snapshots'], { + cwd: exec.cwd('no-snapshots'), + env: { + AVA_FORCE_CI: 'not-ci' + } + }); + + await t.notThrowsAsync(run); +}); + +test('snapshots remain if not updating', testSnapshotPruningSafe, { + cwd: exec.cwd('removal'), + cli: [], + remove: false +}); + +test('snapshots remain if tests run with --match', testSnapshotPruningSafe, { + cwd: exec.cwd('removal'), + cli: ['--update-snapshots', '--match=\'*snapshot*\''], + remove: false, + checkRun: async (t, run) => { + const result = await t.throwsAsync(run, undefined, 'Expected fixture to throw'); + t.snapshot(exec.cleanOutput(result.stderr), 'stderr'); + } +}); + +test('snapshots remain if tests selected by line numbers', testSnapshotPruningSafe, { + cwd: exec.cwd('removal'), + cli: ['test.js:3-12', '--update-snapshots'], + remove: false, + checkRun: async (t, run) => { + const result = await t.throwsAsync(run, undefined, 'Expected fixture to throw'); + t.snapshot(exec.cleanOutput(result.stderr), 'stderr'); + } +}); + +test('snapshots remain if using test.only', testSnapshotPruningSafe, { + cwd: exec.cwd('only-test'), + cli: ['--update-snapshots'], + remove: false, + checkRun: async (t, run) => { + await t.notThrowsAsync(run, 'Expected fixture not to throw'); + const result = await run; + t.snapshot(result.stats.unsavedSnapshots, 'files where snapshots could not be updated'); + } +}); + +test('snapshots remain if tests are skipped', testSnapshotPruningSafe, { + cwd: exec.cwd('skipped-tests'), + cli: ['--update-snapshots'], + remove: false, + checkRun: async (t, run) => { + await t.notThrowsAsync(run, 'Expected fixture not to throw'); + const result = await run; + t.snapshot(result.stats.unsavedSnapshots, 'files where snapshots could not be updated'); + } +}); + +test('snapshots remain if snapshot assertions are skipped (-u)', testSnapshotPruningSafe, { + cwd: exec.cwd('skipped-snapshots'), + cli: ['--update-snapshots'], + remove: false, + checkRun: async (t, run) => { + const result = await t.throwsAsync(run, { + message: /Snapshot assertions cannot be skipped when updating snapshots/ + }, 'Expected fixture to throw'); + t.snapshot(result.stats.unsavedSnapshots, 'files where snapshots could not be updated'); + } +}); + +test('snapshots remain if snapshot assertions are skipped (!-u)', testSnapshotPruningSafe, { + cwd: exec.cwd('skipped-snapshots'), + cli: [], + remove: false +}); + +test('snapshots remain if used in a discarded try()', testSnapshotPruningSafe, { + cwd: exec.cwd('try'), + cli: ['--update-snapshots'], + remove: false +}); + +test('snapshots remain if skipped in a discarded try()', testSnapshotPruningSafe, { + cwd: exec.cwd('skipped-snapshots-in-try'), + cli: ['--update-snapshots'], + remove: false, + checkRun: async (t, run) => { + await t.notThrowsAsync(run, 'Expected fixture not to throw'); + const result = await run; + t.snapshot(result.stats.unsavedSnapshots, 'files where snapshots could not be updated'); + } +}); diff --git a/test/snapshot-removal/try.js b/test/snapshot-removal/try.js deleted file mode 100644 index 78d7a879c..000000000 --- a/test/snapshot-removal/try.js +++ /dev/null @@ -1,10 +0,0 @@ -const test = require('@ava/test'); -const exec = require('../helpers/exec'); -const {testSnapshotPruningSafe} = require('./helpers/macros'); -const path = require('path'); - -test('snapshots remain if used in a discarded try()', testSnapshotPruningSafe, { - cwd: exec.cwd(path.parse(__filename).name), - cli: ['--update-snapshots'], - remove: false -}); From 2e23b6653643ef53c1804f021129711527bf3a51 Mon Sep 17 00:00:00 2001 From: ninevra Date: Fri, 18 Dec 2020 18:35:49 -0800 Subject: [PATCH 25/34] Refactor macro exports --- test/snapshot-removal/helpers/macros.js | 2 +- test/snapshot-removal/test.js | 28 +++++++++++++------------ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/test/snapshot-removal/helpers/macros.js b/test/snapshot-removal/helpers/macros.js index 9634d0429..35d4a42b5 100644 --- a/test/snapshot-removal/helpers/macros.js +++ b/test/snapshot-removal/helpers/macros.js @@ -22,7 +22,7 @@ function withTemporaryFixture(macro) { }; } -module.exports.testSnapshotPruningSafe = withTemporaryFixture(testSnapshotPruning); +module.exports.withTemporaryFixture = withTemporaryFixture; async function testSnapshotPruning(t, { cwd, diff --git a/test/snapshot-removal/test.js b/test/snapshot-removal/test.js index 15bcc9472..dc99251ab 100644 --- a/test/snapshot-removal/test.js +++ b/test/snapshot-removal/test.js @@ -1,15 +1,17 @@ const test = require('@ava/test'); const exec = require('../helpers/exec'); -const {testSnapshotPruningSafe} = require('./helpers/macros'); +const {testSnapshotPruning, withTemporaryFixture} = require('./helpers/macros'); const path = require('path'); -test('snapshots are removed when tests stop using them', testSnapshotPruningSafe, { +const macro = withTemporaryFixture(testSnapshotPruning); + +test('snapshots are removed when tests stop using them', macro, { cwd: exec.cwd('removal'), cli: ['--update-snapshots'], remove: true }); -test('snapshots are removed from a snapshot directory', testSnapshotPruningSafe, { +test('snapshots are removed from a snapshot directory', macro, { cwd: exec.cwd('snapshot-dir'), cli: ['--update-snapshots'], remove: true, @@ -17,7 +19,7 @@ test('snapshots are removed from a snapshot directory', testSnapshotPruningSafe, reportPath: path.join('test', 'snapshots', 'test.js.md') }); -test('snapshots are removed from a custom snapshotDir', testSnapshotPruningSafe, { +test('snapshots are removed from a custom snapshotDir', macro, { cwd: exec.cwd('fixed-snapshot-dir'), cli: ['--update-snapshots'], remove: true, @@ -38,13 +40,13 @@ test('removing non-existent snapshots doesn\'t throw', async t => { await t.notThrowsAsync(run); }); -test('snapshots remain if not updating', testSnapshotPruningSafe, { +test('snapshots remain if not updating', macro, { cwd: exec.cwd('removal'), cli: [], remove: false }); -test('snapshots remain if tests run with --match', testSnapshotPruningSafe, { +test('snapshots remain if tests run with --match', macro, { cwd: exec.cwd('removal'), cli: ['--update-snapshots', '--match=\'*snapshot*\''], remove: false, @@ -54,7 +56,7 @@ test('snapshots remain if tests run with --match', testSnapshotPruningSafe, { } }); -test('snapshots remain if tests selected by line numbers', testSnapshotPruningSafe, { +test('snapshots remain if tests selected by line numbers', macro, { cwd: exec.cwd('removal'), cli: ['test.js:3-12', '--update-snapshots'], remove: false, @@ -64,7 +66,7 @@ test('snapshots remain if tests selected by line numbers', testSnapshotPruningSa } }); -test('snapshots remain if using test.only', testSnapshotPruningSafe, { +test('snapshots remain if using test.only', macro, { cwd: exec.cwd('only-test'), cli: ['--update-snapshots'], remove: false, @@ -75,7 +77,7 @@ test('snapshots remain if using test.only', testSnapshotPruningSafe, { } }); -test('snapshots remain if tests are skipped', testSnapshotPruningSafe, { +test('snapshots remain if tests are skipped', macro, { cwd: exec.cwd('skipped-tests'), cli: ['--update-snapshots'], remove: false, @@ -86,7 +88,7 @@ test('snapshots remain if tests are skipped', testSnapshotPruningSafe, { } }); -test('snapshots remain if snapshot assertions are skipped (-u)', testSnapshotPruningSafe, { +test('snapshots remain if snapshot assertions are skipped (-u)', macro, { cwd: exec.cwd('skipped-snapshots'), cli: ['--update-snapshots'], remove: false, @@ -98,19 +100,19 @@ test('snapshots remain if snapshot assertions are skipped (-u)', testSnapshotPru } }); -test('snapshots remain if snapshot assertions are skipped (!-u)', testSnapshotPruningSafe, { +test('snapshots remain if snapshot assertions are skipped (!-u)', macro, { cwd: exec.cwd('skipped-snapshots'), cli: [], remove: false }); -test('snapshots remain if used in a discarded try()', testSnapshotPruningSafe, { +test('snapshots remain if used in a discarded try()', macro, { cwd: exec.cwd('try'), cli: ['--update-snapshots'], remove: false }); -test('snapshots remain if skipped in a discarded try()', testSnapshotPruningSafe, { +test('snapshots remain if skipped in a discarded try()', macro, { cwd: exec.cwd('skipped-snapshots-in-try'), cli: ['--update-snapshots'], remove: false, From b7b708d5adaf38638522b881d5b881fc77874539 Mon Sep 17 00:00:00 2001 From: ninevra Date: Sat, 19 Dec 2020 16:35:32 -0800 Subject: [PATCH 26/34] Restyle watcher-rerun-unlink test fixture --- .../snapshots/watcher-rerun-unlink/test.js | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/test-tap/fixture/snapshots/watcher-rerun-unlink/test.js b/test-tap/fixture/snapshots/watcher-rerun-unlink/test.js index 1fcc6332d..885f9e698 100644 --- a/test-tap/fixture/snapshots/watcher-rerun-unlink/test.js +++ b/test-tap/fixture/snapshots/watcher-rerun-unlink/test.js @@ -1,14 +1,10 @@ -/* eslint-disable capitalized-comments */ - const test = require('../../../..'); if (process.env.TEMPLATE) { test('test title', t => { t.snapshot({foo: 'bar'}); - t.snapshot({answer: 42}); - - t.assert(true); + t.pass(); }); test('another test', t => { @@ -16,16 +12,10 @@ if (process.env.TEMPLATE) { }); } else { test('test title', t => { - // t.snapshot({foo: 'bar'}); - - // t.snapshot({answer: 42}); - - t.assert(true); + t.pass(); }); test('another test', t => { - // t.snapshot(new Map()); - - t.assert(true); + t.pass(); }); } From d98f697e1b3927b5d201c3615bf6d0f4e578245a Mon Sep 17 00:00:00 2001 From: ninevra Date: Sat, 19 Dec 2020 16:36:01 -0800 Subject: [PATCH 27/34] Document return type of cleanSnapshots() --- lib/snapshot-manager.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/snapshot-manager.js b/lib/snapshot-manager.js index d1a63c5c1..cf9f1f5c6 100644 --- a/lib/snapshot-manager.js +++ b/lib/snapshot-manager.js @@ -464,7 +464,8 @@ function determineSnapshotPaths({file, fixedLocation, projectDir}) { }; } -// Remove snapshot and report if they exist +// Remove snapshot and report if they exist. Returns an array containing the +// paths of the touched files. async function cleanSnapshots({file, fixedLocation, projectDir}) { const {dir, snapFile, reportFile} = determineSnapshotPaths({file, fixedLocation, projectDir}); From 6d022c78c5afd4daaa6ef0f1995c515837ddc4ca Mon Sep 17 00:00:00 2001 From: ninevra Date: Sat, 19 Dec 2020 16:56:05 -0800 Subject: [PATCH 28/34] Test that snapshots aren't spuriously removed This is probably redundant. Also asserts that snapshot files aren't falsely reported as non-updateable. --- test/snapshot-removal/snapshots/test.js.md | 19 +++++++++++++++++++ test/snapshot-removal/snapshots/test.js.snap | Bin 393 -> 504 bytes test/snapshot-removal/test.js | 15 +++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/test/snapshot-removal/snapshots/test.js.md b/test/snapshot-removal/snapshots/test.js.md index 82ab6f81d..4f676d00b 100644 --- a/test/snapshot-removal/snapshots/test.js.md +++ b/test/snapshot-removal/snapshots/test.js.md @@ -55,3 +55,22 @@ Generated by [AVA](https://avajs.dev). file: 'test.js', }, ] + +## snapshots remain if they are still used + +> passed tests + + [ + { + file: 'test.js', + title: 'another snapshot', + }, + { + file: 'test.js', + title: 'some snapshots', + }, + ] + +> files where snapshots could not be updated + + [] diff --git a/test/snapshot-removal/snapshots/test.js.snap b/test/snapshot-removal/snapshots/test.js.snap index 123f0ba154934f609c66d21d9fbf83bec86998b6..178ddaf27c1e236bbe14f9358bf083ec5f409805 100644 GIT binary patch delta 380 zcmV-?0fYXD1NZ|nK~_N^Q*L2!b7*gLAa*he0sxe~M!mZ;_Tr$NJEYXN%KIOS2mk;8 z00003JHo)gz|N5}hkxM`ZI&A6CE_obc0Ro(!SR_1EUL}|!CpYj4DXQpK)E0m-b zmlW%v7~@aSnBvr&)Z`LOa}<&)6-qKvGm8{*GV@Xu@=9})Qh$pOhI247=>TaXAQon1 z6J%s{EGkN@WMqV~`1$!67`Yf285tP485wQhnv#KZCQK8Xe^M6Eaz-XNQ$Rp~fsq%i zjUT9nNraIZ=n+9imbA>AR2YXHxg3a36G3nvQ0Y&glK()g%!sCx8N(|~#CQcw4Qoke2||%TA~1htq!uX@L-QP! a%;hW2&rO9JiWHnULl6LlvWFoK1poknznNwL delta 268 zcmV+n0rUR&1BnANK~_N^Q*L2!b7*gLAa*he0syDq<-@}pXK+uJA-0CDT5%tX2mk;8 z00003Tg1S?z{Zg>hks&b26nJ(7+Dzv87+hJ5(|nm@=J;pk`wdt@=FwwQWZ)IQW8s2 zQxwWGQu7pY6HAgaGV{_EiVIScGt)AY6-rWzON#YSjPWOEOmS*XYH|stISNUY3MCn- znMDdYnR%%Sd8N5YsYM9GIT)FAfV2@13p26_GO{`r6(v?OGEu@<{QUe3j9d(ij0}w2 zjEpvLP02ty6Q+sHKPd}nIU^ICDIg%gz{m^M#t&4(B*MrH^oSrMOIl`5DvZMpa-d#T S@u*{lgku0(W#9pN0{{RL?q-|- diff --git a/test/snapshot-removal/test.js b/test/snapshot-removal/test.js index dc99251ab..71d21584d 100644 --- a/test/snapshot-removal/test.js +++ b/test/snapshot-removal/test.js @@ -46,6 +46,21 @@ test('snapshots remain if not updating', macro, { remove: false }); +test('snapshots remain if they are still used', macro, { + cwd: exec.cwd('removal'), + cli: ['--update-snapshots'], + remove: false, + env: { + TEMPLATE: 'true' + }, + async checkRun(t, run) { + await t.notThrowsAsync(run, 'Expected fixture not to throw'); + const result = await run; + t.snapshot(result.stats.passed, 'passed tests'); + t.snapshot(result.stats.unsavedSnapshots, 'files where snapshots could not be updated'); + } +}); + test('snapshots remain if tests run with --match', macro, { cwd: exec.cwd('removal'), cli: ['--update-snapshots', '--match=\'*snapshot*\''], From a5a81d7c5ecea2de31821562a3f47d6f48ed52e7 Mon Sep 17 00:00:00 2001 From: ninevra Date: Sat, 19 Dec 2020 16:56:33 -0800 Subject: [PATCH 29/34] Remove a redundant test --- test/snapshot-removal/snapshots/test.js.md | 10 ++++++++++ test/snapshot-removal/snapshots/test.js.snap | Bin 504 -> 530 bytes test/snapshot-removal/test.js | 8 +------- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/test/snapshot-removal/snapshots/test.js.md b/test/snapshot-removal/snapshots/test.js.md index 4f676d00b..3f7a452ff 100644 --- a/test/snapshot-removal/snapshots/test.js.md +++ b/test/snapshot-removal/snapshots/test.js.md @@ -74,3 +74,13 @@ Generated by [AVA](https://avajs.dev). > files where snapshots could not be updated [] + +## snapshots remain if snapshot assertions are skipped + +> files where snapshots could not be updated + + [ + { + file: 'test.js', + }, + ] diff --git a/test/snapshot-removal/snapshots/test.js.snap b/test/snapshot-removal/snapshots/test.js.snap index 178ddaf27c1e236bbe14f9358bf083ec5f409805..0c5c5442ffd8db51d5328e4a5167e55d951e1922 100644 GIT binary patch delta 381 zcmV-@0fPSc1Cj(XK~_N^Q*L2!b7*gLAa*he0ssf6;*FrGl>|dr03UbGJsKa22mk;8 z00003yT`!5z`>C+pnqJj=kwl%%F}nGKTLNI5NXP01dCn=VrB+*uum9S83Y+EgYyy# ziZk*{iWQO*^YZdb6p~UEN()jFOHxx5$}>{)6mk%(iMseQj;^&GLscbQj1H9 z^-zrQCumG@YEEi$38py;NtFsE8L62?3OSj1sS0_exk;%-2!F#l7@2f{v=I;sGqMRX zvN{$OB~~&r!dU$L{0xj-42+BnjNFWjHgHYJKspnqiOoML3urkb6Pzg^Ai%)L3)aRD zRKp~~$PDy|AR|j!W=<-M!wzzwURLp_V}_bzm<@om84x=Vau5^PLEaFJKvo%uW(1qD z5Xf8(#HR_k4??K)Cs4_MAXa8XQ_76t6((Z5f~JPGB(nseNFWiIzcNyb6pEpFj!Ne8 b73b%s!VN_VPMje)sG=VLsRzVWwFLkGGNqOZ delta 355 zcmV-p0i6Dl1o#6oK~_N^Q*L2!b7*gLAa*he0sxe~M!mZ;_Tr$NJEYXN%KIOS2mk;8 z00003JHo)gz|N5}pnuE^^Z^JkvN8xVS_bDO78GaXmlP``C+6klmnbBqDwGzaB$lM6 zD3oWU<|*VRmLz9n=A|nX7o;X6C?K)tNu zQO685$1ocJX)_>pAmkt>u!FoI8iA}b5X}fSV Date: Sat, 19 Dec 2020 17:01:35 -0800 Subject: [PATCH 30/34] Update snapshots --- test/snapshot-removal/snapshots/test.js.md | 14 ++------------ test/snapshot-removal/snapshots/test.js.snap | Bin 530 -> 513 bytes 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/test/snapshot-removal/snapshots/test.js.md b/test/snapshot-removal/snapshots/test.js.md index 3f7a452ff..89e1a2a84 100644 --- a/test/snapshot-removal/snapshots/test.js.md +++ b/test/snapshot-removal/snapshots/test.js.md @@ -14,7 +14,7 @@ Generated by [AVA](https://avajs.dev). }, ] -## snapshots remain if snapshot assertions are skipped (-u) +## snapshots remain if snapshot assertions are skipped > files where snapshots could not be updated @@ -46,16 +46,6 @@ Generated by [AVA](https://avajs.dev). 'Snapshots cannot be updated when selecting specific tests by their line number.' -## snapshots remain if using test.only - -> files where snapshots could not be updated - - [ - { - file: 'test.js', - }, - ] - ## snapshots remain if they are still used > passed tests @@ -75,7 +65,7 @@ Generated by [AVA](https://avajs.dev). [] -## snapshots remain if snapshot assertions are skipped +## snapshots remain if using test.only > files where snapshots could not be updated diff --git a/test/snapshot-removal/snapshots/test.js.snap b/test/snapshot-removal/snapshots/test.js.snap index 0c5c5442ffd8db51d5328e4a5167e55d951e1922..6dd5b8c82b8b5e996fcb0a4175486cd8131429dc 100644 GIT binary patch delta 479 zcmV<50U-X81c3xHK~_N^Q*L2!b7*gLAa*he0swO%V&ghY>YB`WpQ(4Un`Ix12mk;8 z00003JHo)gz|N5}E`N;@ZI&A6CE_obc0Ro(!SR_1Ec%%dg2k8^7{0guuWos~c<0_p z$qV|VZig{~MHd5c!Jf~18!AuVk^V5n-MH(z|6of=Ro(qkUz`ScKRGYon6>+ zjS(!m7l^%`IAb)6i#>kMQeRf-p0AgWAHbF*K$D*Rd zN=8N)i=UsLfq#*Ufsu)Uk(-gx8=?`&Dg)7sU^5m1nahFrG|UV(|D-ISdl;GEOaTD_ zu+pDECI5j~nGsDXGpbTvu!a0UHB2Im%)np}WMoOp%t?iD*g@XY%PK}w!&;JAf>0!o z2n?%?)FOpqX!uddT)yJ`+*G)s#fad<8G=Y5&ZGmRjU)(#HX}HUZQz=cfpjK;;N=Bt VBkZ?P#|#O_005)Mn|clf006#B)ARrU delta 496 zcmV|dr03UbGJsKa22mk;8 z00003yT`!5z`>C+E`Rsd|J5yz7w_CVDS1Jk)a@`vu&5zW>#5x9o`JFt&6leyl+=Cg zkYWUj?gL_PC(ann;$n}Vv(%SW`Zz3LgorXRFw8m7y)Wd?aNHDT(IZ!-iFH4ccec|cYhBMY072k^>XiRZxPHJ)qra1~pl?o*pshLFzIhlE>3VEfuNvTB$!#Nn4 zbbz!G5DPQ134b!OIu;crRx&ceSp59_42)b1jEoG7+>DGia81cTIuoXe%|9s%XgMPj zoGBn6z`)20*2WK1!z9AU4D^T~BTHIlPAZJU4sxJgR`IA~hMHrT4S=*65IYcZ5EIxz z-VlvIRvCz91e>uC$XpJ@rwO Date: Wed, 30 Dec 2020 23:45:55 -0800 Subject: [PATCH 31/34] Avoid creating skipSnapshotHook variable --- lib/test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/test.js b/lib/test.js index 604b80dc8..866590d17 100644 --- a/lib/test.js +++ b/lib/test.js @@ -248,10 +248,10 @@ class Test { return result; }; - const skipSnapshotHook = options.skipSnapshot || (() => {}); - this.skipSnapshot = () => { - skipSnapshotHook(); + if (typeof options.skipSnapshot === 'function') { + options.skipSnapshot(); + } if (options.updateSnapshots) { this.addFailedAssertion(new Error('Snapshot assertions cannot be skipped when updating snapshots')); From d14ef0a4569a642422e9e505883ac862bf2e44a0 Mon Sep 17 00:00:00 2001 From: ninevra Date: Wed, 30 Dec 2020 23:56:59 -0800 Subject: [PATCH 32/34] Use sync operations in cleanSnapshots() --- lib/runner.js | 4 ++-- lib/snapshot-manager.js | 10 +++++----- lib/worker/subprocess.js | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/runner.js b/lib/runner.js index 4118b6637..1f5577439 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -205,7 +205,7 @@ class Runner extends Emittery { this.skippedSnapshots = true; } - async saveSnapshotState() { + saveSnapshotState() { if ( this.updateSnapshots && ( @@ -222,7 +222,7 @@ class Runner extends Emittery { } if (this.updateSnapshots) { - return {touchedFiles: await snapshotManager.cleanSnapshots({ + return {touchedFiles: snapshotManager.cleanSnapshots({ file: this.file, fixedLocation: this.snapshotDir, projectDir: this.projectDir diff --git a/lib/snapshot-manager.js b/lib/snapshot-manager.js index cf9f1f5c6..b76e6571e 100644 --- a/lib/snapshot-manager.js +++ b/lib/snapshot-manager.js @@ -466,12 +466,12 @@ function determineSnapshotPaths({file, fixedLocation, projectDir}) { // Remove snapshot and report if they exist. Returns an array containing the // paths of the touched files. -async function cleanSnapshots({file, fixedLocation, projectDir}) { +function cleanSnapshots({file, fixedLocation, projectDir}) { const {dir, snapFile, reportFile} = determineSnapshotPaths({file, fixedLocation, projectDir}); - async function clean(file) { + function clean(file) { try { - await fs.promises.unlink(file); + fs.unlinkSync(file); return [file]; } catch (error) { if (error.code === 'ENOENT') { @@ -483,8 +483,8 @@ async function cleanSnapshots({file, fixedLocation, projectDir}) { } return [ - ...await clean(path.join(dir, snapFile)), - ...await clean(path.join(dir, reportFile)) + ...clean(path.join(dir, snapFile)), + ...clean(path.join(dir, reportFile)) ]; } diff --git a/lib/worker/subprocess.js b/lib/worker/subprocess.js index a40c281f2..abd9aabab 100644 --- a/lib/worker/subprocess.js +++ b/lib/worker/subprocess.js @@ -93,7 +93,7 @@ ipc.options.then(async options => { runner.on('finish', async () => { try { - const {cannotSave, touchedFiles} = await runner.saveSnapshotState(); + const {cannotSave, touchedFiles} = runner.saveSnapshotState(); if (cannotSave) { ipc.send({type: 'snapshot-error'}); } else if (touchedFiles) { From 76a93c54e3c1c087068acd5602816370e566bb7c Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Thu, 31 Dec 2020 15:17:41 +0100 Subject: [PATCH 33/34] Reuse file cleaning function --- lib/snapshot-manager.js | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/snapshot-manager.js b/lib/snapshot-manager.js index b76e6571e..5e19b1609 100644 --- a/lib/snapshot-manager.js +++ b/lib/snapshot-manager.js @@ -464,27 +464,27 @@ function determineSnapshotPaths({file, fixedLocation, projectDir}) { }; } +function cleanFile(file) { + try { + fs.unlinkSync(file); + return [file]; + } catch (error) { + if (error.code === 'ENOENT') { + return []; + } + + throw error; + } +} + // Remove snapshot and report if they exist. Returns an array containing the // paths of the touched files. function cleanSnapshots({file, fixedLocation, projectDir}) { const {dir, snapFile, reportFile} = determineSnapshotPaths({file, fixedLocation, projectDir}); - function clean(file) { - try { - fs.unlinkSync(file); - return [file]; - } catch (error) { - if (error.code === 'ENOENT') { - return []; - } - - throw error; - } - } - return [ - ...clean(path.join(dir, snapFile)), - ...clean(path.join(dir, reportFile)) + ...cleanFile(path.join(dir, snapFile)), + ...cleanFile(path.join(dir, reportFile)) ]; } From 23a43bbfb9d7bb27e1bead23b166d946ba10fe8d Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Thu, 31 Dec 2020 15:22:24 +0100 Subject: [PATCH 34/34] Clarify AVA import in fixtures --- test/snapshot-removal/fixtures/fixed-snapshot-dir/test.js | 2 +- test/snapshot-removal/fixtures/only-test/test.js | 2 +- test/snapshot-removal/fixtures/removal/test.js | 2 +- test/snapshot-removal/fixtures/skipped-snapshots-in-try/test.js | 2 +- test/snapshot-removal/fixtures/skipped-snapshots/test.js | 2 +- test/snapshot-removal/fixtures/skipped-tests/test.js | 2 +- test/snapshot-removal/fixtures/snapshot-dir/test/test.js | 2 +- test/snapshot-removal/fixtures/try/test.js | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/test/snapshot-removal/fixtures/fixed-snapshot-dir/test.js b/test/snapshot-removal/fixtures/fixed-snapshot-dir/test.js index 54da975d0..61e035eae 100644 --- a/test/snapshot-removal/fixtures/fixed-snapshot-dir/test.js +++ b/test/snapshot-removal/fixtures/fixed-snapshot-dir/test.js @@ -1,4 +1,4 @@ -const test = require(process.env.AVA_PATH || 'ava'); +const test = require(process.env.AVA_PATH); // This fixture is copied to a temporary directory, so require AVA through its configured path. if (process.env.TEMPLATE) { test('some snapshots', t => { diff --git a/test/snapshot-removal/fixtures/only-test/test.js b/test/snapshot-removal/fixtures/only-test/test.js index e31f803ba..0bad8f5f2 100644 --- a/test/snapshot-removal/fixtures/only-test/test.js +++ b/test/snapshot-removal/fixtures/only-test/test.js @@ -1,4 +1,4 @@ -const test = require(process.env.AVA_PATH || 'ava'); +const test = require(process.env.AVA_PATH); // This fixture is copied to a temporary directory, so require AVA through its configured path. if (process.env.TEMPLATE) { test('some snapshots', t => { diff --git a/test/snapshot-removal/fixtures/removal/test.js b/test/snapshot-removal/fixtures/removal/test.js index 54da975d0..61e035eae 100644 --- a/test/snapshot-removal/fixtures/removal/test.js +++ b/test/snapshot-removal/fixtures/removal/test.js @@ -1,4 +1,4 @@ -const test = require(process.env.AVA_PATH || 'ava'); +const test = require(process.env.AVA_PATH); // This fixture is copied to a temporary directory, so require AVA through its configured path. if (process.env.TEMPLATE) { test('some snapshots', t => { diff --git a/test/snapshot-removal/fixtures/skipped-snapshots-in-try/test.js b/test/snapshot-removal/fixtures/skipped-snapshots-in-try/test.js index 10391d317..cc43c7145 100644 --- a/test/snapshot-removal/fixtures/skipped-snapshots-in-try/test.js +++ b/test/snapshot-removal/fixtures/skipped-snapshots-in-try/test.js @@ -1,4 +1,4 @@ -const test = require(process.env.AVA_PATH || 'ava'); +const test = require(process.env.AVA_PATH); // This fixture is copied to a temporary directory, so require AVA through its configured path. if (process.env.TEMPLATE) { test('skipped snapshots in try', async t => { diff --git a/test/snapshot-removal/fixtures/skipped-snapshots/test.js b/test/snapshot-removal/fixtures/skipped-snapshots/test.js index 1f7925638..a01ab31fe 100644 --- a/test/snapshot-removal/fixtures/skipped-snapshots/test.js +++ b/test/snapshot-removal/fixtures/skipped-snapshots/test.js @@ -1,4 +1,4 @@ -const test = require(process.env.AVA_PATH || 'ava'); +const test = require(process.env.AVA_PATH); // This fixture is copied to a temporary directory, so require AVA through its configured path. if (process.env.TEMPLATE) { test('some snapshots', t => { diff --git a/test/snapshot-removal/fixtures/skipped-tests/test.js b/test/snapshot-removal/fixtures/skipped-tests/test.js index cc3251e42..a4344b841 100644 --- a/test/snapshot-removal/fixtures/skipped-tests/test.js +++ b/test/snapshot-removal/fixtures/skipped-tests/test.js @@ -1,4 +1,4 @@ -const test = require(process.env.AVA_PATH || 'ava'); +const test = require(process.env.AVA_PATH); // This fixture is copied to a temporary directory, so require AVA through its configured path. if (process.env.TEMPLATE) { test('some snapshots', t => { diff --git a/test/snapshot-removal/fixtures/snapshot-dir/test/test.js b/test/snapshot-removal/fixtures/snapshot-dir/test/test.js index 54da975d0..61e035eae 100644 --- a/test/snapshot-removal/fixtures/snapshot-dir/test/test.js +++ b/test/snapshot-removal/fixtures/snapshot-dir/test/test.js @@ -1,4 +1,4 @@ -const test = require(process.env.AVA_PATH || 'ava'); +const test = require(process.env.AVA_PATH); // This fixture is copied to a temporary directory, so require AVA through its configured path. if (process.env.TEMPLATE) { test('some snapshots', t => { diff --git a/test/snapshot-removal/fixtures/try/test.js b/test/snapshot-removal/fixtures/try/test.js index 07efe59d7..fb4d44277 100644 --- a/test/snapshot-removal/fixtures/try/test.js +++ b/test/snapshot-removal/fixtures/try/test.js @@ -1,4 +1,4 @@ -const test = require(process.env.AVA_PATH || 'ava'); +const test = require(process.env.AVA_PATH); // This fixture is copied to a temporary directory, so require AVA through its configured path. if (process.env.TEMPLATE) { test('snapshots in try', async t => {