Skip to content

Commit f877284

Browse files
rogeliogcpojer
authored andcommitted
Fix update snapshot when mode: watchAll (#5696)
* Fix update snapshot * Update CHANGELOG * Update CHANGELOG.md
1 parent 48560bf commit f877284

6 files changed

Lines changed: 92 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
config ([#5674](https://github.com/facebook/jest/pull/5674))
1919
* `[jest-runtime]` remove retainLines from coverage instrumentation
2020
([#5692](https://github.com/facebook/jest/pull/5692))
21+
* `[jest-cli]` Fix update snapshot issue when using watchAll
22+
([#5696](https://github.com/facebook/jest/pull/5696))
2123

2224
## 22.4.2
2325

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ jest.doMock(
6565
const watch = require('../watch').default;
6666

6767
const nextTick = () => new Promise(res => process.nextTick(res));
68+
const toHex = char => Number(char.charCodeAt(0)).toString(16);
6869

6970
afterEach(runJestMock.mockReset);
7071

@@ -410,6 +411,68 @@ describe('Watch mode flows', () => {
410411
expect(runJestMock).toHaveBeenCalledTimes(2);
411412
});
412413

414+
it('Pressing "t" reruns the tests in "test name pattern" mode', async () => {
415+
const hooks = new JestHooks();
416+
417+
watch(globalConfig, contexts, pipe, hasteMapInstances, stdin, hooks);
418+
runJestMock.mockReset();
419+
420+
stdin.emit(KEYS.T);
421+
['t', 'e', 's', 't'].map(toHex).forEach(key => stdin.emit(key));
422+
stdin.emit(KEYS.ENTER);
423+
await nextTick();
424+
425+
expect(runJestMock.mock.calls[0][0].globalConfig).toMatchObject({
426+
testNamePattern: 'test',
427+
testPathPattern: '',
428+
watch: true,
429+
watchAll: false,
430+
});
431+
});
432+
433+
it('Pressing "p" reruns the tests in "filename pattern" mode', async () => {
434+
const hooks = new JestHooks();
435+
436+
watch(globalConfig, contexts, pipe, hasteMapInstances, stdin, hooks);
437+
runJestMock.mockReset();
438+
439+
stdin.emit(KEYS.P);
440+
['f', 'i', 'l', 'e'].map(toHex).forEach(key => stdin.emit(key));
441+
stdin.emit(KEYS.ENTER);
442+
await nextTick();
443+
444+
expect(runJestMock.mock.calls[0][0].globalConfig).toMatchObject({
445+
testNamePattern: '',
446+
testPathPattern: 'file',
447+
watch: true,
448+
watchAll: false,
449+
});
450+
});
451+
452+
it('Can combine "p" and "t" filters', async () => {
453+
const hooks = new JestHooks();
454+
455+
watch(globalConfig, contexts, pipe, hasteMapInstances, stdin, hooks);
456+
runJestMock.mockReset();
457+
458+
stdin.emit(KEYS.P);
459+
['f', 'i', 'l', 'e'].map(toHex).forEach(key => stdin.emit(key));
460+
stdin.emit(KEYS.ENTER);
461+
await nextTick();
462+
463+
stdin.emit(KEYS.T);
464+
['t', 'e', 's', 't'].map(toHex).forEach(key => stdin.emit(key));
465+
stdin.emit(KEYS.ENTER);
466+
await nextTick();
467+
468+
expect(runJestMock.mock.calls[1][0].globalConfig).toMatchObject({
469+
testNamePattern: 'test',
470+
testPathPattern: 'file',
471+
watch: true,
472+
watchAll: false,
473+
});
474+
});
475+
413476
it('Pressing "u" reruns the tests in "update snapshot" mode', async () => {
414477
const hooks = new JestHooks();
415478

@@ -426,14 +489,32 @@ describe('Watch mode flows', () => {
426489
expect(runJestMock.mock.calls[0][0].globalConfig).toMatchObject({
427490
updateSnapshot: 'all',
428491
watch: true,
492+
watchAll: false,
429493
});
430494

431495
stdin.emit(KEYS.A);
496+
432497
await nextTick();
433498
// updateSnapshot is not sticky after a run.
434499
expect(runJestMock.mock.calls[1][0].globalConfig).toMatchObject({
435500
updateSnapshot: 'new',
436501
watch: false,
502+
watchAll: true,
503+
});
504+
505+
results = {snapshot: {failure: true}};
506+
507+
stdin.emit(KEYS.A);
508+
await nextTick();
509+
510+
runJestMock.mockReset();
511+
stdin.emit(KEYS.U);
512+
await nextTick();
513+
514+
expect(runJestMock.mock.calls[0][0].globalConfig).toMatchObject({
515+
updateSnapshot: 'all',
516+
watch: false,
517+
watchAll: true,
437518
});
438519
});
439520

packages/jest-cli/src/plugins/test_name_pattern.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ class TestNamePatternPlugin extends BaseWatchPlugin {
3636

3737
run(globalConfig: GlobalConfig, updateConfigAndRun: Function): Promise<void> {
3838
return new Promise((res, rej) => {
39-
const testPathPatternPrompt = new TestNamePatternPrompt(
39+
const testNamePatternPrompt = new TestNamePatternPrompt(
4040
this._stdout,
4141
this._prompt,
4242
);
4343

44-
testPathPatternPrompt.run(
44+
testNamePatternPrompt.run(
4545
(value: string) => {
46-
updateConfigAndRun({testNamePattern: value});
46+
updateConfigAndRun({mode: 'watch', testNamePattern: value});
4747
res();
4848
},
4949
rej,

packages/jest-cli/src/plugins/test_path_pattern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class TestPathPatternPlugin extends BaseWatchPlugin {
4444

4545
testPathPatternPrompt.run(
4646
(value: string) => {
47-
updateConfigAndRun({testPathPattern: value});
47+
updateConfigAndRun({mode: 'watch', testPathPattern: value});
4848
res();
4949
},
5050
rej,

packages/jest-cli/src/plugins/update_snapshots_interactive.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class UpdateSnapshotInteractivePlugin extends BaseWatchPlugin {
4747
this._failedSnapshotTestPaths,
4848
(path: string, shouldUpdateSnapshot: boolean) => {
4949
updateConfigAndRun({
50+
mode: 'watch',
5051
testNamePattern: '',
5152
testPathPattern: path,
5253
updateSnapshot: shouldUpdateSnapshot ? 'all' : 'none',

packages/jest-cli/src/watch.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,19 @@ export default function watch(
8282
});
8383

8484
const updateConfigAndRun = ({
85+
mode,
8586
testNamePattern,
8687
testPathPattern,
8788
updateSnapshot,
8889
}: {
90+
mode?: 'watch' | 'watchAll',
8991
testNamePattern?: string,
9092
testPathPattern?: string,
9193
updateSnapshot?: SnapshotUpdateState,
9294
} = {}) => {
9395
const previousUpdateSnapshot = globalConfig.updateSnapshot;
9496
globalConfig = updateGlobalConfig(globalConfig, {
95-
mode: 'watch',
97+
mode,
9698
testNamePattern:
9799
testNamePattern !== undefined
98100
? testNamePattern
@@ -312,6 +314,7 @@ export default function watch(
312314
break;
313315
case KEYS.C:
314316
updateConfigAndRun({
317+
mode: 'watch',
315318
testNamePattern: '',
316319
testPathPattern: '',
317320
});

0 commit comments

Comments
 (0)