forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(jest-worker): Remove circular references from messages sent to wo…
…rkers Fixes jestjs#10577
- Loading branch information
Showing
7 changed files
with
112 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
packages/jest-worker/src/workers/__tests__/messageParent.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import { PARENT_MESSAGE_CUSTOM } from '../../types'; | ||
|
||
const processSend = process.send; | ||
|
||
let messageParent; | ||
let mockWorkerThreads; | ||
|
||
beforeEach(() => { | ||
mockWorkerThreads = {}; | ||
process.send = jest.fn(); | ||
jest.mock('worker_threads', () => mockWorkerThreads); | ||
messageParent = require('../messageParent').default; | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetModules(); | ||
// console.log(require('worker_threads')); | ||
process.send = processSend; | ||
}); | ||
|
||
describe("with worker threads", () => { | ||
beforeEach(() => { | ||
mockWorkerThreads.parentPort = { | ||
postMessage: jest.fn() | ||
}; | ||
}); | ||
|
||
it('cand send a message', () => { | ||
messageParent('some-message'); | ||
expect(mockWorkerThreads.parentPort.postMessage).toHaveBeenCalledWith([PARENT_MESSAGE_CUSTOM, 'some-message']); | ||
}); | ||
|
||
it('removes circular references from the message being sent', () => { | ||
const circular = { some: 'thing', ref: null }; | ||
circular.ref = circular; | ||
messageParent(circular); | ||
expect(mockWorkerThreads.parentPort.postMessage).toHaveBeenCalledWith([PARENT_MESSAGE_CUSTOM, { | ||
some: 'thing', | ||
ref: '[Circular]' | ||
}]); | ||
}) | ||
}); | ||
|
||
describe("without worker threads", () => { | ||
it('cand send a message', () => { | ||
messageParent('some-message'); | ||
expect(process.send).toHaveBeenCalledWith([PARENT_MESSAGE_CUSTOM, 'some-message']); | ||
}); | ||
|
||
it('removes circular references from the message being sent', () => { | ||
const circular = { some: 'thing', ref: null }; | ||
circular.ref = circular; | ||
messageParent(circular); | ||
expect(process.send).toHaveBeenCalledWith([PARENT_MESSAGE_CUSTOM, { | ||
some: 'thing', | ||
ref: '[Circular]' | ||
}]); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters