Skip to content

Commit a24204a

Browse files
rickhanloniicpojer
authored andcommitted
Hide interactive mode if there are no failed snapshot tests (#5450)
* Hide interactive mode if there are no failed snapshot tests * Update changelog
1 parent 1fda501 commit a24204a

File tree

4 files changed

+120
-8
lines changed

4 files changed

+120
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
### Fixes
99

10+
* `[jest-cli]` Hide interactive mode if there are no failed snapshot tests ([#5450](https://github.com/facebook/jest/pull/5450))
1011
* `[babel-jest]` Remove retainLines from babel-jest
1112
([#5326](https://github.com/facebook/jest/pull/5439))
1213
* `[jest-cli]` Glob patterns ignore non-`require`-able files (e.g. `README.md`)
@@ -25,7 +26,7 @@
2526
* `[expect]` Make `rejects` and `resolves` synchronously validate its argument.
2627
([#5364](https://github.com/facebook/jest/pull/5364))
2728
* `[docs]` Add tutorial page for ES6 class mocks.
28-
([#5383]https://github.com/facebook/jest/pull/5383))
29+
([#5383](https://github.com/facebook/jest/pull/5383))
2930
* `[jest-resolve]` Search required modules in node_modules and then in custom
3031
paths. ([#5403](https://github.com/facebook/jest/pull/5403))
3132
* `[jest-resolve]` Get builtin modules from node core.

packages/jest-cli/src/__tests__/__snapshots__/watch.test.js.snap

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,38 @@ Watch Usage
3838
],
3939
]
4040
`;
41+
42+
exports[`Watch mode flows shows update snapshot prompt (with interactive) 1`] = `
43+
Array [
44+
Array [
45+
"
46+
Watch Usage
47+
› Press a to run all tests.
48+
› Press f to run only failed tests.
49+
› Press u to update failing snapshots.
50+
› Press i to update failing snapshots interactively.
51+
› Press p to filter by a filename regex pattern.
52+
› Press t to filter by a test name regex pattern.
53+
› Press q to quit watch mode.
54+
› Press Enter to trigger a test run.
55+
",
56+
],
57+
]
58+
`;
59+
60+
exports[`Watch mode flows shows update snapshot prompt (without interactive) 1`] = `
61+
Array [
62+
Array [
63+
"
64+
Watch Usage
65+
› Press a to run all tests.
66+
› Press f to run only failed tests.
67+
› Press u to update failing snapshots.
68+
› Press p to filter by a filename regex pattern.
69+
› Press t to filter by a test name regex pattern.
70+
› Press q to quit watch mode.
71+
› Press Enter to trigger a test run.
72+
",
73+
],
74+
]
75+
`;

packages/jest-cli/src/__tests__/watch.test.js

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import TestWatcher from '../test_watcher';
1313
import {KEYS} from '../constants';
1414

1515
const runJestMock = jest.fn();
16-
1716
const watchPluginPath = `${__dirname}/__fixtures__/watch_plugin`;
1817
const watchPlugin2Path = `${__dirname}/__fixtures__/watch_plugin2`;
18+
let results;
1919

2020
jest.doMock('chalk', () => new chalk.constructor({enabled: false}));
2121
jest.doMock(
@@ -27,7 +27,7 @@ jest.doMock(
2727
runJestMock.apply(null, args);
2828

2929
// Call the callback
30-
onComplete({snapshot: {}});
30+
onComplete(results);
3131

3232
return Promise.resolve();
3333
},
@@ -70,6 +70,7 @@ describe('Watch mode flows', () => {
7070
hasteMapInstances = [{on: () => {}}];
7171
contexts = [{config}];
7272
stdin = new MockStdin();
73+
results = {snapshot: {}};
7374
});
7475

7576
it('Correctly passing test path pattern', () => {
@@ -175,6 +176,61 @@ describe('Watch mode flows', () => {
175176
expect(pipeMockCalls.slice(determiningTestsToRun + 1)).toMatchSnapshot();
176177
});
177178

179+
it('shows update snapshot prompt (without interactive)', async () => {
180+
jest.unmock('jest-util');
181+
const util = require('jest-util');
182+
util.isInteractive = true;
183+
results = {snapshot: {failure: true}};
184+
185+
const ci_watch = require('../watch').default;
186+
ci_watch(
187+
Object.assign({}, globalConfig, {
188+
rootDir: __dirname,
189+
watchPlugins: [],
190+
}),
191+
contexts,
192+
pipe,
193+
hasteMapInstances,
194+
stdin,
195+
);
196+
197+
const pipeMockCalls = pipe.write.mock.calls;
198+
199+
const determiningTestsToRun = pipeMockCalls.findIndex(
200+
([c]) => c === 'Determining test suites to run...',
201+
);
202+
203+
expect(pipeMockCalls.slice(determiningTestsToRun + 1)).toMatchSnapshot();
204+
});
205+
206+
it('shows update snapshot prompt (with interactive)', async () => {
207+
jest.unmock('jest-util');
208+
const util = require('jest-util');
209+
util.isInteractive = true;
210+
util.getFailedSnapshotTests = jest.fn(() => ['test.js']);
211+
results = {snapshot: {failure: true}};
212+
213+
const ci_watch = require('../watch').default;
214+
ci_watch(
215+
Object.assign({}, globalConfig, {
216+
rootDir: __dirname,
217+
watchPlugins: [],
218+
}),
219+
contexts,
220+
pipe,
221+
hasteMapInstances,
222+
stdin,
223+
);
224+
225+
const pipeMockCalls = pipe.write.mock.calls;
226+
227+
const determiningTestsToRun = pipeMockCalls.findIndex(
228+
([c]) => c === 'Determining test suites to run...',
229+
);
230+
231+
expect(pipeMockCalls.slice(determiningTestsToRun + 1)).toMatchSnapshot();
232+
});
233+
178234
it('triggers enter on a WatchPlugin when its key is pressed', () => {
179235
const plugin = require(watchPluginPath);
180236

packages/jest-cli/src/watch.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export default function watch(
6969
searchSource: new SearchSource(context),
7070
}));
7171
let hasSnapshotFailure = false;
72+
let hasSnapshotFailureInteractive = false;
7273
let isRunning = false;
7374
let testWatcher;
7475
let shouldDisplayWatchUsage = true;
@@ -130,8 +131,9 @@ export default function watch(
130131
globalConfig,
131132
onComplete: results => {
132133
isRunning = false;
133-
hasSnapshotFailure = !!results.snapshot.failure;
134134
failedSnapshotTestPaths = getFailedSnapshotTests(results);
135+
hasSnapshotFailure = !!results.snapshot.failure;
136+
hasSnapshotFailureInteractive = failedSnapshotTestPaths.length > 0;
135137

136138
// Create a new testWatcher instance so that re-runs won't be blocked.
137139
// The old instance that was passed to Jest will still be interrupted
@@ -149,7 +151,12 @@ export default function watch(
149151
}
150152
if (shouldDisplayWatchUsage) {
151153
outputStream.write(
152-
usage(globalConfig, watchPlugins, hasSnapshotFailure),
154+
usage(
155+
globalConfig,
156+
watchPlugins,
157+
hasSnapshotFailure,
158+
hasSnapshotFailureInteractive,
159+
),
153160
);
154161
shouldDisplayWatchUsage = false; // hide Watch Usage after first run
155162
isWatchUsageDisplayed = true;
@@ -327,7 +334,12 @@ export default function watch(
327334
outputStream.write(ansiEscapes.cursorUp());
328335
outputStream.write(ansiEscapes.eraseDown);
329336
outputStream.write(
330-
usage(globalConfig, watchPlugins, hasSnapshotFailure),
337+
usage(
338+
globalConfig,
339+
watchPlugins,
340+
hasSnapshotFailure,
341+
hasSnapshotFailureInteractive,
342+
),
331343
);
332344
isWatchUsageDisplayed = true;
333345
shouldDisplayWatchUsage = false;
@@ -339,7 +351,14 @@ export default function watch(
339351
const onCancelPatternPrompt = () => {
340352
outputStream.write(ansiEscapes.cursorHide);
341353
outputStream.write(ansiEscapes.clearScreen);
342-
outputStream.write(usage(globalConfig, watchPlugins, hasSnapshotFailure));
354+
outputStream.write(
355+
usage(
356+
globalConfig,
357+
watchPlugins,
358+
hasSnapshotFailure,
359+
hasSnapshotFailureInteractive,
360+
),
361+
);
343362
outputStream.write(ansiEscapes.cursorShow);
344363
};
345364

@@ -380,6 +399,7 @@ const usage = (
380399
globalConfig,
381400
watchPlugins: WatchPluginRegistry,
382401
snapshotFailure,
402+
snapshotFailureInteractive,
383403
delimiter = '\n',
384404
) => {
385405
const messages = [
@@ -415,7 +435,7 @@ const usage = (
415435
chalk.dim(' to update failing snapshots.')
416436
: null,
417437

418-
snapshotFailure
438+
snapshotFailureInteractive
419439
? chalk.dim(' \u203A Press ') +
420440
'i' +
421441
chalk.dim(' to update failing snapshots interactively.')

0 commit comments

Comments
 (0)