Skip to content

Commit e2c65f8

Browse files
committed
Implement review comments
1 parent 908c73d commit e2c65f8

File tree

4 files changed

+247
-219
lines changed

4 files changed

+247
-219
lines changed

packages/jest-core/src/__tests__/runCore.test.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {tmpdir} from 'os';
9-
import {resolve} from 'path';
108
import {makeGlobalConfig, makeProjectConfig} from '@jest/test-utils';
11-
import {runCore} from '../';
9+
import * as jestUtil from 'jest-util';
10+
import {runCore} from '../runCore';
1211
import runJest from '../runJest';
1312

1413
jest.mock('jest-runtime', () => ({
@@ -22,15 +21,25 @@ jest.mock('../runJest', () =>
2221
onComplete({results: {success: true}});
2322
}),
2423
);
24+
jest.mock('jest-util', () => {
25+
const original = jest.requireActual<typeof jestUtil>('jest-util');
26+
27+
return {
28+
...original,
29+
createDirectory: jest.fn(),
30+
};
31+
});
2532

2633
describe(runCore, () => {
34+
beforeEach(() => {
35+
jest.spyOn(jestUtil, 'createDirectory').mockReturnValue();
36+
});
37+
2738
it('should run once and provide the result', async () => {
2839
const actualResult = await runCore(makeGlobalConfig(), [
29-
makeProjectConfig({
30-
cacheDirectory: resolve(tmpdir(), 'jest_runCore_test'),
31-
}),
40+
makeProjectConfig(),
3241
]);
3342
expect(jest.mocked(runJest)).toHaveBeenCalled();
34-
expect(actualResult).toEqual({results: {success: true}});
43+
expect(actualResult).toEqual({result: {results: {success: true}}});
3544
});
3645
});

packages/jest-core/src/cli/index.ts

Lines changed: 3 additions & 211 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,16 @@ import {performance} from 'perf_hooks';
99
import chalk = require('chalk');
1010
import exit = require('exit');
1111
import * as fs from 'graceful-fs';
12-
import {CustomConsole} from '@jest/console';
13-
import type {AggregatedResult, TestContext} from '@jest/test-result';
12+
import type {AggregatedResult} from '@jest/test-result';
1413
import type {Config} from '@jest/types';
15-
import type {ChangedFilesPromise} from 'jest-changed-files';
1614
import {readConfigs} from 'jest-config';
17-
import type {IHasteMap} from 'jest-haste-map';
18-
import Runtime from 'jest-runtime';
19-
import {createDirectory, preRunMessage} from 'jest-util';
20-
import {TestWatcher} from 'jest-watcher';
2115
import {formatHandleErrors} from '../collectHandles';
22-
import getChangedFilesPromise from '../getChangedFilesPromise';
2316
import getConfigsOfProjectsToRun from '../getConfigsOfProjectsToRun';
2417
import getProjectNamesMissingWarning from '../getProjectNamesMissingWarning';
2518
import getSelectProjectsMessage from '../getSelectProjectsMessage';
26-
import createContext from '../lib/createContext';
27-
import handleDeprecationWarnings from '../lib/handleDeprecationWarnings';
2819
import logDebugMessages from '../lib/logDebugMessages';
2920
import pluralize from '../pluralize';
30-
import runJest from '../runJest';
31-
import type {Filter} from '../types';
32-
import watch from '../watch';
33-
34-
const {print: preRunMessagePrint} = preRunMessage;
35-
36-
type OnCompleteCallback = (results: AggregatedResult) => void | undefined;
21+
import {_run} from '../runCore';
3722

3823
export async function runCLI(
3924
argv: Config.Argv,
@@ -95,7 +80,7 @@ export async function runCLI(
9580
);
9681
}
9782

98-
const results = await runCore(
83+
const results = await _run(
9984
globalConfig,
10085
configsOfProjectsToRun,
10186
hasDeprecationWarnings,
@@ -126,196 +111,3 @@ export async function runCLI(
126111
performance.mark('jest/runCLI:end');
127112
return {globalConfig, results};
128113
}
129-
130-
const buildContextsAndHasteMaps = async (
131-
configs: Array<Config.ProjectConfig>,
132-
globalConfig: Config.GlobalConfig,
133-
outputStream: NodeJS.WriteStream,
134-
) => {
135-
const hasteMapInstances = Array(configs.length);
136-
const contexts = await Promise.all(
137-
configs.map(async (config, index) => {
138-
createDirectory(config.cacheDirectory);
139-
const hasteMapInstance = await Runtime.createHasteMap(config, {
140-
console: new CustomConsole(outputStream, outputStream),
141-
maxWorkers: Math.max(
142-
1,
143-
Math.floor(globalConfig.maxWorkers / configs.length),
144-
),
145-
resetCache: !config.cache,
146-
watch: globalConfig.watch || globalConfig.watchAll,
147-
watchman: globalConfig.watchman,
148-
workerThreads: globalConfig.workerThreads,
149-
});
150-
hasteMapInstances[index] = hasteMapInstance;
151-
return createContext(config, await hasteMapInstance.build());
152-
}),
153-
);
154-
155-
return {contexts, hasteMapInstances};
156-
};
157-
158-
/**
159-
* Runs Jest either in watch mode or as a one-off. This is a lower-level API than `runCLI` and is intended for internal use by `runCLI` or externally.
160-
* Note that `process.exit` might be called when using `globalConfig.watch` or `globalConfig.watchAll` is true.
161-
*
162-
* @param globalConfig The global configuration to use for this run. It can be obtained using `readConfigs` (imported from 'jest-config').
163-
* @param configs The project configurations to run. It can be obtained using `readConfigs` (imported from 'jest-config').
164-
* @param warnForDeprecations Whether or not to warn for deprecation messages when `globalConfig.watch` or `globalConfig.watchAll` is true.
165-
* @param outputStream The stream to write output to. If not provided, it defaults to `process.stdout`.
166-
* @returns A Promise that resolves to the result, or never resolves when `globalConfig.watch` or `globalConfig.watchAll` is true.
167-
* @example
168-
* import { runCore, readConfigs } from 'jest';
169-
*
170-
* const { globalConfig, configs } = await readConfigs(process.argv, [process.cwd()]);
171-
* const results = await runCore(globalConfig, configs);
172-
* console.log(results);
173-
*/
174-
export const runCore = async (
175-
globalConfig: Config.GlobalConfig,
176-
configs: Array<Config.ProjectConfig>,
177-
warnForDeprecations = false,
178-
outputStream: NodeJS.WriteStream = process.stdout,
179-
): Promise<AggregatedResult> => {
180-
// Queries to hg/git can take a while, so we need to start the process
181-
// as soon as possible, so by the time we need the result it's already there.
182-
const changedFilesPromise = getChangedFilesPromise(globalConfig, configs);
183-
if (changedFilesPromise) {
184-
performance.mark('jest/getChangedFiles:start');
185-
changedFilesPromise.finally(() => {
186-
performance.mark('jest/getChangedFiles:end');
187-
});
188-
}
189-
190-
// Filter may need to do an HTTP call or something similar to setup.
191-
// We will wait on an async response from this before using the filter.
192-
let filter: Filter | undefined;
193-
if (globalConfig.filter && !globalConfig.skipFilter) {
194-
const rawFilter = require(globalConfig.filter);
195-
let filterSetupPromise: Promise<unknown | undefined> | undefined;
196-
if (rawFilter.setup) {
197-
// Wrap filter setup Promise to avoid "uncaught Promise" error.
198-
// If an error is returned, we surface it in the return value.
199-
filterSetupPromise = (async () => {
200-
try {
201-
await rawFilter.setup();
202-
} catch (err) {
203-
return err;
204-
}
205-
return undefined;
206-
})();
207-
}
208-
filter = async (testPaths: Array<string>) => {
209-
if (filterSetupPromise) {
210-
// Expect an undefined return value unless there was an error.
211-
const err = await filterSetupPromise;
212-
if (err) {
213-
throw err;
214-
}
215-
}
216-
return rawFilter(testPaths);
217-
};
218-
}
219-
220-
performance.mark('jest/buildContextsAndHasteMaps:start');
221-
const {contexts, hasteMapInstances} = await buildContextsAndHasteMaps(
222-
configs,
223-
globalConfig,
224-
outputStream,
225-
);
226-
performance.mark('jest/buildContextsAndHasteMaps:end');
227-
228-
if (globalConfig.watch || globalConfig.watchAll) {
229-
await runWatch(
230-
contexts,
231-
configs,
232-
warnForDeprecations,
233-
globalConfig,
234-
outputStream,
235-
hasteMapInstances,
236-
filter,
237-
);
238-
// If in watch mode, return the promise that will never resolve.
239-
// If the watch mode is interrupted, watch should handle the process
240-
// shutdown.
241-
// eslint-disable-next-line @typescript-eslint/no-empty-function
242-
return new Promise(() => {});
243-
} else {
244-
let result: AggregatedResult;
245-
await runWithoutWatch(
246-
globalConfig,
247-
contexts,
248-
outputStream,
249-
r => {
250-
result = r;
251-
},
252-
changedFilesPromise,
253-
filter,
254-
);
255-
return result!;
256-
}
257-
};
258-
259-
const runWatch = async (
260-
contexts: Array<TestContext>,
261-
_configs: Array<Config.ProjectConfig>,
262-
warnForDeprecations: boolean,
263-
globalConfig: Config.GlobalConfig,
264-
outputStream: NodeJS.WriteStream,
265-
hasteMapInstances: Array<IHasteMap>,
266-
filter?: Filter,
267-
) => {
268-
if (warnForDeprecations) {
269-
try {
270-
await handleDeprecationWarnings(outputStream, process.stdin);
271-
return await watch(
272-
globalConfig,
273-
contexts,
274-
outputStream,
275-
hasteMapInstances,
276-
undefined,
277-
undefined,
278-
filter,
279-
);
280-
} catch {
281-
exit(0);
282-
}
283-
}
284-
285-
return watch(
286-
globalConfig,
287-
contexts,
288-
outputStream,
289-
hasteMapInstances,
290-
undefined,
291-
undefined,
292-
filter,
293-
);
294-
};
295-
296-
const runWithoutWatch = async (
297-
globalConfig: Config.GlobalConfig,
298-
contexts: Array<TestContext>,
299-
outputStream: NodeJS.WriteStream,
300-
onComplete: OnCompleteCallback,
301-
changedFilesPromise?: ChangedFilesPromise,
302-
filter?: Filter,
303-
) => {
304-
const startRun = async (): Promise<void | null> => {
305-
if (!globalConfig.listTests) {
306-
preRunMessagePrint(outputStream);
307-
}
308-
return runJest({
309-
changedFilesPromise,
310-
contexts,
311-
failedTestsCache: undefined,
312-
filter,
313-
globalConfig,
314-
onComplete,
315-
outputStream,
316-
startRun,
317-
testWatcher: new TestWatcher({isWatchMode: false}),
318-
});
319-
};
320-
return startRun();
321-
};

packages/jest-core/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
export {default as SearchSource} from './SearchSource';
99
export {createTestScheduler} from './TestScheduler';
10-
export {runCLI, runCore} from './cli';
10+
export {runCLI} from './cli';
11+
export {runCore} from './runCore';
1112
export {default as getVersion} from './version';
1213
export {readConfigs, readInitialOptions} from 'jest-config';

0 commit comments

Comments
 (0)