Skip to content

Commit

Permalink
fixes suggested by @simen
Browse files Browse the repository at this point in the history
  • Loading branch information
sauravhiremath committed Jul 21, 2020
1 parent 5666ef2 commit f8fbd6b
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 15 deletions.
15 changes: 7 additions & 8 deletions packages/jest-worker/src/Farm.ts
Expand Up @@ -50,17 +50,12 @@ export default class Farm {
method: string,
...args: Array<any>
): PromiseWithCustomMessage<unknown> {
const customMessageListeners: Set<OnCustomMessage> = new Set();
const customMessageListeners = new Set<OnCustomMessage>();

const addCustomMessageListener = (listener: OnCustomMessage) => {
customMessageListeners.add(listener);
return () => {
// Check if the following check is required
customMessageListeners.forEach(customListener => {
if (customListener === listener) {
customMessageListeners.delete(customListener);
}
});
customMessageListeners.delete(listener);
};
};

Expand Down Expand Up @@ -89,7 +84,11 @@ export default class Farm {

const onEnd: OnEnd = (error: Error | null, result: unknown) => {
customMessageListeners.clear();
return error ? reject(error) : resolve(result);
if (error) {
return reject(error);
} else {
return resolve(result);
}
};

const task = {onCustomMessage, onEnd, onStart, request};
Expand Down
Expand Up @@ -38,10 +38,6 @@ function assertCallsToChild(childNum, ...calls) {
});
}

jest.mock('worker_threads', () => {
throw Error('Unsupported');
});

describe('Jest Worker Integration', () => {
beforeEach(() => {
mockForkedProcesses = [];
Expand Down
3 changes: 1 addition & 2 deletions packages/jest-worker/src/index.ts
Expand Up @@ -15,7 +15,7 @@ import type {
WorkerPoolInterface,
WorkerPoolOptions,
} from './types';
import _messageParent from './workers/messageParent';
export {default as messageParent} from './workers/messageParent';

function getExposedMethods(
workerPath: string,
Expand Down Expand Up @@ -150,4 +150,3 @@ export default class JestWorker {
}

export type {PromiseWithCustomMessage};
export const messageParent = _messageParent;
2 changes: 1 addition & 1 deletion packages/jest-worker/src/workers/messageParent.ts
Expand Up @@ -5,11 +5,11 @@
* LICENSE file in the root directory of this source tree.
*/

import {isMainThread, parentPort} from 'worker_threads';
import {PARENT_MESSAGE_CUSTOM} from '../types';

const isWorkerThread = () => {
try {
const {isMainThread, parentPort} = require('worker_threads');
return !isMainThread && parentPort;
} catch {
return false;
Expand Down

0 comments on commit f8fbd6b

Please sign in to comment.