Skip to content

Commit f440ded

Browse files
ndelangenjeysal
authored andcommitted
add filename for obsolete snapshot file (#8665)
* 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
1 parent 5177236 commit f440ded

File tree

14 files changed

+94
-7
lines changed

14 files changed

+94
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- `[expect]` Improve report when mock-spy matcher fails, part 4 ([#8710](https://github.com/facebook/jest/pull/8710))
1111
- `[expect]` Throw matcher error when received cannot be jasmine spy ([#8747](https://github.com/facebook/jest/pull/8747))
1212
- `[jest-snapshot]` Highlight substring differences when matcher fails, part 3 ([#8569](https://github.com/facebook/jest/pull/8569))
13+
- `[jest-core]` Improve report when snapshots are obsolete ([#8448](https://github.com/facebook/jest/pull/8665))
1314
- `[jest-cli]` Improve chai support (with detailed output, to match jest exceptions) ([#8454](https://github.com/facebook/jest/pull/8454))
1415
- `[*]` Manage the global timeout with `--testTimeout` command line argument. ([#8456](https://github.com/facebook/jest/pull/8456))
1516
- `[pretty-format]` Render custom displayName of memoized components
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import runJest from '../runJest';
9+
10+
describe('Snapshot serializers', () => {
11+
it('renders snapshot', () => {
12+
const result = runJest('snapshot-unknown', ['-w=1']);
13+
const stderr = result.stderr;
14+
15+
expect(stderr).toMatch('2 snapshot files obsolete');
16+
expect(stderr).toMatch('__tests__/__snapshots__/fails.test.js.snap');
17+
expect(stderr).toMatch('__tests__/__snapshots__/fails2.test.js.snap');
18+
expect(result.status).toBe(1);
19+
});
20+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`snapshot this one makes not toMatchSnapshot assertion, but has a .snap file 1`] = `"normal"`;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`snapshot this one makes not toMatchSnapshot assertion, but has a .snap file 1`] = `"normal"`;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`snapshot some snapshots exists and are fine 1`] = `"normal"`;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
*/
8+
'use strict';
9+
10+
describe('snapshot', () => {
11+
it('some snapshots exists and are fine', () => {
12+
expect('normal').toMatchSnapshot();
13+
});
14+
});

e2e/snapshot-unknown/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"jest": {
3+
"testEnvironment": "node"
4+
}
5+
}

packages/jest-core/src/TestScheduler.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,13 @@ export default class TestScheduler {
145145
context.hasteFS,
146146
this._globalConfig.updateSnapshot,
147147
snapshot.buildSnapshotResolver(context.config),
148+
context.config.testPathIgnorePatterns,
148149
);
149150

150151
aggregatedResults.snapshot.filesRemoved += status.filesRemoved;
152+
aggregatedResults.snapshot.filesRemovedList = (
153+
aggregatedResults.snapshot.filesRemovedList || []
154+
).concat(status.filesRemovedList);
151155
});
152156
const updateAll = this._globalConfig.updateSnapshot === 'all';
153157
aggregatedResults.snapshot.didUpdate = updateAll;

packages/jest-reporters/src/__tests__/get_snapshot_summary.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ test('creates a snapshot summary', () => {
2121
didUpdate: false,
2222
filesAdded: 1,
2323
filesRemoved: 1,
24+
filesRemovedList: [],
2425
filesUnmatched: 1,
2526
filesUpdated: 1,
2627
matched: 2,
@@ -49,6 +50,7 @@ test('creates a snapshot summary after an update', () => {
4950
didUpdate: true,
5051
filesAdded: 1,
5152
filesRemoved: 1,
53+
filesRemovedList: [],
5254
filesUnmatched: 1,
5355
filesUpdated: 1,
5456
unchecked: 1,
@@ -75,6 +77,7 @@ it('creates a snapshot summary with multiple snapshot being written/updated', ()
7577
didUpdate: false,
7678
filesAdded: 2,
7779
filesRemoved: 2,
80+
filesRemovedList: [],
7881
filesUnmatched: 2,
7982
filesUpdated: 2,
8083
unchecked: 2,
@@ -105,6 +108,7 @@ it('returns nothing if there are no updates', () => {
105108
didUpdate: false,
106109
filesAdded: 0,
107110
filesRemoved: 0,
111+
filesRemovedList: [],
108112
filesUnmatched: 0,
109113
filesUpdated: 0,
110114
unchecked: 0,

packages/jest-reporters/src/__tests__/summary_reporter.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ test('snapshots needs update with yarn test', () => {
7070
numTotalTestSuites: 1,
7171
numTotalTests: 1,
7272
snapshot: {
73+
filesRemovedList: [],
7374
filesUnmatched: 1,
7475
total: 2,
7576
uncheckedKeysByFile: [],
@@ -98,6 +99,7 @@ test('snapshots all have results (no update)', () => {
9899
didUpdate: false,
99100
filesAdded: 1,
100101
filesRemoved: 1,
102+
filesRemovedList: [],
101103
filesUnmatched: 1,
102104
filesUpdated: 1,
103105
matched: 2,
@@ -134,6 +136,7 @@ test('snapshots all have results (after update)', () => {
134136
didUpdate: true,
135137
filesAdded: 1,
136138
filesRemoved: 1,
139+
filesRemovedList: [],
137140
filesUnmatched: 1,
138141
filesUpdated: 1,
139142
matched: 2,

packages/jest-reporters/src/get_snapshot_summary.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ export default (
8484
);
8585
}
8686
}
87+
if (snapshots.filesRemovedList && snapshots.filesRemovedList.length) {
88+
const [head, ...tail] = snapshots.filesRemovedList;
89+
summary.push(` ${DOWN_ARROW} ${DOT}${formatTestPath(globalConfig, head)}`);
90+
91+
tail.forEach(key => {
92+
summary.push(` ${DOT}${formatTestPath(globalConfig, key)}`);
93+
});
94+
}
8795

8896
if (snapshots.unchecked) {
8997
if (snapshots.didUpdate) {

packages/jest-snapshot/src/index.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,22 +122,39 @@ const cleanup = (
122122
hasteFS: HasteFS,
123123
update: Config.SnapshotUpdateState,
124124
snapshotResolver: JestSnapshotResolver,
125-
) => {
125+
testPathIgnorePatterns?: Config.ProjectConfig['testPathIgnorePatterns'],
126+
): {
127+
filesRemoved: number;
128+
filesRemovedList: Array<string>;
129+
} => {
126130
const pattern = '\\.' + EXTENSION + '$';
127131
const files = hasteFS.matchFiles(pattern);
128-
const filesRemoved = files.reduce((acc, snapshotFile) => {
129-
if (!fileExists(snapshotResolver.resolveTestPath(snapshotFile), hasteFS)) {
132+
let testIgnorePatternsRegex: RegExp | null = null;
133+
if (testPathIgnorePatterns && testPathIgnorePatterns.length > 0) {
134+
testIgnorePatternsRegex = new RegExp(testPathIgnorePatterns.join('|'));
135+
}
136+
137+
const list = files.filter(snapshotFile => {
138+
const testPath = snapshotResolver.resolveTestPath(snapshotFile);
139+
140+
// ignore snapshots of ignored tests
141+
if (testIgnorePatternsRegex && testIgnorePatternsRegex.test(testPath)) {
142+
return false;
143+
}
144+
145+
if (!fileExists(testPath, hasteFS)) {
130146
if (update === 'all') {
131147
fs.unlinkSync(snapshotFile);
132148
}
133-
return acc + 1;
149+
return true;
134150
}
135151

136-
return acc;
137-
}, 0);
152+
return false;
153+
});
138154

139155
return {
140-
filesRemoved,
156+
filesRemoved: list.length,
157+
filesRemovedList: list,
141158
};
142159
};
143160

packages/jest-test-result/src/helpers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const makeEmptyAggregatedTestResult = (): AggregatedResult => ({
2727
filesAdded: 0,
2828
// combines individual test results + removed files after the full run
2929
filesRemoved: 0,
30+
filesRemovedList: [],
3031
filesUnmatched: 0,
3132
filesUpdated: 0,
3233
matched: 0,

packages/jest-test-result/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ export type SnapshotSummary = {
181181
failure: boolean;
182182
filesAdded: number;
183183
filesRemoved: number;
184+
filesRemovedList: Array<string>;
184185
filesUnmatched: number;
185186
filesUpdated: number;
186187
matched: number;

0 commit comments

Comments
 (0)