Skip to content

Commit 39f83a9

Browse files
excitement-engineercpojer
authored andcommitted
Separated the snapshot summary creation from the printing to improve testability. (#4373)
1 parent cb8e760 commit 39f83a9

4 files changed

Lines changed: 206 additions & 65 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`creates a snapshot summary 1`] = `
4+
Array [
5+
"<bold>Snapshot Summary",
6+
"<bold><green> › 1 snapshot</> written in 1 test suite.",
7+
"<bold><red> › 1 snapshot test</> failed in 1 test suite. <dim>Inspect your code changes or press --u to update them.",
8+
"<bold><green> › 1 snapshot</> updated in 1 test suite.",
9+
"<bold><red> › 1 obsolete snapshot file</> found, press --u to remove it.",
10+
"<bold><red> › 1 obsolete snapshot</> found, press --u to remove it.",
11+
]
12+
`;
13+
14+
exports[`creates a snapshot summary after an update 1`] = `
15+
Array [
16+
"<bold>Snapshot Summary",
17+
"<bold><green> › 1 snapshot</> written in 1 test suite.",
18+
"<bold><red> › 1 snapshot test</> failed in 1 test suite. <dim>Inspect your code changes or press --u to update them.",
19+
"<bold><green> › 1 snapshot</> updated in 1 test suite.",
20+
"<bold><red> › 1 obsolete snapshot file</> removed.",
21+
"<bold><red> › 1 obsolete snapshot</> removed.",
22+
]
23+
`;
24+
25+
exports[`creates a snapshot summary with multiple snapshot being written/updated 1`] = `
26+
Array [
27+
"<bold>Snapshot Summary",
28+
"<bold><green> › 2 snapshots</> written in 2 test suites.",
29+
"<bold><red> › 2 snapshot tests</> failed in 2 test suites. <dim>Inspect your code changes or press --u to update them.",
30+
"<bold><green> › 2 snapshots</> updated in 2 test suites.",
31+
"<bold><red> › 2 obsolete snapshot files</> found, press --u to remove them..",
32+
"<bold><red> › 2 obsolete snapshots</> found, press --u to remove them.",
33+
]
34+
`;
35+
36+
exports[`returns nothing if there are no updates 1`] = `
37+
Array [
38+
"<bold>Snapshot Summary",
39+
]
40+
`;
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the BSD-style license found in the
5+
* LICENSE file in the root directory of this source tree. An additional grant
6+
* of patent rights can be found in the PATENTS file in the same directory.
7+
*/
8+
9+
'use strict';
10+
11+
import getSnapshotSummary from '../get_snapshot_summary';
12+
13+
const UPDATE_COMMAND = 'press --u';
14+
15+
test('creates a snapshot summary', () => {
16+
const snapshots = {
17+
added: 1,
18+
didUpdate: false,
19+
filesAdded: 1,
20+
filesRemoved: 1,
21+
filesUnmatched: 1,
22+
filesUpdated: 1,
23+
matched: 2,
24+
total: 2,
25+
unchecked: 1,
26+
unmatched: 1,
27+
updated: 1,
28+
};
29+
30+
expect(getSnapshotSummary(snapshots, UPDATE_COMMAND)).toMatchSnapshot();
31+
});
32+
33+
test('creates a snapshot summary after an update', () => {
34+
const snapshots = {
35+
added: 1,
36+
didUpdate: true,
37+
filesAdded: 1,
38+
filesRemoved: 1,
39+
filesUnmatched: 1,
40+
filesUpdated: 1,
41+
unchecked: 1,
42+
unmatched: 1,
43+
updated: 1,
44+
};
45+
46+
expect(getSnapshotSummary(snapshots, UPDATE_COMMAND)).toMatchSnapshot();
47+
});
48+
49+
it('creates a snapshot summary with multiple snapshot being written/updated', () => {
50+
const snapshots = {
51+
added: 2,
52+
didUpdate: false,
53+
filesAdded: 2,
54+
filesRemoved: 2,
55+
filesUnmatched: 2,
56+
filesUpdated: 2,
57+
unchecked: 2,
58+
unmatched: 2,
59+
updated: 2,
60+
};
61+
62+
expect(getSnapshotSummary(snapshots, UPDATE_COMMAND)).toMatchSnapshot();
63+
});
64+
65+
it('returns nothing if there are no updates', () => {
66+
const snapshots = {
67+
added: 0,
68+
didUpdate: false,
69+
filesAdded: 0,
70+
filesRemoved: 0,
71+
filesUnmatched: 0,
72+
filesUpdated: 0,
73+
unchecked: 0,
74+
unmatched: 0,
75+
updated: 0,
76+
};
77+
expect(getSnapshotSummary(snapshots, UPDATE_COMMAND)).toMatchSnapshot();
78+
});
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the BSD-style license found in the
5+
* LICENSE file in the root directory of this source tree. An additional grant
6+
* of patent rights can be found in the PATENTS file in the same directory.
7+
*
8+
* @flow
9+
*/
10+
11+
import type {SnapshotSummary} from 'types/TestResult';
12+
13+
import chalk from 'chalk';
14+
import {pluralize} from './utils';
15+
16+
const ARROW = ' \u203A ';
17+
const FAIL_COLOR = chalk.bold.red;
18+
const SNAPSHOT_ADDED = chalk.bold.green;
19+
const SNAPSHOT_NOTE = chalk.dim;
20+
const SNAPSHOT_REMOVED = chalk.bold.red;
21+
const SNAPSHOT_SUMMARY = chalk.bold;
22+
const SNAPSHOT_UPDATED = chalk.bold.green;
23+
24+
module.exports = (
25+
snapshots: SnapshotSummary,
26+
updateCommand: string,
27+
): Array<string> => {
28+
const summary = [];
29+
summary.push(SNAPSHOT_SUMMARY('Snapshot Summary'));
30+
if (snapshots.added) {
31+
summary.push(
32+
SNAPSHOT_ADDED(ARROW + pluralize('snapshot', snapshots.added)) +
33+
` written in ${pluralize('test suite', snapshots.filesAdded)}.`,
34+
);
35+
}
36+
37+
if (snapshots.unmatched) {
38+
summary.push(
39+
FAIL_COLOR(ARROW + pluralize('snapshot test', snapshots.unmatched)) +
40+
` failed in ` +
41+
`${pluralize('test suite', snapshots.filesUnmatched)}. ` +
42+
SNAPSHOT_NOTE(
43+
'Inspect your code changes or ' + updateCommand + ' to update them.',
44+
),
45+
);
46+
}
47+
48+
if (snapshots.updated) {
49+
summary.push(
50+
SNAPSHOT_UPDATED(ARROW + pluralize('snapshot', snapshots.updated)) +
51+
` updated in ${pluralize('test suite', snapshots.filesUpdated)}.`,
52+
);
53+
}
54+
55+
if (snapshots.filesRemoved) {
56+
summary.push(
57+
SNAPSHOT_REMOVED(
58+
ARROW + pluralize('obsolete snapshot file', snapshots.filesRemoved),
59+
) +
60+
(snapshots.didUpdate
61+
? ' removed.'
62+
: ' found, ' +
63+
updateCommand +
64+
' to remove ' +
65+
(snapshots.filesRemoved === 1 ? 'it' : 'them.') +
66+
'.'),
67+
);
68+
}
69+
70+
if (snapshots.unchecked) {
71+
summary.push(
72+
FAIL_COLOR(ARROW + pluralize('obsolete snapshot', snapshots.unchecked)) +
73+
(snapshots.didUpdate
74+
? ' removed.'
75+
: ' found, ' +
76+
updateCommand +
77+
' to remove ' +
78+
(snapshots.filesRemoved === 1 ? 'it' : 'them') +
79+
'.'),
80+
);
81+
}
82+
83+
return summary;
84+
};

packages/jest-cli/src/reporters/summary_reporter.js

Lines changed: 4 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,11 @@ import type {ReporterOnStartOptions} from 'types/Reporters';
1515

1616
import chalk from 'chalk';
1717
import BaseReporter from './base_reporter';
18-
import {getSummary, pluralize} from './utils';
18+
import {getSummary} from './utils';
1919
import getResultHeader from './get_result_header';
20+
import getSnapshotSummary from './get_snapshot_summary';
2021
import testPathPatternToRegExp from '../test_path_pattern_to_regexp';
2122

22-
const ARROW = ' \u203A ';
23-
const FAIL_COLOR = chalk.bold.red;
24-
const SNAPSHOT_ADDED = chalk.bold.green;
25-
const SNAPSHOT_NOTE = chalk.dim;
26-
const SNAPSHOT_REMOVED = chalk.bold.red;
27-
const SNAPSHOT_SUMMARY = chalk.bold;
28-
const SNAPSHOT_UPDATED = chalk.bold.green;
2923
const TEST_SUMMARY_THRESHOLD = 20;
3024

3125
const NPM_EVENTS = new Set([
@@ -149,63 +143,8 @@ class SummaryReporter extends BaseReporter {
149143
updateCommand = 're-run with `-u`';
150144
}
151145

152-
this.log(SNAPSHOT_SUMMARY('Snapshot Summary'));
153-
if (snapshots.added) {
154-
this.log(
155-
SNAPSHOT_ADDED(ARROW + pluralize('snapshot', snapshots.added)) +
156-
` written in ${pluralize('test suite', snapshots.filesAdded)}.`,
157-
);
158-
}
159-
160-
if (snapshots.unmatched) {
161-
this.log(
162-
FAIL_COLOR(ARROW + pluralize('snapshot test', snapshots.unmatched)) +
163-
` failed in ` +
164-
`${pluralize('test suite', snapshots.filesUnmatched)}. ` +
165-
SNAPSHOT_NOTE(
166-
'Inspect your code changes or ' +
167-
updateCommand +
168-
' to update them.',
169-
),
170-
);
171-
}
172-
173-
if (snapshots.updated) {
174-
this.log(
175-
SNAPSHOT_UPDATED(ARROW + pluralize('snapshot', snapshots.updated)) +
176-
` updated in ${pluralize('test suite', snapshots.filesUpdated)}.`,
177-
);
178-
}
179-
180-
if (snapshots.filesRemoved) {
181-
this.log(
182-
SNAPSHOT_REMOVED(
183-
ARROW + pluralize('obsolete snapshot file', snapshots.filesRemoved),
184-
) +
185-
(snapshots.didUpdate
186-
? ' removed.'
187-
: ' found, ' +
188-
updateCommand +
189-
' to remove ' +
190-
(snapshots.filesRemoved === 1 ? 'it' : 'them.') +
191-
'.'),
192-
);
193-
}
194-
195-
if (snapshots.unchecked) {
196-
this.log(
197-
FAIL_COLOR(
198-
ARROW + pluralize('obsolete snapshot', snapshots.unchecked),
199-
) +
200-
(snapshots.didUpdate
201-
? ' removed.'
202-
: ' found, ' +
203-
updateCommand +
204-
' to remove ' +
205-
(snapshots.filesRemoved === 1 ? 'it' : 'them') +
206-
'.'),
207-
);
208-
}
146+
const snapshotSummary = getSnapshotSummary(snapshots, updateCommand);
147+
snapshotSummary.forEach(this.log);
209148

210149
this.log(''); // print empty line
211150
}

0 commit comments

Comments
 (0)