Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add filename for obsolete snapshot file #8665

Merged
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -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"`;
Original file line number Diff line number Diff line change
@@ -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"`;
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
4 changes: 4 additions & 0 deletions packages/jest-core/src/TestScheduler.ts
Original file line number Diff line number Diff line change
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 = [
jeysal marked this conversation as resolved.
Show resolved Hide resolved
...(aggregatedResults.snapshot.filesRemovedList || []),
].concat(status.filesRemovedList);
});
const updateAll = this._globalConfig.updateSnapshot === 'all';
aggregatedResults.snapshot.didUpdate = updateAll;
Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
SimenB marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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