-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
feat(jest-worker): add JestWorkerFarm
helper type
#12753
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 |
---|---|---|
|
@@ -26,10 +26,12 @@ import type { | |
} from '@jest/test-result'; | ||
import type {Config} from '@jest/types'; | ||
import {clearLine, isInteractive} from 'jest-util'; | ||
import {Worker} from 'jest-worker'; | ||
import {JestWorkerFarm, Worker} from 'jest-worker'; | ||
import BaseReporter from './BaseReporter'; | ||
import getWatermarks from './getWatermarks'; | ||
import type {CoverageWorker, ReporterContext} from './types'; | ||
import type {ReporterContext} from './types'; | ||
|
||
type CoverageWorker = typeof import('./CoverageWorker'); | ||
|
||
const FAIL_COLOR = chalk.bold.red; | ||
const RUNNING_TEST_COLOR = chalk.bold.dim; | ||
|
@@ -137,7 +139,9 @@ export default class CoverageReporter extends BaseReporter { | |
); | ||
} | ||
|
||
let worker: CoverageWorker | Worker; | ||
let worker: | ||
| JestWorkerFarm<CoverageWorker> | ||
| typeof import('./CoverageWorker'); | ||
|
||
if (this._globalConfig.maxWorkers <= 1) { | ||
worker = require('./CoverageWorker'); | ||
|
@@ -148,7 +152,7 @@ export default class CoverageReporter extends BaseReporter { | |
forkOptions: {serialization: 'json'}, | ||
maxRetries: 2, | ||
numWorkers: this._globalConfig.maxWorkers, | ||
}); | ||
}) as JestWorkerFarm<CoverageWorker>; | ||
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. Type casting is needed for now. Will work without casting after implementing |
||
} | ||
|
||
const instrumentation = files.map(async fileObj => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {expectError, expectType} from 'tsd-lite'; | ||
import type {JestWorkerFarm} from 'jest-worker'; | ||
import type * as testWorker from './testWorker'; | ||
|
||
type TestWorker = { | ||
runTest: () => void; | ||
isResult: boolean; | ||
end: () => void; // reserved keys should be excluded from returned type | ||
getStderr: () => string; | ||
getStdout: () => string; | ||
setup: () => void; | ||
teardown: () => void; | ||
}; | ||
|
||
// unknown JestWorkerFarm | ||
|
||
declare const unknownWorkerFarm: JestWorkerFarm<Record<string, unknown>>; | ||
|
||
expectError(unknownWorkerFarm.runTest()); | ||
expectError(unknownWorkerFarm.runTestAsync()); | ||
|
||
expectError(unknownWorkerFarm.getResult()); | ||
expectError(unknownWorkerFarm.isResult); | ||
|
||
expectError(unknownWorkerFarm.setup()); | ||
expectError(unknownWorkerFarm.teardown()); | ||
|
||
expectType<Promise<{forceExited: boolean}>>(unknownWorkerFarm.end()); | ||
expectType<NodeJS.ReadableStream>(unknownWorkerFarm.getStderr()); | ||
expectType<NodeJS.ReadableStream>(unknownWorkerFarm.getStdout()); | ||
|
||
// detected JestWorkerFarm | ||
|
||
declare const detectedWorkerFarm: JestWorkerFarm<typeof testWorker>; | ||
|
||
expectType<Promise<void>>(detectedWorkerFarm.runTest()); | ||
expectType<Promise<void>>(detectedWorkerFarm.runTestAsync()); | ||
|
||
expectError(detectedWorkerFarm.getResult()); | ||
expectError(detectedWorkerFarm.isResult); | ||
|
||
expectError(detectedWorkerFarm.setup()); | ||
expectError(detectedWorkerFarm.teardown()); | ||
|
||
expectError<Promise<void>>(detectedWorkerFarm.end()); | ||
expectType<Promise<{forceExited: boolean}>>(detectedWorkerFarm.end()); | ||
|
||
expectError<Promise<string>>(detectedWorkerFarm.getStderr()); | ||
expectType<NodeJS.ReadableStream>(detectedWorkerFarm.getStderr()); | ||
|
||
expectError<Promise<string>>(detectedWorkerFarm.getStdout()); | ||
expectType<NodeJS.ReadableStream>(detectedWorkerFarm.getStdout()); | ||
|
||
// typed JestWorkerFarm | ||
|
||
declare const typedWorkerFarm: JestWorkerFarm<TestWorker>; | ||
|
||
expectType<Promise<void>>(typedWorkerFarm.runTest()); | ||
|
||
expectError(typedWorkerFarm.isResult); | ||
expectError(typedWorkerFarm.runTestAsync()); | ||
|
||
expectError(typedWorkerFarm.setup()); | ||
expectError(typedWorkerFarm.teardown()); | ||
|
||
expectError<Promise<void>>(typedWorkerFarm.end()); | ||
expectType<Promise<{forceExited: boolean}>>(typedWorkerFarm.end()); | ||
|
||
expectError<Promise<string>>(typedWorkerFarm.getStderr()); | ||
expectType<NodeJS.ReadableStream>(typedWorkerFarm.getStderr()); | ||
|
||
expectError<Promise<string>>(typedWorkerFarm.getStdout()); | ||
expectType<NodeJS.ReadableStream>(typedWorkerFarm.getStdout()); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
export function runTest(): void {} | ||
export async function runTestAsync(): Promise<void> {} | ||
|
||
function getResult(): string { | ||
return 'result'; | ||
} | ||
export const isResult = true; | ||
|
||
// reserved keys should be excluded from returned type | ||
|
||
export function end(): void {} | ||
export function getStderr(): string { | ||
return 'get-err'; | ||
} | ||
export function getStdout(): string { | ||
return 'get-out'; | ||
} | ||
export function setup(): void {} | ||
export function teardown(): void {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"extends": "../../../tsconfig.json", | ||
"compilerOptions": { | ||
"composite": false, | ||
"noUnusedLocals": false, | ||
"noUnusedParameters": false, | ||
"skipLibCheck": true, | ||
|
||
"types": ["node"] | ||
}, | ||
"include": ["./**/*"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,20 +9,20 @@ import FifoQueue from './FifoQueue'; | |
import { | ||
CHILD_MESSAGE_CALL, | ||
ChildMessage, | ||
FarmOptions, | ||
OnCustomMessage, | ||
OnEnd, | ||
OnStart, | ||
PromiseWithCustomMessage, | ||
QueueChildMessage, | ||
TaskQueue, | ||
WorkerCallback, | ||
WorkerFarmOptions, | ||
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 rename is not a breaking change, because the type was not exposed before. |
||
WorkerInterface, | ||
WorkerSchedulingPolicy, | ||
} from './types'; | ||
|
||
export default class Farm { | ||
private readonly _computeWorkerKey: FarmOptions['computeWorkerKey']; | ||
private readonly _computeWorkerKey: WorkerFarmOptions['computeWorkerKey']; | ||
private readonly _workerSchedulingPolicy: WorkerSchedulingPolicy; | ||
private readonly _cacheKeys: Record<string, WorkerInterface> = | ||
Object.create(null); | ||
|
@@ -33,7 +33,7 @@ export default class Farm { | |
constructor( | ||
private _numOfWorkers: number, | ||
private _callback: WorkerCallback, | ||
options: FarmOptions = {}, | ||
options: WorkerFarmOptions = {}, | ||
) { | ||
this._computeWorkerKey = options.computeWorkerKey; | ||
this._workerSchedulingPolicy = | ||
|
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.
Just cleaned up type exports.