Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use "export default". Change BSD to MIT
  • Loading branch information
Miguel Jimenez Esun committed Oct 3, 2017
commit 9ee26c64780aebb1eb489a96621bfd7998608672
2 changes: 1 addition & 1 deletion packages/jest-worker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Node module name or absolute path of the file to be loaded in the child processe

List of method names that can be called on the child processes from the parent process. You cannot expose any method named like a public `Worker` method, or starting with `_`. If you use method auto-discovery, then these methods will not be exposed, even if they exist.

#### `numWorkers: number` (required)
#### `numWorkers: number` (optional)

Amount of workers to spwan. Defaults to the number of CPUs minus 1.

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "git",
"url": "https://github.com/facebook/jest.git"
},
"license": "BSD-3-Clause",
"license": "MIT",
"main": "build/index.js",
"dependencies": {
"merge-stream": "^1.0.1"
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-worker/src/__performance_tests__/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// eslint-disable-next-line import/no-extraneous-dependencies
const workerFarm = require('worker-farm');
const JestWorker = require('../../build');
import JestWorker from '../../build';

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
const calls = 10000;
Expand Down
12 changes: 5 additions & 7 deletions packages/jest-worker/src/__tests__/child.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
* Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';
Expand All @@ -14,13 +12,13 @@ const processExit = process.exit;
const processSend = process.send;
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));

const {
import {
CHILD_MESSAGE_INITIALIZE,
CHILD_MESSAGE_CALL,
CHILD_MESSAGE_END,
PARENT_MESSAGE_OK,
PARENT_MESSAGE_ERROR,
} = require('../types');
} from '../types';

let mockCount;

Expand Down
20 changes: 11 additions & 9 deletions packages/jest-worker/src/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
* Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';
Expand All @@ -28,7 +26,7 @@ beforeEach(() => {
// in a global list, so that they can be accessed later. This list is reset in
// every test.
jest.mock('../worker', () => {
return jest.fn(() => {
const fakeClass = jest.fn(() => {
const fakeWorker = {
getStderr: () => ({once() {}, pipe() {}}),
getStdout: () => ({once() {}, pipe() {}}),
Expand All @@ -39,6 +37,11 @@ beforeEach(() => {

return fakeWorker;
});

return {
__esModule: true,
default: fakeClass,
};
});

jest.mock(
Expand All @@ -61,8 +64,8 @@ beforeEach(() => {
{virtual: true},
);

Worker = require('../worker');
Farm = require('../index');
Worker = require('../worker').default;
Farm = require('../index').default;
});

afterEach(() => {
Expand All @@ -89,7 +92,6 @@ it('breaks if any of the forbidden methods is tried to be exposed', () => {
).toThrow();

expect(() => new Farm('/tmp/baz.js', {exposedMethods: ['end']})).toThrow();
expect(() => new Farm('/tmp/baz.js', {exposedMethods: ['_foo']})).toThrow();
});

it('works with minimal options', () => {
Expand Down
16 changes: 7 additions & 9 deletions packages/jest-worker/src/__tests__/worker.test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
* Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

/* eslint-disable no-new */

const {EventEmitter} = require('events');
import {EventEmitter} from 'events';

const {
import {
CHILD_MESSAGE_CALL,
CHILD_MESSAGE_INITIALIZE,
PARENT_MESSAGE_ERROR,
PARENT_MESSAGE_OK,
} = require('../types');
} from '../types';

let Worker;
let forkInterface;
Expand All @@ -36,7 +34,7 @@ beforeEach(() => {
childProcess = require('child_process');
childProcess.fork.mockImplementation(() => forkInterface);

Worker = require('../worker');
Worker = require('../worker').default;
});

afterEach(() => {
Expand Down
10 changes: 5 additions & 5 deletions packages/jest-worker/src/child.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
* Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

'use strict';
Expand Down
47 changes: 21 additions & 26 deletions packages/jest-worker/src/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
* Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

'use strict';

const mergeStream = require('merge-stream');
const os = require('os');
const path = require('path');

const {CHILD_MESSAGE_CALL, CHILD_MESSAGE_END} = require('./types');
const Worker = require('./worker');
import mergeStream from 'merge-stream';
import os from 'os';
import path from 'path';

import type {FarmOptions} from './types';
import type {Readable} from 'stream';

import {CHILD_MESSAGE_CALL, CHILD_MESSAGE_END} from './types';
import Worker from './worker';

/* istanbul ignore next */
const emptyMethod = () => {};

/**
* The Jest farm (publicly called "Worker") is a class that allows you to queue
* methods across multiple child processes, in order to parallelize work. This
Expand All @@ -44,7 +47,7 @@ import type {Readable} from 'stream';
* processed by the same worker. This is specially useful if your workers are
* caching results.
*/
class Farm {
export default class {
_stdout: Readable;
_stderr: Readable;
_ending: boolean;
Expand Down Expand Up @@ -85,7 +88,6 @@ class Farm {
const child = require(workerPath);

exposedMethods = Object.keys(child)
.filter(name => this._isValidMethod(name))
.filter(name => typeof child[name] === 'function');

if (typeof child === 'function') {
Expand All @@ -94,7 +96,11 @@ class Farm {
}

exposedMethods.forEach(name => {
if (!this._isValidMethod(name)) {
if (name.startsWith('_')) {
return;
}

if (this.constructor.prototype.hasOwnProperty(name)) {
throw new TypeError('Cannot define a method called ' + name);
}

Expand Down Expand Up @@ -127,7 +133,7 @@ class Farm {
// We do not cache the request object here. If so, it would only be only
// processed by one of the workers, and we want them all to close.
for (let i = 0; i < workers.length; i++) {
workers[i].send([CHILD_MESSAGE_END, false], this._empty);
workers[i].send([CHILD_MESSAGE_END, false], emptyMethod);
}

this._ending = true;
Expand Down Expand Up @@ -179,15 +185,4 @@ class Farm {
}
});
}

_isValidMethod(name) {
return !Farm.prototype.hasOwnProperty(name) && !name.startsWith('_');
}

/* istanbul ignore next */
_empty() {
// Intentionally empty method.
}
}

module.exports = Farm;
10 changes: 5 additions & 5 deletions packages/jest-worker/src/types.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
* Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

'use strict';
Expand Down
20 changes: 9 additions & 11 deletions packages/jest-worker/src/worker.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
* Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

'use strict';

const childProcess = require('child_process');
import childProcess from 'child_process';

const {
import {
CHILD_MESSAGE_INITIALIZE,
PARENT_MESSAGE_ERROR,
PARENT_MESSAGE_OK,
} = require('./types');
} from './types';

import type {ChildProcess} from 'child_process';
import type {Readable} from 'stream';
Expand Down Expand Up @@ -46,7 +46,7 @@ import type {
* field is changed to "true", so that other workers which might encounter the
* same call skip it.
*/
class Worker {
export default class {
_busy: boolean;
_child: ChildProcess;
_options: WorkerOptions;
Expand Down Expand Up @@ -159,5 +159,3 @@ class Worker {
}
}
}

module.exports = Worker;