diff --git a/CHANGELOG.md b/CHANGELOG.md index 5193d38f60e1..396ecdc27cf2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ config ([#5674](https://github.com/facebook/jest/pull/5674)) * `[jest-runtime]` remove retainLines from coverage instrumentation ([#5692](https://github.com/facebook/jest/pull/5692)) +* `[jest-cli]` Fix update snapshot issue when using watchAll + ([#5696](https://github.com/facebook/jest/pull/5696)) ## 22.4.2 diff --git a/packages/jest-cli/src/__tests__/watch.test.js b/packages/jest-cli/src/__tests__/watch.test.js index 2a89a3829a71..15ab2cb90051 100644 --- a/packages/jest-cli/src/__tests__/watch.test.js +++ b/packages/jest-cli/src/__tests__/watch.test.js @@ -65,6 +65,7 @@ jest.doMock( const watch = require('../watch').default; const nextTick = () => new Promise(res => process.nextTick(res)); +const toHex = char => Number(char.charCodeAt(0)).toString(16); afterEach(runJestMock.mockReset); @@ -410,6 +411,68 @@ describe('Watch mode flows', () => { expect(runJestMock).toHaveBeenCalledTimes(2); }); + it('Pressing "t" reruns the tests in "test name pattern" mode', async () => { + const hooks = new JestHooks(); + + watch(globalConfig, contexts, pipe, hasteMapInstances, stdin, hooks); + runJestMock.mockReset(); + + stdin.emit(KEYS.T); + ['t', 'e', 's', 't'].map(toHex).forEach(key => stdin.emit(key)); + stdin.emit(KEYS.ENTER); + await nextTick(); + + expect(runJestMock.mock.calls[0][0].globalConfig).toMatchObject({ + testNamePattern: 'test', + testPathPattern: '', + watch: true, + watchAll: false, + }); + }); + + it('Pressing "p" reruns the tests in "filename pattern" mode', async () => { + const hooks = new JestHooks(); + + watch(globalConfig, contexts, pipe, hasteMapInstances, stdin, hooks); + runJestMock.mockReset(); + + stdin.emit(KEYS.P); + ['f', 'i', 'l', 'e'].map(toHex).forEach(key => stdin.emit(key)); + stdin.emit(KEYS.ENTER); + await nextTick(); + + expect(runJestMock.mock.calls[0][0].globalConfig).toMatchObject({ + testNamePattern: '', + testPathPattern: 'file', + watch: true, + watchAll: false, + }); + }); + + it('Can combine "p" and "t" filters', async () => { + const hooks = new JestHooks(); + + watch(globalConfig, contexts, pipe, hasteMapInstances, stdin, hooks); + runJestMock.mockReset(); + + stdin.emit(KEYS.P); + ['f', 'i', 'l', 'e'].map(toHex).forEach(key => stdin.emit(key)); + stdin.emit(KEYS.ENTER); + await nextTick(); + + stdin.emit(KEYS.T); + ['t', 'e', 's', 't'].map(toHex).forEach(key => stdin.emit(key)); + stdin.emit(KEYS.ENTER); + await nextTick(); + + expect(runJestMock.mock.calls[1][0].globalConfig).toMatchObject({ + testNamePattern: 'test', + testPathPattern: 'file', + watch: true, + watchAll: false, + }); + }); + it('Pressing "u" reruns the tests in "update snapshot" mode', async () => { const hooks = new JestHooks(); @@ -426,14 +489,32 @@ describe('Watch mode flows', () => { expect(runJestMock.mock.calls[0][0].globalConfig).toMatchObject({ updateSnapshot: 'all', watch: true, + watchAll: false, }); stdin.emit(KEYS.A); + await nextTick(); // updateSnapshot is not sticky after a run. expect(runJestMock.mock.calls[1][0].globalConfig).toMatchObject({ updateSnapshot: 'new', watch: false, + watchAll: true, + }); + + results = {snapshot: {failure: true}}; + + stdin.emit(KEYS.A); + await nextTick(); + + runJestMock.mockReset(); + stdin.emit(KEYS.U); + await nextTick(); + + expect(runJestMock.mock.calls[0][0].globalConfig).toMatchObject({ + updateSnapshot: 'all', + watch: false, + watchAll: true, }); }); diff --git a/packages/jest-cli/src/plugins/test_name_pattern.js b/packages/jest-cli/src/plugins/test_name_pattern.js index fbfc20cca6aa..1d28c3fee001 100644 --- a/packages/jest-cli/src/plugins/test_name_pattern.js +++ b/packages/jest-cli/src/plugins/test_name_pattern.js @@ -36,14 +36,14 @@ class TestNamePatternPlugin extends BaseWatchPlugin { run(globalConfig: GlobalConfig, updateConfigAndRun: Function): Promise { return new Promise((res, rej) => { - const testPathPatternPrompt = new TestNamePatternPrompt( + const testNamePatternPrompt = new TestNamePatternPrompt( this._stdout, this._prompt, ); - testPathPatternPrompt.run( + testNamePatternPrompt.run( (value: string) => { - updateConfigAndRun({testNamePattern: value}); + updateConfigAndRun({mode: 'watch', testNamePattern: value}); res(); }, rej, diff --git a/packages/jest-cli/src/plugins/test_path_pattern.js b/packages/jest-cli/src/plugins/test_path_pattern.js index 2e63ac56ad23..5512938341e9 100644 --- a/packages/jest-cli/src/plugins/test_path_pattern.js +++ b/packages/jest-cli/src/plugins/test_path_pattern.js @@ -44,7 +44,7 @@ class TestPathPatternPlugin extends BaseWatchPlugin { testPathPatternPrompt.run( (value: string) => { - updateConfigAndRun({testPathPattern: value}); + updateConfigAndRun({mode: 'watch', testPathPattern: value}); res(); }, rej, diff --git a/packages/jest-cli/src/plugins/update_snapshots_interactive.js b/packages/jest-cli/src/plugins/update_snapshots_interactive.js index 2be6de43dce9..32be3ee4a114 100644 --- a/packages/jest-cli/src/plugins/update_snapshots_interactive.js +++ b/packages/jest-cli/src/plugins/update_snapshots_interactive.js @@ -47,6 +47,7 @@ class UpdateSnapshotInteractivePlugin extends BaseWatchPlugin { this._failedSnapshotTestPaths, (path: string, shouldUpdateSnapshot: boolean) => { updateConfigAndRun({ + mode: 'watch', testNamePattern: '', testPathPattern: path, updateSnapshot: shouldUpdateSnapshot ? 'all' : 'none', diff --git a/packages/jest-cli/src/watch.js b/packages/jest-cli/src/watch.js index 7b5ee1b20ec6..b10ddb7a7219 100644 --- a/packages/jest-cli/src/watch.js +++ b/packages/jest-cli/src/watch.js @@ -82,17 +82,19 @@ export default function watch( }); const updateConfigAndRun = ({ + mode, testNamePattern, testPathPattern, updateSnapshot, }: { + mode?: 'watch' | 'watchAll', testNamePattern?: string, testPathPattern?: string, updateSnapshot?: SnapshotUpdateState, } = {}) => { const previousUpdateSnapshot = globalConfig.updateSnapshot; globalConfig = updateGlobalConfig(globalConfig, { - mode: 'watch', + mode, testNamePattern: testNamePattern !== undefined ? testNamePattern @@ -312,6 +314,7 @@ export default function watch( break; case KEYS.C: updateConfigAndRun({ + mode: 'watch', testNamePattern: '', testPathPattern: '', });