From 5585afaf6877820288f459c7d94e70084b29b765 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 13 Feb 2023 16:51:42 +0100 Subject: [PATCH 1/4] chore: tweak watch mode test --- packages/jest-core/src/__tests__/watch.test.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/jest-core/src/__tests__/watch.test.js b/packages/jest-core/src/__tests__/watch.test.js index 46988831d6c1..6afd427f3ea6 100644 --- a/packages/jest-core/src/__tests__/watch.test.js +++ b/packages/jest-core/src/__tests__/watch.test.js @@ -111,7 +111,11 @@ describe('Watch mode flows', () => { beforeEach(() => { isInteractive = true; - jest.doMock('jest-util/build/isInteractive', () => isInteractive); + jest.doMock('jest-util', () => { + const original = jest.requireActual('jest-util'); + + return {...original, isInteractive}; + }); watch = require('../watch').default; const config = { rootDir: __dirname, From 17744cfefb9c19979f696a6640bc6834427a035c Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 13 Feb 2023 16:55:38 +0100 Subject: [PATCH 2/4] runtime test as well --- packages/jest-runtime/src/__tests__/defaultResolver.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/jest-runtime/src/__tests__/defaultResolver.js b/packages/jest-runtime/src/__tests__/defaultResolver.js index b7bba046c7fc..7d3d6a822d34 100644 --- a/packages/jest-runtime/src/__tests__/defaultResolver.js +++ b/packages/jest-runtime/src/__tests__/defaultResolver.js @@ -5,5 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -import resolver from 'jest-resolve/build/defaultResolver.js'; -module.exports = resolver; +module.exports = (request, opts) => { + return opts.defaultResolver(request, opts); +}; From 1e618a15ceea8ef74c694405f25a24121ac9b614 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 13 Feb 2023 17:23:48 +0100 Subject: [PATCH 3/4] add missing awaits --- .../jest-core/src/__tests__/watch.test.js | 90 ++++++++++--------- 1 file changed, 46 insertions(+), 44 deletions(-) diff --git a/packages/jest-core/src/__tests__/watch.test.js b/packages/jest-core/src/__tests__/watch.test.js index 6afd427f3ea6..27414319e6b6 100644 --- a/packages/jest-core/src/__tests__/watch.test.js +++ b/packages/jest-core/src/__tests__/watch.test.js @@ -135,10 +135,10 @@ describe('Watch mode flows', () => { jest.resetModules(); }); - it('Correctly passing test path pattern', () => { + it('Correctly passing test path pattern', async () => { globalConfig.testPathPattern = 'test-*'; - watch(globalConfig, contexts, pipe, hasteMapInstances, stdin); + await watch(globalConfig, contexts, pipe, hasteMapInstances, stdin); expect(runJestMock.mock.calls[0][0]).toMatchObject({ contexts, @@ -151,10 +151,10 @@ describe('Watch mode flows', () => { }); }); - it('Correctly passing test name pattern', () => { + it('Correctly passing test name pattern', async () => { globalConfig.testNamePattern = 'test-*'; - watch(globalConfig, contexts, pipe, hasteMapInstances, stdin); + await watch(globalConfig, contexts, pipe, hasteMapInstances, stdin); expect(runJestMock.mock.calls[0][0]).toMatchObject({ contexts, @@ -167,8 +167,8 @@ describe('Watch mode flows', () => { }); }); - it('Runs Jest once by default and shows usage', () => { - watch(globalConfig, contexts, pipe, hasteMapInstances, stdin); + it('Runs Jest once by default and shows usage', async () => { + await watch(globalConfig, contexts, pipe, hasteMapInstances, stdin); expect(runJestMock.mock.calls[0][0]).toMatchObject({ contexts, globalConfig, @@ -181,12 +181,12 @@ describe('Watch mode flows', () => { expect(pipe.write.mock.calls.reverse()[0]).toMatchSnapshot(); }); - it('Runs Jest in a non-interactive environment not showing usage', () => { + it('Runs Jest in a non-interactive environment not showing usage', async () => { jest.resetModules(); isInteractive = false; watch = require('../watch').default; - watch(globalConfig, contexts, pipe, hasteMapInstances, stdin); + await watch(globalConfig, contexts, pipe, hasteMapInstances, stdin); expect(runJestMock.mock.calls[0][0]).toMatchObject({ contexts, globalConfig, @@ -199,9 +199,9 @@ describe('Watch mode flows', () => { expect(pipe.write.mock.calls.reverse()[0]).toMatchSnapshot(); }); - it('resolves relative to the package root', () => { - expect(async () => { - await watch( + it('resolves relative to the package root', async () => { + await expect( + watch( { ...globalConfig, rootDir: __dirname, @@ -211,8 +211,8 @@ describe('Watch mode flows', () => { pipe, hasteMapInstances, stdin, - ); - }).not.toThrow(); + ), + ).resolves.toBeUndefined(); }); it('shows prompts for WatchPlugins in alphabetical order', async () => { @@ -243,7 +243,7 @@ describe('Watch mode flows', () => { it('shows update snapshot prompt (without interactive)', async () => { results = {snapshot: {failure: true}}; - watch( + await watch( { ...globalConfig, rootDir: __dirname, @@ -286,7 +286,7 @@ describe('Watch mode flows', () => { ], }; - watch( + await watch( { ...globalConfig, rootDir: __dirname, @@ -321,7 +321,7 @@ describe('Watch mode flows', () => { {virtual: true}, ); - watch( + await watch( { ...globalConfig, rootDir: __dirname, @@ -358,7 +358,7 @@ describe('Watch mode flows', () => { {virtual: true}, ); - watch( + await watch( { ...globalConfig, rootDir: __dirname, @@ -437,7 +437,7 @@ describe('Watch mode flows', () => { ${'p'} | ${'TestPathPattern'} `( 'allows WatchPlugins to override non-reserved internal plugins', - ({key}) => { + async ({key}) => { const run = jest.fn(() => Promise.resolve()); const pluginPath = `${__dirname}/__fixtures__/plugin_valid_override_${key}`; jest.doMock( @@ -457,17 +457,19 @@ describe('Watch mode flows', () => { {virtual: true}, ); - watch( - { - ...globalConfig, - rootDir: __dirname, - watchPlugins: [{config: {}, path: pluginPath}], - }, - contexts, - pipe, - hasteMapInstances, - stdin, - ); + await expect( + watch( + { + ...globalConfig, + rootDir: __dirname, + watchPlugins: [{config: {}, path: pluginPath}], + }, + contexts, + pipe, + hasteMapInstances, + stdin, + ), + ).resolves.toBeUndefined(); }, ); @@ -690,7 +692,7 @@ describe('Watch mode flows', () => { watchPlugins: [{config: {}, path: pluginPath}], }; - watch(config, contexts, pipe, hasteMapInstances, stdin); + await watch(config, contexts, pipe, hasteMapInstances, stdin); await nextTick(); stdin.emit('x'); @@ -815,8 +817,8 @@ describe('Watch mode flows', () => { expect(showPrompt2).toHaveBeenCalled(); }); - it('Pressing "o" runs test in "only changed files" mode', () => { - watch(globalConfig, contexts, pipe, hasteMapInstances, stdin); + it('Pressing "o" runs test in "only changed files" mode', async () => { + await watch(globalConfig, contexts, pipe, hasteMapInstances, stdin); runJestMock.mockReset(); stdin.emit('o'); @@ -829,8 +831,8 @@ describe('Watch mode flows', () => { }); }); - it('Pressing "a" runs test in "watch all" mode', () => { - watch(globalConfig, contexts, pipe, hasteMapInstances, stdin); + it('Pressing "a" runs test in "watch all" mode', async () => { + await watch(globalConfig, contexts, pipe, hasteMapInstances, stdin); runJestMock.mockReset(); stdin.emit('a'); @@ -843,8 +845,8 @@ describe('Watch mode flows', () => { }); }); - it('Pressing "ENTER" reruns the tests', () => { - watch(globalConfig, contexts, pipe, hasteMapInstances, stdin); + it('Pressing "ENTER" reruns the tests', async () => { + await watch(globalConfig, contexts, pipe, hasteMapInstances, stdin); expect(runJestMock).toHaveBeenCalledTimes(1); stdin.emit(KEYS.ENTER); expect(runJestMock).toHaveBeenCalledTimes(2); @@ -853,7 +855,7 @@ describe('Watch mode flows', () => { it('Pressing "t" reruns the tests in "test name pattern" mode', async () => { const hooks = new JestHook(); - watch(globalConfig, contexts, pipe, hasteMapInstances, stdin, hooks); + await watch(globalConfig, contexts, pipe, hasteMapInstances, stdin, hooks); runJestMock.mockReset(); stdin.emit('t'); @@ -871,7 +873,7 @@ describe('Watch mode flows', () => { it('Pressing "p" reruns the tests in "filename pattern" mode', async () => { const hooks = new JestHook(); - watch(globalConfig, contexts, pipe, hasteMapInstances, stdin, hooks); + await watch(globalConfig, contexts, pipe, hasteMapInstances, stdin, hooks); runJestMock.mockReset(); stdin.emit('p'); @@ -889,7 +891,7 @@ describe('Watch mode flows', () => { it('Can combine "p" and "t" filters', async () => { const hooks = new JestHook(); - watch(globalConfig, contexts, pipe, hasteMapInstances, stdin, hooks); + await watch(globalConfig, contexts, pipe, hasteMapInstances, stdin, hooks); runJestMock.mockReset(); stdin.emit('p'); @@ -915,7 +917,7 @@ describe('Watch mode flows', () => { globalConfig.updateSnapshot = 'new'; - watch(globalConfig, contexts, pipe, hasteMapInstances, stdin, hooks); + await watch(globalConfig, contexts, pipe, hasteMapInstances, stdin, hooks); runJestMock.mockReset(); hooks.getEmitter().onTestRunComplete({snapshot: {failure: true}}); @@ -955,17 +957,17 @@ describe('Watch mode flows', () => { }); }); - it('passWithNoTest should be set to true in watch mode', () => { + it('passWithNoTest should be set to true in watch mode', async () => { globalConfig.passWithNoTests = false; - watch(globalConfig, contexts, pipe, hasteMapInstances, stdin); + await watch(globalConfig, contexts, pipe, hasteMapInstances, stdin); globalConfig.passWithNoTests = true; expect(runJestMock.mock.calls[0][0]).toMatchObject({ globalConfig, }); }); - it('shows the correct usage for the f key in "only failed tests" mode', () => { - watch(globalConfig, contexts, pipe, hasteMapInstances, stdin); + it('shows the correct usage for the f key in "only failed tests" mode', async () => { + await watch(globalConfig, contexts, pipe, hasteMapInstances, stdin); stdin.emit('f'); stdin.emit('w'); From d8a4b7a071ac2a3dac9827961927bd8349f117fa Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 13 Feb 2023 17:38:00 +0100 Subject: [PATCH 4/4] fix test --- packages/jest-core/src/__tests__/watch.test.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/jest-core/src/__tests__/watch.test.js b/packages/jest-core/src/__tests__/watch.test.js index 27414319e6b6..7f412e1561a3 100644 --- a/packages/jest-core/src/__tests__/watch.test.js +++ b/packages/jest-core/src/__tests__/watch.test.js @@ -114,7 +114,23 @@ describe('Watch mode flows', () => { jest.doMock('jest-util', () => { const original = jest.requireActual('jest-util'); - return {...original, isInteractive}; + return { + ...original, + isInteractive, + // this imports internally, so we need to check `isInteractive` manually + preRunMessage: { + print: function mockedPrint(stream) { + if (isInteractive) { + stream.write('Determining test suites to run...'); + } + }, + remove: function mockedRemove(stream) { + if (isInteractive) { + original.clearLine(stream); + } + }, + }, + }; }); watch = require('../watch').default; const config = {