Skip to content

Commit

Permalink
Migrate jest-haste-map to jest-worker (#4812)
Browse files Browse the repository at this point in the history
* Migrate jest-haste-map to jest-worker

* Add feedback

* Change type check to duck type check
  • Loading branch information
mjesun authored and cpojer committed Nov 2, 2017
1 parent 5c22548 commit 065c7b8
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 205 deletions.
4 changes: 2 additions & 2 deletions packages/jest-haste-map/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"fb-watchman": "^2.0.0",
"graceful-fs": "^4.1.11",
"jest-docblock": "^21.2.0",
"jest-worker": "^21.2.1",
"micromatch": "^2.3.11",
"sane": "^2.0.0",
"worker-farm": "^1.5.1"
"sane": "^2.0.0"
}
}
42 changes: 22 additions & 20 deletions packages/jest-haste-map/src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ jest.mock('child_process', () => ({
execSync() {},
}));

jest.mock('worker-farm', () => {
const mock = jest.fn(
(options, worker) =>
(workerFarmMock = jest.fn((data, callback) =>
require(worker)(data, callback),
)),
);
mock.end = jest.fn();
return mock;
jest.mock('jest-worker', () => {
return jest.fn(worker => {
mockWorker = jest.fn((...args) => require(worker).worker(...args));
mockEnd = jest.fn();

return {
end: mockEnd,
worker: mockWorker,
};
});
});

jest.mock('../crawlers/node');
Expand Down Expand Up @@ -98,7 +99,8 @@ let HasteMap;
let mockClocks;
let mockEmitters;
let object;
let workerFarmMock;
let mockEnd;
let mockWorker;
let getCacheFilePath;

describe('HasteMap', () => {
Expand Down Expand Up @@ -659,27 +661,27 @@ describe('HasteMap', () => {
});

it('distributes work across workers', () => {
const workerFarm = require('worker-farm');
const jestWorker = require('jest-worker');
return new HasteMap(
Object.assign({}, defaultConfig, {
maxWorkers: 4,
}),
)
.build()
.then(({__hasteMapForTest: data}) => {
expect(workerFarm.mock.calls.length).toBe(1);
expect(jestWorker.mock.calls.length).toBe(1);

expect(workerFarmMock.mock.calls.length).toBe(5);
expect(mockWorker.mock.calls.length).toBe(5);

expect(workerFarmMock.mock.calls).toEqual([
[{filePath: '/fruits/__mocks__/Pear.js'}, expect.any(Function)],
[{filePath: '/fruits/banana.js'}, expect.any(Function)],
[{filePath: '/fruits/pear.js'}, expect.any(Function)],
[{filePath: '/fruits/strawberry.js'}, expect.any(Function)],
[{filePath: '/vegetables/melon.js'}, expect.any(Function)],
expect(mockWorker.mock.calls).toEqual([
[{filePath: '/fruits/__mocks__/Pear.js'}],
[{filePath: '/fruits/banana.js'}],
[{filePath: '/fruits/pear.js'}],
[{filePath: '/fruits/strawberry.js'}],
[{filePath: '/vegetables/melon.js'}],
]);

expect(workerFarm.end).toBeCalledWith(workerFarmMock);
expect(mockEnd).toBeCalled();
});
});

Expand Down
76 changes: 19 additions & 57 deletions packages/jest-haste-map/src/__tests__/worker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@ import skipOnWindows from '../../../../scripts/skip_on_windows';

import H from '../constants';

const worker = require('../worker');
const {worker} = require('../worker');

let createCallback;
let mockFs;
let moduleData;
let readFileSync;
let workerError;

describe('worker', () => {
skipOnWindows.suite();
Expand Down Expand Up @@ -58,61 +55,32 @@ describe('worker', () => {

throw new Error(`Cannot read path '${path}'.`);
});

moduleData = null;
workerError = null;
createCallback = () =>
jest.fn((error, data) => {
workerError = error;
moduleData = data;
});
});

afterEach(() => {
fs.readFileSync = readFileSync;
});

it('parses JavaScript files and extracts module information', () => {
let callback = createCallback();
worker({filePath: '/fruits/pear.js'}, callback);

// Worker is synchronous. callback must have been called by now
expect(callback).toBeCalled();

expect(workerError).toBe(null);
expect(moduleData).toEqual({
it('parses JavaScript files and extracts module information', async () => {
expect(await worker({filePath: '/fruits/pear.js'})).toEqual({
dependencies: ['Banana', 'Strawberry'],
id: 'Pear',
module: ['/fruits/pear.js', H.MODULE],
});

callback = createCallback();
worker({filePath: '/fruits/strawberry.js'}, callback);

expect(callback).toBeCalled();

expect(workerError).toBe(null);
expect(moduleData).toEqual({
expect(await worker({filePath: '/fruits/strawberry.js'})).toEqual({
dependencies: [],
id: 'Strawberry',
module: ['/fruits/strawberry.js', H.MODULE],
});
});

it('delegates to hasteImplModulePath for getting the id', () => {
const callback = createCallback();
worker(
{
filePath: '/fruits/strawberry.js',
hasteImplModulePath: path.resolve(__dirname, 'haste_impl.js'),
},
callback,
);

// Worker is synchronous. callback must have been called by now
expect(callback).toBeCalled();
it('delegates to hasteImplModulePath for getting the id', async () => {
const moduleData = await worker({
filePath: '/fruits/strawberry.js',
hasteImplModulePath: path.resolve(__dirname, 'haste_impl.js'),
});

expect(workerError).toBe(null);
expect(moduleData.id).toBe('strawberry');
expect(moduleData).toEqual(
expect.objectContaining({
Expand All @@ -123,28 +91,22 @@ describe('worker', () => {
);
});

it('parses package.json files as haste packages', () => {
const callback = createCallback();

worker({filePath: '/package.json'}, callback);
expect(callback).toBeCalled();

expect(workerError).toBe(null);
expect(moduleData).toEqual({
it('parses package.json files as haste packages', async () => {
expect(await worker({filePath: '/package.json'})).toEqual({
dependencies: undefined,
id: 'haste-package',
module: ['/package.json', H.PACKAGE],
});
});

it('returns an error when a file cannot be accessed', () => {
const callback = createCallback();

worker({filePath: '/kiwi.js'}, callback);
it('returns an error when a file cannot be accessed', async () => {
let error = null;
try {
await worker({filePath: '/kiwi.js'});
} catch (err) {
error = err;
}

expect(callback).toBeCalled();
expect(moduleData).toBe(undefined);
expect(workerError.type).toEqual('Error');
expect(workerError.message).toEqual(`Cannot read path '/kiwi.js'.`);
expect(error.message).toEqual(`Cannot read path '/kiwi.js'.`);
});
});
Loading

0 comments on commit 065c7b8

Please sign in to comment.