Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
81 changes: 81 additions & 0 deletions packages/jest-cli/src/__tests__/watch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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();

Expand All @@ -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,
});
});

Expand Down
6 changes: 3 additions & 3 deletions packages/jest-cli/src/plugins/test_name_pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ class TestNamePatternPlugin extends BaseWatchPlugin {

run(globalConfig: GlobalConfig, updateConfigAndRun: Function): Promise<void> {
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,
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-cli/src/plugins/test_path_pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class TestPathPatternPlugin extends BaseWatchPlugin {

testPathPatternPrompt.run(
(value: string) => {
updateConfigAndRun({testPathPattern: value});
updateConfigAndRun({mode: 'watch', testPathPattern: value});
res();
},
rej,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class UpdateSnapshotInteractivePlugin extends BaseWatchPlugin {
this._failedSnapshotTestPaths,
(path: string, shouldUpdateSnapshot: boolean) => {
updateConfigAndRun({
mode: 'watch',
testNamePattern: '',
testPathPattern: path,
updateSnapshot: shouldUpdateSnapshot ? 'all' : 'none',
Expand Down
5 changes: 4 additions & 1 deletion packages/jest-cli/src/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -312,6 +314,7 @@ export default function watch(
break;
case KEYS.C:
updateConfigAndRun({
mode: 'watch',
testNamePattern: '',
testPathPattern: '',
});
Expand Down