Skip to content
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

Make jest-worker use jest-serializer #5613

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/jest-worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"license": "MIT",
"main": "build/index.js",
"dependencies": {
"jest-serializer": "^22.3.0",
"merge-stream": "^1.0.1"
}
}
95 changes: 95 additions & 0 deletions packages/jest-worker/src/__tests__/channel.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* Copyright (c) 2018-present, Facebook, Inc. 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 EventEmitter from 'events';
import Channel from '../channel';
import serializer from 'jest-serializer';

let stream;
let channel;

beforeEach(() => {
stream = Object.assign(new EventEmitter(), {
write: jest.fn(),
});

channel = new Channel(stream);
});

it('sends data prefixed with length', () => {
const data = {
bar: [0, true, '2'],
foo: 42,
};

const buffer = serializer.serialize(data);

channel.send(data);

// Verify that the length is sent first.
expect(stream.write.mock.calls[0]).toEqual([
new Buffer([buffer.length, 0, 0, 0]),
]);

// Then the corresponding serialized message as a buffer.
expect(stream.write.mock.calls[1]).toEqual([buffer]);
});

it('processes a single message split into different buffers', () => {
const data = {
bar: [0, true, '2'],
foo: 42,
};

const message = serializer.serialize(data);
const received = jest.fn();

channel.on('message', received);

// Just received the length.
stream.emit('data', Buffer.from([message.length, 0, 0, 0]));
expect(received).not.toBeCalled();

// Now received half of the buffer.
stream.emit('data', message.slice(0, message.length / 2));
expect(received).not.toBeCalled();

// And now the full buffer.
stream.emit('data', message.slice(message.length / 2));
expect(received).toHaveBeenCalledTimes(1);

expect(received.mock.calls[0][0]).toEqual(data);
});

it('can process multiple messages into a single buffer', () => {
const data1 = {
bar: [0, true, '2'],
foo: 42,
};

const data2 = [null, 'red', {}];

const message1 = serializer.serialize(data1);
const message2 = serializer.serialize(data2);
const message = Buffer.allocUnsafe(8 + message1.length + message2.length);
const received = jest.fn();

message.writeUInt32LE(message1.length, 0);
message1.copy(message, 4);

message.writeUInt32LE(message2.length, 4 + message1.length);
message2.copy(message, 8 + message1.length);

channel.on('message', received);
stream.emit('data', message);

expect(received).toHaveBeenCalledTimes(2);
expect(received.mock.calls[0][0]).toEqual(data1);
expect(received.mock.calls[1][0]).toEqual(data2);
});
Loading