Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: do not use deep imports in unit tests #13905

Merged
merged 4 commits into from
Feb 13, 2023
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
112 changes: 67 additions & 45 deletions packages/jest-core/src/__tests__/watch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,27 @@ 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,
// 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 = {
rootDir: __dirname,
Expand All @@ -131,10 +151,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,
Expand All @@ -147,10 +167,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,
Expand All @@ -163,8 +183,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,
Expand All @@ -177,12 +197,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,
Expand All @@ -195,9 +215,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,
Expand All @@ -207,8 +227,8 @@ describe('Watch mode flows', () => {
pipe,
hasteMapInstances,
stdin,
);
}).not.toThrow();
),
).resolves.toBeUndefined();
});

it('shows prompts for WatchPlugins in alphabetical order', async () => {
Expand Down Expand Up @@ -239,7 +259,7 @@ describe('Watch mode flows', () => {
it('shows update snapshot prompt (without interactive)', async () => {
results = {snapshot: {failure: true}};

watch(
await watch(
{
...globalConfig,
rootDir: __dirname,
Expand Down Expand Up @@ -282,7 +302,7 @@ describe('Watch mode flows', () => {
],
};

watch(
await watch(
{
...globalConfig,
rootDir: __dirname,
Expand Down Expand Up @@ -317,7 +337,7 @@ describe('Watch mode flows', () => {
{virtual: true},
);

watch(
await watch(
{
...globalConfig,
rootDir: __dirname,
Expand Down Expand Up @@ -354,7 +374,7 @@ describe('Watch mode flows', () => {
{virtual: true},
);

watch(
await watch(
{
...globalConfig,
rootDir: __dirname,
Expand Down Expand Up @@ -433,7 +453,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(
Expand All @@ -453,17 +473,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();
},
);

Expand Down Expand Up @@ -686,7 +708,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');
Expand Down Expand Up @@ -811,8 +833,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');
Expand All @@ -825,8 +847,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');
Expand All @@ -839,8 +861,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);
Expand All @@ -849,7 +871,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');
Expand All @@ -867,7 +889,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');
Expand All @@ -885,7 +907,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');
Expand All @@ -911,7 +933,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}});
Expand Down Expand Up @@ -951,17 +973,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');
Expand Down
5 changes: 3 additions & 2 deletions packages/jest-runtime/src/__tests__/defaultResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};