-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separated the snapshot summary creation from the printing to improve …
…testability. (#4373)
- Loading branch information
1 parent
cb8e760
commit 39f83a9
Showing
4 changed files
with
206 additions
and
65 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
packages/jest-cli/src/reporters/__tests__/__snapshots__/get_snapshot_summary.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`creates a snapshot summary 1`] = ` | ||
Array [ | ||
"<bold>Snapshot Summary", | ||
"<bold><green> › 1 snapshot</> written in 1 test suite.", | ||
"<bold><red> › 1 snapshot test</> failed in 1 test suite. <dim>Inspect your code changes or press --u to update them.", | ||
"<bold><green> › 1 snapshot</> updated in 1 test suite.", | ||
"<bold><red> › 1 obsolete snapshot file</> found, press --u to remove it.", | ||
"<bold><red> › 1 obsolete snapshot</> found, press --u to remove it.", | ||
] | ||
`; | ||
exports[`creates a snapshot summary after an update 1`] = ` | ||
Array [ | ||
"<bold>Snapshot Summary", | ||
"<bold><green> › 1 snapshot</> written in 1 test suite.", | ||
"<bold><red> › 1 snapshot test</> failed in 1 test suite. <dim>Inspect your code changes or press --u to update them.", | ||
"<bold><green> › 1 snapshot</> updated in 1 test suite.", | ||
"<bold><red> › 1 obsolete snapshot file</> removed.", | ||
"<bold><red> › 1 obsolete snapshot</> removed.", | ||
] | ||
`; | ||
exports[`creates a snapshot summary with multiple snapshot being written/updated 1`] = ` | ||
Array [ | ||
"<bold>Snapshot Summary", | ||
"<bold><green> › 2 snapshots</> written in 2 test suites.", | ||
"<bold><red> › 2 snapshot tests</> failed in 2 test suites. <dim>Inspect your code changes or press --u to update them.", | ||
"<bold><green> › 2 snapshots</> updated in 2 test suites.", | ||
"<bold><red> › 2 obsolete snapshot files</> found, press --u to remove them..", | ||
"<bold><red> › 2 obsolete snapshots</> found, press --u to remove them.", | ||
] | ||
`; | ||
exports[`returns nothing if there are no updates 1`] = ` | ||
Array [ | ||
"<bold>Snapshot Summary", | ||
] | ||
`; |
78 changes: 78 additions & 0 deletions
78
packages/jest-cli/src/reporters/__tests__/get_snapshot_summary.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* Copyright (c) 2017-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import getSnapshotSummary from '../get_snapshot_summary'; | ||
|
||
const UPDATE_COMMAND = 'press --u'; | ||
|
||
test('creates a snapshot summary', () => { | ||
const snapshots = { | ||
added: 1, | ||
didUpdate: false, | ||
filesAdded: 1, | ||
filesRemoved: 1, | ||
filesUnmatched: 1, | ||
filesUpdated: 1, | ||
matched: 2, | ||
total: 2, | ||
unchecked: 1, | ||
unmatched: 1, | ||
updated: 1, | ||
}; | ||
|
||
expect(getSnapshotSummary(snapshots, UPDATE_COMMAND)).toMatchSnapshot(); | ||
}); | ||
|
||
test('creates a snapshot summary after an update', () => { | ||
const snapshots = { | ||
added: 1, | ||
didUpdate: true, | ||
filesAdded: 1, | ||
filesRemoved: 1, | ||
filesUnmatched: 1, | ||
filesUpdated: 1, | ||
unchecked: 1, | ||
unmatched: 1, | ||
updated: 1, | ||
}; | ||
|
||
expect(getSnapshotSummary(snapshots, UPDATE_COMMAND)).toMatchSnapshot(); | ||
}); | ||
|
||
it('creates a snapshot summary with multiple snapshot being written/updated', () => { | ||
const snapshots = { | ||
added: 2, | ||
didUpdate: false, | ||
filesAdded: 2, | ||
filesRemoved: 2, | ||
filesUnmatched: 2, | ||
filesUpdated: 2, | ||
unchecked: 2, | ||
unmatched: 2, | ||
updated: 2, | ||
}; | ||
|
||
expect(getSnapshotSummary(snapshots, UPDATE_COMMAND)).toMatchSnapshot(); | ||
}); | ||
|
||
it('returns nothing if there are no updates', () => { | ||
const snapshots = { | ||
added: 0, | ||
didUpdate: false, | ||
filesAdded: 0, | ||
filesRemoved: 0, | ||
filesUnmatched: 0, | ||
filesUpdated: 0, | ||
unchecked: 0, | ||
unmatched: 0, | ||
updated: 0, | ||
}; | ||
expect(getSnapshotSummary(snapshots, UPDATE_COMMAND)).toMatchSnapshot(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import type {SnapshotSummary} from 'types/TestResult'; | ||
|
||
import chalk from 'chalk'; | ||
import {pluralize} from './utils'; | ||
|
||
const ARROW = ' \u203A '; | ||
const FAIL_COLOR = chalk.bold.red; | ||
const SNAPSHOT_ADDED = chalk.bold.green; | ||
const SNAPSHOT_NOTE = chalk.dim; | ||
const SNAPSHOT_REMOVED = chalk.bold.red; | ||
const SNAPSHOT_SUMMARY = chalk.bold; | ||
const SNAPSHOT_UPDATED = chalk.bold.green; | ||
|
||
module.exports = ( | ||
snapshots: SnapshotSummary, | ||
updateCommand: string, | ||
): Array<string> => { | ||
const summary = []; | ||
summary.push(SNAPSHOT_SUMMARY('Snapshot Summary')); | ||
if (snapshots.added) { | ||
summary.push( | ||
SNAPSHOT_ADDED(ARROW + pluralize('snapshot', snapshots.added)) + | ||
` written in ${pluralize('test suite', snapshots.filesAdded)}.`, | ||
); | ||
} | ||
|
||
if (snapshots.unmatched) { | ||
summary.push( | ||
FAIL_COLOR(ARROW + pluralize('snapshot test', snapshots.unmatched)) + | ||
` failed in ` + | ||
`${pluralize('test suite', snapshots.filesUnmatched)}. ` + | ||
SNAPSHOT_NOTE( | ||
'Inspect your code changes or ' + updateCommand + ' to update them.', | ||
), | ||
); | ||
} | ||
|
||
if (snapshots.updated) { | ||
summary.push( | ||
SNAPSHOT_UPDATED(ARROW + pluralize('snapshot', snapshots.updated)) + | ||
` updated in ${pluralize('test suite', snapshots.filesUpdated)}.`, | ||
); | ||
} | ||
|
||
if (snapshots.filesRemoved) { | ||
summary.push( | ||
SNAPSHOT_REMOVED( | ||
ARROW + pluralize('obsolete snapshot file', snapshots.filesRemoved), | ||
) + | ||
(snapshots.didUpdate | ||
? ' removed.' | ||
: ' found, ' + | ||
updateCommand + | ||
' to remove ' + | ||
(snapshots.filesRemoved === 1 ? 'it' : 'them.') + | ||
'.'), | ||
); | ||
} | ||
|
||
if (snapshots.unchecked) { | ||
summary.push( | ||
FAIL_COLOR(ARROW + pluralize('obsolete snapshot', snapshots.unchecked)) + | ||
(snapshots.didUpdate | ||
? ' removed.' | ||
: ' found, ' + | ||
updateCommand + | ||
' to remove ' + | ||
(snapshots.filesRemoved === 1 ? 'it' : 'them') + | ||
'.'), | ||
); | ||
} | ||
|
||
return summary; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters