-
-
Couldn't load subscription status.
- Fork 6.6k
Move coverage worker into jest-worker #4596
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| */ | ||
|
|
||
| import type {GlobalConfig} from 'types/Config'; | ||
| import type {TestResult} from 'types/TestResult'; | ||
| import type { | ||
| OnTestFailure, | ||
| OnTestStart, | ||
|
|
@@ -17,13 +18,18 @@ import type { | |
| TestWatcher, | ||
| } from 'types/TestRunner'; | ||
|
|
||
| import pify from 'pify'; | ||
| import type {WorkerData} from './test_worker'; | ||
|
|
||
| import runTest from './run_test'; | ||
| import throat from 'throat'; | ||
| import workerFarm from 'worker-farm'; | ||
| import Worker from 'jest-worker'; | ||
|
|
||
| const TEST_WORKER_PATH = require.resolve('./test_worker'); | ||
|
|
||
| type CoverageWorker = Worker & { | ||
| default: (WorkerData) => Promise<TestResult>, | ||
| }; | ||
|
|
||
| class TestRunner { | ||
| _globalConfig: GlobalConfig; | ||
|
|
||
|
|
@@ -89,17 +95,12 @@ class TestRunner { | |
| onResult: OnTestSuccess, | ||
| onFailure: OnTestFailure, | ||
| ) { | ||
| const farm = workerFarm( | ||
| { | ||
| autoStart: true, | ||
| maxConcurrentCallsPerWorker: 1, | ||
| maxConcurrentWorkers: this._globalConfig.maxWorkers, | ||
| maxRetries: 2, // Allow for a couple of transient errors. | ||
| }, | ||
| TEST_WORKER_PATH, | ||
| ); | ||
| // $FlowFixMe: "default" property will be exposed. | ||
| const worker: CoverageWorker = new Worker(TEST_WORKER_PATH, { | ||
| numWorkers: this._globalConfig.maxWorkers, | ||
| }); | ||
|
|
||
| const mutex = throat(this._globalConfig.maxWorkers); | ||
| const worker = pify(farm); | ||
|
|
||
| // Send test suites to workers continuously instead of all at once to track | ||
| // the start time of individual tests. | ||
|
|
@@ -108,14 +109,16 @@ class TestRunner { | |
| if (watcher.isInterrupted()) { | ||
| return Promise.reject(); | ||
| } | ||
|
|
||
| await onStart(test); | ||
| return worker({ | ||
|
|
||
| return worker.default({ | ||
| config: test.context.config, | ||
| globalConfig: this._globalConfig, | ||
| path: test.path, | ||
| rawModuleMap: watcher.isWatchMode() | ||
| ? test.context.moduleMap.getRawModuleMap() | ||
| : null, | ||
| : undefined, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is causing the single test failure |
||
| }); | ||
| }); | ||
|
|
||
|
|
@@ -146,7 +149,7 @@ class TestRunner { | |
| ), | ||
| ); | ||
|
|
||
| const cleanup = () => workerFarm.end(farm); | ||
| const cleanup = () => worker.end(); | ||
| return Promise.race([runAllTests, onInterrupt]).then(cleanup, cleanup); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,15 +22,13 @@ import {separateMessageFromStack} from 'jest-message-util'; | |
| import Runtime from 'jest-runtime'; | ||
| import runTest from './run_test'; | ||
|
|
||
| type WorkerData = {| | ||
| export type WorkerData = {| | ||
| config: ProjectConfig, | ||
| globalConfig: GlobalConfig, | ||
| path: Path, | ||
| rawModuleMap?: RawModuleMap, | ||
| |}; | ||
|
|
||
| type WorkerCallback = (error: ?SerializableError, result?: TestResult) => void; | ||
|
|
||
| const formatError = (error: string | Error): SerializableError => { | ||
| if (typeof error === 'string') { | ||
| const {message, stack} = separateMessageFromStack(error); | ||
|
|
@@ -69,33 +67,20 @@ const getResolver = (config, rawModuleMap) => { | |
| } | ||
| }; | ||
|
|
||
| // Cannot be ESM export because of worker-farm | ||
| module.exports = ( | ||
| {config, globalConfig, path, rawModuleMap}: WorkerData, | ||
| callback: WorkerCallback, | ||
| ) => { | ||
| let parentExited = false; | ||
| const disconnectCallback = () => (parentExited = true); | ||
| const removeListener = () => | ||
| process.removeListener('disconnect', disconnectCallback); | ||
| process.on('disconnect', disconnectCallback); | ||
|
|
||
| export default async function({ | ||
| config, | ||
| globalConfig, | ||
| path, | ||
| rawModuleMap, | ||
| }: WorkerData): Promise<TestResult> { | ||
| try { | ||
| runTest(path, globalConfig, config, getResolver(config, rawModuleMap)).then( | ||
| result => { | ||
| removeListener(); | ||
| if (!parentExited) { | ||
| callback(null, result); | ||
| } | ||
| }, | ||
| error => { | ||
| removeListener(); | ||
| if (!parentExited) { | ||
| callback(formatError(error)); | ||
| } | ||
| }, | ||
| return await runTest( | ||
| path, | ||
| globalConfig, | ||
| config, | ||
| getResolver(config, rawModuleMap), | ||
| ); | ||
| } catch (error) { | ||
| callback(formatError(error)); | ||
| } catch (err) { | ||
| throw formatError(err); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any particular reason |
||
| } | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be cool if
default(as long as__esModule: true) would be the actual default export instead of attached to thedefaultkey (so you could just doworker(stuff)).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have been thinking about this, and I'm happy to see feedback on that direction. The reason of the
defaultcomes from the fact that a class instance is returned fromnew. The change here would be to makeconstructorreturn a method, but theninstanceof JestWorkerwouldn't work.I will think more about it and see what's the best way of fixing the issue; but generally speaking, I agree with your comment.