Skip to content

Commit

Permalink
add filename for obsolete snapshot file (#8665)
Browse files Browse the repository at this point in the history
* add a reproduction to my issue

* ADD filesRemovedList property to snapshotSummary & display this in the CLI report

* ADD tests

* ADD to changelog

* FIX unit tests & add a null-check before getting the array length of new property filesRemovedList

* chore: run with default reporter to debug ci

* simplify cleanup function of jest-snapshot

* chore: use filter over reduce

* chore: only report obsolete snapshots of non-ignored tests

* Revert "chore: run with default reporter to debug ci"

This reverts commit d2ff007.

* Update CHANGELOG.md

* get rid of unnecessary array spread
  • Loading branch information
ndelangen authored and jeysal committed Jul 26, 2019
1 parent 5177236 commit f440ded
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@
- `[expect]` Improve report when mock-spy matcher fails, part 4 ([#8710](https://github.com/facebook/jest/pull/8710))
- `[expect]` Throw matcher error when received cannot be jasmine spy ([#8747](https://github.com/facebook/jest/pull/8747))
- `[jest-snapshot]` Highlight substring differences when matcher fails, part 3 ([#8569](https://github.com/facebook/jest/pull/8569))
- `[jest-core]` Improve report when snapshots are obsolete ([#8448](https://github.com/facebook/jest/pull/8665))
- `[jest-cli]` Improve chai support (with detailed output, to match jest exceptions) ([#8454](https://github.com/facebook/jest/pull/8454))
- `[*]` Manage the global timeout with `--testTimeout` command line argument. ([#8456](https://github.com/facebook/jest/pull/8456))
- `[pretty-format]` Render custom displayName of memoized components
Expand Down
20 changes: 20 additions & 0 deletions e2e/__tests__/snapshot-unknown.test.ts
@@ -0,0 +1,20 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import runJest from '../runJest';

describe('Snapshot serializers', () => {
it('renders snapshot', () => {
const result = runJest('snapshot-unknown', ['-w=1']);
const stderr = result.stderr;

expect(stderr).toMatch('2 snapshot files obsolete');
expect(stderr).toMatch('__tests__/__snapshots__/fails.test.js.snap');
expect(stderr).toMatch('__tests__/__snapshots__/fails2.test.js.snap');
expect(result.status).toBe(1);
});
});
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`snapshot this one makes not toMatchSnapshot assertion, but has a .snap file 1`] = `"normal"`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`snapshot this one makes not toMatchSnapshot assertion, but has a .snap file 1`] = `"normal"`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`snapshot some snapshots exists and are fine 1`] = `"normal"`;
14 changes: 14 additions & 0 deletions e2e/snapshot-unknown/__tests__/works.test.js
@@ -0,0 +1,14 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
'use strict';

describe('snapshot', () => {
it('some snapshots exists and are fine', () => {
expect('normal').toMatchSnapshot();
});
});
5 changes: 5 additions & 0 deletions e2e/snapshot-unknown/package.json
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
4 changes: 4 additions & 0 deletions packages/jest-core/src/TestScheduler.ts
Expand Up @@ -145,9 +145,13 @@ export default class TestScheduler {
context.hasteFS,
this._globalConfig.updateSnapshot,
snapshot.buildSnapshotResolver(context.config),
context.config.testPathIgnorePatterns,
);

aggregatedResults.snapshot.filesRemoved += status.filesRemoved;
aggregatedResults.snapshot.filesRemovedList = (
aggregatedResults.snapshot.filesRemovedList || []
).concat(status.filesRemovedList);
});
const updateAll = this._globalConfig.updateSnapshot === 'all';
aggregatedResults.snapshot.didUpdate = updateAll;
Expand Down
Expand Up @@ -21,6 +21,7 @@ test('creates a snapshot summary', () => {
didUpdate: false,
filesAdded: 1,
filesRemoved: 1,
filesRemovedList: [],
filesUnmatched: 1,
filesUpdated: 1,
matched: 2,
Expand Down Expand Up @@ -49,6 +50,7 @@ test('creates a snapshot summary after an update', () => {
didUpdate: true,
filesAdded: 1,
filesRemoved: 1,
filesRemovedList: [],
filesUnmatched: 1,
filesUpdated: 1,
unchecked: 1,
Expand All @@ -75,6 +77,7 @@ it('creates a snapshot summary with multiple snapshot being written/updated', ()
didUpdate: false,
filesAdded: 2,
filesRemoved: 2,
filesRemovedList: [],
filesUnmatched: 2,
filesUpdated: 2,
unchecked: 2,
Expand Down Expand Up @@ -105,6 +108,7 @@ it('returns nothing if there are no updates', () => {
didUpdate: false,
filesAdded: 0,
filesRemoved: 0,
filesRemovedList: [],
filesUnmatched: 0,
filesUpdated: 0,
unchecked: 0,
Expand Down
Expand Up @@ -70,6 +70,7 @@ test('snapshots needs update with yarn test', () => {
numTotalTestSuites: 1,
numTotalTests: 1,
snapshot: {
filesRemovedList: [],
filesUnmatched: 1,
total: 2,
uncheckedKeysByFile: [],
Expand Down Expand Up @@ -98,6 +99,7 @@ test('snapshots all have results (no update)', () => {
didUpdate: false,
filesAdded: 1,
filesRemoved: 1,
filesRemovedList: [],
filesUnmatched: 1,
filesUpdated: 1,
matched: 2,
Expand Down Expand Up @@ -134,6 +136,7 @@ test('snapshots all have results (after update)', () => {
didUpdate: true,
filesAdded: 1,
filesRemoved: 1,
filesRemovedList: [],
filesUnmatched: 1,
filesUpdated: 1,
matched: 2,
Expand Down
8 changes: 8 additions & 0 deletions packages/jest-reporters/src/get_snapshot_summary.ts
Expand Up @@ -84,6 +84,14 @@ export default (
);
}
}
if (snapshots.filesRemovedList && snapshots.filesRemovedList.length) {
const [head, ...tail] = snapshots.filesRemovedList;
summary.push(` ${DOWN_ARROW} ${DOT}${formatTestPath(globalConfig, head)}`);

tail.forEach(key => {
summary.push(` ${DOT}${formatTestPath(globalConfig, key)}`);
});
}

if (snapshots.unchecked) {
if (snapshots.didUpdate) {
Expand Down
31 changes: 24 additions & 7 deletions packages/jest-snapshot/src/index.ts
Expand Up @@ -122,22 +122,39 @@ const cleanup = (
hasteFS: HasteFS,
update: Config.SnapshotUpdateState,
snapshotResolver: JestSnapshotResolver,
) => {
testPathIgnorePatterns?: Config.ProjectConfig['testPathIgnorePatterns'],
): {
filesRemoved: number;
filesRemovedList: Array<string>;
} => {
const pattern = '\\.' + EXTENSION + '$';
const files = hasteFS.matchFiles(pattern);
const filesRemoved = files.reduce((acc, snapshotFile) => {
if (!fileExists(snapshotResolver.resolveTestPath(snapshotFile), hasteFS)) {
let testIgnorePatternsRegex: RegExp | null = null;
if (testPathIgnorePatterns && testPathIgnorePatterns.length > 0) {
testIgnorePatternsRegex = new RegExp(testPathIgnorePatterns.join('|'));
}

const list = files.filter(snapshotFile => {
const testPath = snapshotResolver.resolveTestPath(snapshotFile);

// ignore snapshots of ignored tests
if (testIgnorePatternsRegex && testIgnorePatternsRegex.test(testPath)) {
return false;
}

if (!fileExists(testPath, hasteFS)) {
if (update === 'all') {
fs.unlinkSync(snapshotFile);
}
return acc + 1;
return true;
}

return acc;
}, 0);
return false;
});

return {
filesRemoved,
filesRemoved: list.length,
filesRemovedList: list,
};
};

Expand Down
1 change: 1 addition & 0 deletions packages/jest-test-result/src/helpers.ts
Expand Up @@ -27,6 +27,7 @@ export const makeEmptyAggregatedTestResult = (): AggregatedResult => ({
filesAdded: 0,
// combines individual test results + removed files after the full run
filesRemoved: 0,
filesRemovedList: [],
filesUnmatched: 0,
filesUpdated: 0,
matched: 0,
Expand Down
1 change: 1 addition & 0 deletions packages/jest-test-result/src/types.ts
Expand Up @@ -181,6 +181,7 @@ export type SnapshotSummary = {
failure: boolean;
filesAdded: number;
filesRemoved: number;
filesRemovedList: Array<string>;
filesUnmatched: number;
filesUpdated: number;
matched: number;
Expand Down

0 comments on commit f440ded

Please sign in to comment.