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
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

### Features

- `[jest-cli]` Improve report when snapshots are obsolete ([#8448](https://github.com/facebook/jest/pull/8665))
- `[expect]` Highlight substring differences when matcher fails, part 1 ([#8448](https://github.com/facebook/jest/pull/8448))
- `[expect]` Highlight substring differences when matcher fails, part 2 ([#8528](https://github.com/facebook/jest/pull/8528))
- `[expect]` Improve report when mock-spy matcher fails, part 1 ([#8640](https://github.com/facebook/jest/pull/8640))
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"
}
}
2 changes: 1 addition & 1 deletion jest.config.ci.js
Expand Up @@ -7,6 +7,6 @@ module.exports = Object.assign({}, require('./jest.config'), {
coverageReporters: ['json'],
reporters: [
['jest-junit', {output: 'reports/junit/js-test-results.xml'}],
['jest-silent-reporter', {useDots: true}],
'default',
],
});
3 changes: 3 additions & 0 deletions packages/jest-core/src/TestScheduler.ts
Expand Up @@ -148,6 +148,9 @@ export default class TestScheduler {
);

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
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
14 changes: 9 additions & 5 deletions packages/jest-snapshot/src/index.ts
Expand Up @@ -122,22 +122,26 @@ const cleanup = (
hasteFS: HasteFS,
update: Config.SnapshotUpdateState,
snapshotResolver: JestSnapshotResolver,
) => {
): {
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) => {
const list = files.reduce<Array<string>>((acc, snapshotFile) => {
if (!fileExists(snapshotResolver.resolveTestPath(snapshotFile), hasteFS)) {
if (update === 'all') {
fs.unlinkSync(snapshotFile);
}
return acc + 1;
return [...acc, snapshotFile];
SimenB marked this conversation as resolved.
Show resolved Hide resolved
}

return acc;
}, 0);
}, []);

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