Skip to content

Commit 6af2f67

Browse files
authored
create @jest/core package (#7696)
1 parent c14a45d commit 6af2f67

File tree

101 files changed

+385
-317
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+385
-317
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
- `[jest-snapshot]`: Migrate to TypeScript ([#7899](https://github.com/facebook/jest/pull/7899))
4343
- `[@jest/transform]`: New package extracted from `jest-runtime` ([#7915](https://github.com/facebook/jest/pull/7915))
4444
- `[babel-plugin-jest-hoist]`: Migrate to TypeScript ([#7898](https://github.com/facebook/jest/pull/7898))
45+
- `[@jest/core]` Create new package, which is `jest-cli` minus `yargs` and `prompts` ([#7696](https://github.com/facebook/jest/pull/7696))
4546

4647
### Performance
4748

jest.config.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ module.exports = {
3939
'/packages/.*/build',
4040
'/packages/.*/build-es5',
4141
'/packages/.*/src/__tests__/setPrettyPrint.ts',
42-
'/packages/jest-cli/src/__tests__/test_root',
43-
'/packages/jest-cli/src/__tests__/__fixtures__/',
44-
'/packages/jest-cli/src/lib/__tests__/fixtures/',
42+
'/packages/jest-core/src/__tests__/test_root',
43+
'/packages/jest-core/src/__tests__/__fixtures__/',
44+
'/packages/jest-cli/src/init/__tests__/fixtures/',
4545
'/packages/jest-haste-map/src/__tests__/haste_impl.js',
4646
'/packages/jest-haste-map/src/__tests__/dependencyExtractor.js',
4747
'/packages/jest-resolve-dependencies/src/__tests__/__fixtures__/',

packages/jest-cli/package.json

+2-32
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,24 @@
22
"name": "jest-cli",
33
"description": "Delightful JavaScript Testing.",
44
"version": "24.1.0",
5-
"main": "build/jest.js",
5+
"main": "build/index.js",
66
"dependencies": {
7-
"@jest/reporters": "^24.1.0",
8-
"@jest/transform": "^24.1.0",
9-
"ansi-escapes": "^3.0.0",
7+
"@jest/core": "^24.1.0",
108
"chalk": "^2.0.1",
119
"exit": "^0.1.2",
12-
"glob": "^7.1.2",
13-
"graceful-fs": "^4.1.15",
1410
"import-local": "^2.0.0",
1511
"is-ci": "^2.0.0",
16-
"jest-changed-files": "^24.0.0",
1712
"jest-config": "^24.1.0",
18-
"jest-environment-jsdom": "^24.0.0",
19-
"jest-get-type": "^24.0.0",
20-
"jest-haste-map": "^24.0.0",
21-
"jest-message-util": "^24.0.0",
22-
"jest-regex-util": "^24.0.0",
23-
"jest-resolve-dependencies": "^24.1.0",
24-
"jest-runner": "^24.1.0",
25-
"jest-runtime": "^24.1.0",
26-
"jest-snapshot": "^24.1.0",
2713
"jest-util": "^24.0.0",
2814
"jest-validate": "^24.0.0",
29-
"jest-watcher": "^24.0.0",
30-
"micromatch": "^3.1.10",
31-
"p-each-series": "^1.0.0",
32-
"pirates": "^4.0.0",
3315
"prompts": "^2.0.1",
3416
"realpath-native": "^1.1.0",
35-
"rimraf": "^2.5.4",
36-
"string-length": "^2.0.0",
37-
"strip-ansi": "^5.0.0",
38-
"which": "^1.2.12",
3917
"yargs": "^12.0.2"
4018
},
4119
"devDependencies": {
42-
"@types/ansi-escapes": "^3.0.0",
4320
"@types/exit": "^0.1.30",
44-
"@types/glob": "^7.1.1",
45-
"@types/graceful-fs": "^4.1.2",
4621
"@types/is-ci": "^1.1.0",
47-
"@types/micromatch": "^3.1.0",
4822
"@types/prompts": "^1.2.0",
49-
"@types/rimraf": "^2.0.2",
50-
"@types/string-length": "^2.0.0",
51-
"@types/strip-ansi": "^3.0.0",
52-
"@types/which": "^1.3.1",
5323
"@types/yargs": "^12.0.2"
5424
},
5525
"bin": {

packages/jest-cli/src/cli/index.js

+42-237
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,23 @@ import type {Argv} from 'types/Argv';
1212
import type {GlobalConfig, Path} from 'types/Config';
1313

1414
import path from 'path';
15-
import {Console, clearLine, createDirectory, preRunMessage} from 'jest-util';
15+
import {clearLine} from 'jest-util';
1616
import {validateCLIOptions} from 'jest-validate';
17-
import {readConfigs, deprecationEntries} from 'jest-config';
17+
import {deprecationEntries} from 'jest-config';
18+
import {runCLI} from '@jest/core';
1819
import * as args from './args';
1920
import chalk from 'chalk';
20-
import createContext from '../lib/create_context';
2121
import exit from 'exit';
22-
import getChangedFilesPromise from '../getChangedFilesPromise';
23-
import {formatHandleErrors} from '../collectHandles';
24-
import handleDeprecationWarnings from '../lib/handle_deprecation_warnings';
25-
import runJest from '../runJest';
26-
import Runtime from 'jest-runtime';
27-
import TestWatcher from '../TestWatcher';
28-
import watch from '../watch';
29-
import pluralize from '../pluralize';
3022
import yargs from 'yargs';
31-
import rimraf from 'rimraf';
3223
import {sync as realpath} from 'realpath-native';
33-
import init from '../lib/init';
34-
import logDebugMessages from '../lib/log_debug_messages';
35-
import getVersion from '../version';
24+
import init from '../init';
3625

37-
const {print: preRunMessagePrint} = preRunMessage;
26+
import {version as VERSION} from '../../package.json';
3827

3928
export async function run(maybeArgv?: Argv, project?: Path) {
4029
try {
4130
// $FlowFixMe:`allow reduced return
42-
const argv: Argv = buildArgv(maybeArgv, project);
31+
const argv: Argv = buildArgv(maybeArgv);
4332

4433
if (argv.init) {
4534
await init();
@@ -59,129 +48,9 @@ export async function run(maybeArgv?: Argv, project?: Path) {
5948
}
6049
}
6150

62-
export const runCLI = async (
63-
argv: Argv,
64-
projects: Array<Path>,
65-
): Promise<{results: AggregatedResult, globalConfig: GlobalConfig}> => {
66-
const realFs = require('fs');
67-
const fs = require('graceful-fs');
68-
fs.gracefulify(realFs);
69-
70-
let results;
71-
72-
// If we output a JSON object, we can't write anything to stdout, since
73-
// it'll break the JSON structure and it won't be valid.
74-
const outputStream =
75-
argv.json || argv.useStderr ? process.stderr : process.stdout;
76-
77-
const {globalConfig, configs, hasDeprecationWarnings} = readConfigs(
78-
argv,
79-
projects,
80-
);
81-
82-
if (argv.debug) {
83-
logDebugMessages(globalConfig, configs, outputStream);
84-
}
85-
86-
if (argv.showConfig) {
87-
logDebugMessages(globalConfig, configs, process.stdout);
88-
exit(0);
89-
}
90-
91-
if (argv.clearCache) {
92-
configs.forEach(config => {
93-
rimraf.sync(config.cacheDirectory);
94-
process.stdout.write(`Cleared ${config.cacheDirectory}\n`);
95-
});
96-
97-
exit(0);
98-
}
99-
100-
await _run(
101-
globalConfig,
102-
configs,
103-
hasDeprecationWarnings,
104-
outputStream,
105-
(r: AggregatedResult) => (results = r),
106-
);
107-
108-
if (argv.watch || argv.watchAll) {
109-
// If in watch mode, return the promise that will never resolve.
110-
// If the watch mode is interrupted, watch should handle the process
111-
// shutdown.
112-
return new Promise(() => {});
113-
}
114-
115-
if (!results) {
116-
throw new Error(
117-
'AggregatedResult must be present after test run is complete',
118-
);
119-
}
120-
121-
const {openHandles} = results;
122-
123-
if (openHandles && openHandles.length) {
124-
const formatted = formatHandleErrors(openHandles, configs[0]);
125-
126-
const openHandlesString = pluralize('open handle', formatted.length, 's');
127-
128-
const message =
129-
chalk.red(
130-
`\nJest has detected the following ${openHandlesString} potentially keeping Jest from exiting:\n\n`,
131-
) + formatted.join('\n\n');
132-
133-
console.error(message);
134-
}
135-
136-
return Promise.resolve({globalConfig, results});
137-
};
138-
139-
const readResultsAndExit = (
140-
result: ?AggregatedResult,
141-
globalConfig: GlobalConfig,
142-
) => {
143-
const code = !result || result.success ? 0 : globalConfig.testFailureExitCode;
144-
145-
// Only exit if needed
146-
process.on('exit', () => {
147-
if (typeof code === 'number' && code !== 0) {
148-
process.exitCode = code;
149-
}
150-
});
151-
152-
if (globalConfig.forceExit) {
153-
if (!globalConfig.detectOpenHandles) {
154-
console.error(
155-
chalk.red.bold('Force exiting Jest\n\n') +
156-
chalk.red(
157-
'Have you considered using `--detectOpenHandles` to detect ' +
158-
'async operations that kept running after all tests finished?',
159-
),
160-
);
161-
}
162-
163-
exit(code);
164-
} else if (!globalConfig.detectOpenHandles) {
165-
setTimeout(() => {
166-
console.error(
167-
chalk.red.bold(
168-
'Jest did not exit one second after the test run has completed.\n\n',
169-
) +
170-
chalk.red(
171-
'This usually means that there are asynchronous operations that ' +
172-
"weren't stopped in your tests. Consider running Jest with " +
173-
'`--detectOpenHandles` to troubleshoot this issue.',
174-
),
175-
);
176-
// $FlowFixMe: `unref` exists in Node
177-
}, 1000).unref();
178-
}
179-
};
180-
181-
export const buildArgv = (maybeArgv: ?Argv, project: ?Path) => {
51+
export const buildArgv = (maybeArgv: ?Argv) => {
18252
const version =
183-
getVersion() +
184-
(__dirname.includes(`packages${path.sep}jest-cli`) ? '-dev' : '');
53+
VERSION + (__dirname.includes(`packages${path.sep}jest-cli`) ? '-dev' : '');
18554

18655
const rawArgv: Argv | string[] = maybeArgv || process.argv.slice(2);
18756
const argv: Argv = yargs(rawArgv)
@@ -234,108 +103,44 @@ const getProjectListFromCLIArgs = (argv, project: ?Path) => {
234103
return projects;
235104
};
236105

237-
const buildContextsAndHasteMaps = async (
238-
configs,
239-
globalConfig,
240-
outputStream,
241-
) => {
242-
const hasteMapInstances = Array(configs.length);
243-
const contexts = await Promise.all(
244-
configs.map(async (config, index) => {
245-
createDirectory(config.cacheDirectory);
246-
const hasteMapInstance = Runtime.createHasteMap(config, {
247-
console: new Console(outputStream, outputStream),
248-
maxWorkers: globalConfig.maxWorkers,
249-
resetCache: !config.cache,
250-
watch: globalConfig.watch || globalConfig.watchAll,
251-
watchman: globalConfig.watchman,
252-
});
253-
hasteMapInstances[index] = hasteMapInstance;
254-
return createContext(config, await hasteMapInstance.build());
255-
}),
256-
);
257-
258-
return {contexts, hasteMapInstances};
259-
};
260-
261-
const _run = async (
262-
globalConfig,
263-
configs,
264-
hasDeprecationWarnings,
265-
outputStream,
266-
onComplete,
106+
const readResultsAndExit = (
107+
result: ?AggregatedResult,
108+
globalConfig: GlobalConfig,
267109
) => {
268-
// Queries to hg/git can take a while, so we need to start the process
269-
// as soon as possible, so by the time we need the result it's already there.
270-
const changedFilesPromise = getChangedFilesPromise(globalConfig, configs);
110+
const code = !result || result.success ? 0 : globalConfig.testFailureExitCode;
271111

272-
const {contexts, hasteMapInstances} = await buildContextsAndHasteMaps(
273-
configs,
274-
globalConfig,
275-
outputStream,
276-
);
112+
// Only exit if needed
113+
process.on('exit', () => {
114+
if (typeof code === 'number' && code !== 0) {
115+
process.exitCode = code;
116+
}
117+
});
277118

278-
globalConfig.watch || globalConfig.watchAll
279-
? await runWatch(
280-
contexts,
281-
configs,
282-
hasDeprecationWarnings,
283-
globalConfig,
284-
outputStream,
285-
hasteMapInstances,
286-
changedFilesPromise,
287-
)
288-
: await runWithoutWatch(
289-
globalConfig,
290-
contexts,
291-
outputStream,
292-
onComplete,
293-
changedFilesPromise,
119+
if (globalConfig.forceExit) {
120+
if (!globalConfig.detectOpenHandles) {
121+
console.error(
122+
chalk.red.bold('Force exiting Jest\n\n') +
123+
chalk.red(
124+
'Have you considered using `--detectOpenHandles` to detect ' +
125+
'async operations that kept running after all tests finished?',
126+
),
294127
);
295-
};
296-
297-
const runWatch = async (
298-
contexts,
299-
configs,
300-
hasDeprecationWarnings,
301-
globalConfig,
302-
outputStream,
303-
hasteMapInstances,
304-
changedFilesPromise,
305-
) => {
306-
if (hasDeprecationWarnings) {
307-
try {
308-
await handleDeprecationWarnings(outputStream, process.stdin);
309-
return watch(globalConfig, contexts, outputStream, hasteMapInstances);
310-
} catch (e) {
311-
exit(0);
312128
}
313-
}
314-
315-
return watch(globalConfig, contexts, outputStream, hasteMapInstances);
316-
};
317129

318-
const runWithoutWatch = async (
319-
globalConfig,
320-
contexts,
321-
outputStream,
322-
onComplete,
323-
changedFilesPromise,
324-
) => {
325-
const startRun = async () => {
326-
if (!globalConfig.listTests) {
327-
preRunMessagePrint(outputStream);
328-
}
329-
return await runJest({
330-
changedFilesPromise,
331-
contexts,
332-
failedTestsCache: null,
333-
globalConfig,
334-
onComplete,
335-
outputStream,
336-
startRun,
337-
testWatcher: new TestWatcher({isWatchMode: false}),
338-
});
339-
};
340-
return await startRun();
130+
exit(code);
131+
} else if (!globalConfig.detectOpenHandles) {
132+
setTimeout(() => {
133+
console.error(
134+
chalk.red.bold(
135+
'Jest did not exit one second after the test run has completed.\n\n',
136+
) +
137+
chalk.red(
138+
'This usually means that there are asynchronous operations that ' +
139+
"weren't stopped in your tests. Consider running Jest with " +
140+
'`--detectOpenHandles` to troubleshoot this issue.',
141+
),
142+
);
143+
// $FlowFixMe: `unref` exists in Node
144+
}, 1000).unref();
145+
}
341146
};

0 commit comments

Comments
 (0)