jest-worker
Advanced tools
Comparing version 23.2.0 to 24.0.0-alpha.0
@@ -19,2 +19,4 @@ /** | ||
let file = null; | ||
let setupArgs = []; | ||
let initialized = false; | ||
@@ -38,2 +40,3 @@ /** | ||
file = request[2]; | ||
setupArgs = request[3]; | ||
break; | ||
@@ -46,3 +49,3 @@ | ||
case (_types || _load_types()).CHILD_MESSAGE_END: | ||
process.exit(0); | ||
end(); | ||
break; | ||
@@ -65,3 +68,17 @@ | ||
function reportError(error) { | ||
function reportClientError(error) { | ||
return reportError( | ||
error, | ||
(_types || _load_types()).PARENT_MESSAGE_CLIENT_ERROR | ||
); | ||
} | ||
function reportInitializeError(error) { | ||
return reportError( | ||
error, | ||
(_types || _load_types()).PARENT_MESSAGE_SETUP_ERROR | ||
); | ||
} | ||
function reportError(error, type) { | ||
if (!process || !process.send) { | ||
@@ -76,3 +93,3 @@ throw new Error('Child can only be used on a forked process'); | ||
process.send([ | ||
(_types || _load_types()).PARENT_MESSAGE_ERROR, | ||
type, | ||
error.constructor && error.constructor.name, | ||
@@ -86,15 +103,54 @@ error.message, | ||
function end() { | ||
// $FlowFixMe: This has to be a dynamic require. | ||
const main = require(file); | ||
if (!main.teardown) { | ||
exitProcess(); | ||
return; | ||
} | ||
execFunction(main.teardown, main, [], exitProcess, exitProcess); | ||
} | ||
function exitProcess() { | ||
process.exit(0); | ||
} | ||
function execMethod(method, args) { | ||
// $FlowFixMe: This has to be a dynamic require. | ||
const main = require(file); | ||
let fn; | ||
if (method === 'default') { | ||
fn = main.__esModule ? main['default'] : main; | ||
} else { | ||
fn = main[method]; | ||
} | ||
function execHelper() { | ||
execFunction(fn, main, args, reportSuccess, reportClientError); | ||
} | ||
if (initialized || !main.setup) { | ||
execHelper(); | ||
return; | ||
} | ||
initialized = true; | ||
execFunction(main.setup, main, setupArgs, execHelper, reportInitializeError); | ||
} | ||
function execFunction(fn, ctx, args, onResult, onError) { | ||
let result; | ||
try { | ||
if (method === 'default') { | ||
result = (main.__esModule ? main['default'] : main).apply(global, args); | ||
} else { | ||
result = main[method].apply(main, args); | ||
} | ||
result = fn.apply(ctx, args); | ||
} catch (err) { | ||
reportError(err); | ||
onError(err); | ||
return; | ||
@@ -104,6 +160,6 @@ } | ||
if (result && typeof result.then === 'function') { | ||
result.then(reportSuccess, reportError); | ||
result.then(onResult, onError); | ||
} else { | ||
reportSuccess(result); | ||
onResult(result); | ||
} | ||
} |
@@ -84,3 +84,4 @@ /** | ||
const numWorkers = | ||
options.numWorkers || (_os || _load_os()).default.cpus().length - 1; | ||
options.numWorkers || | ||
Math.max((_os || _load_os()).default.cpus().length - 1, 1); | ||
const workers = new Array(numWorkers); | ||
@@ -97,3 +98,4 @@ const stdout = (0, (_mergeStream || _load_mergeStream()).default)(); | ||
maxRetries: options.maxRetries || 3, | ||
workerPath | ||
setupArgs: options.setupArgs || [], | ||
workerPath: workerPath | ||
}; | ||
@@ -100,0 +102,0 @@ |
@@ -26,3 +26,4 @@ /** | ||
const PARENT_MESSAGE_OK = (exports.PARENT_MESSAGE_OK = 0); | ||
const PARENT_MESSAGE_ERROR = (exports.PARENT_MESSAGE_ERROR = 1); | ||
const PARENT_MESSAGE_CLIENT_ERROR = (exports.PARENT_MESSAGE_CLIENT_ERROR = 1); | ||
const PARENT_MESSAGE_SETUP_ERROR = (exports.PARENT_MESSAGE_SETUP_ERROR = 2); | ||
@@ -29,0 +30,0 @@ // Option objects. |
@@ -67,3 +67,8 @@ /** | ||
send(request, onProcessStart, onProcessEnd) { | ||
const item = {next: null, onProcessEnd, onProcessStart, request}; | ||
const item = { | ||
next: null, | ||
onProcessEnd: onProcessEnd, | ||
onProcessStart: onProcessStart, | ||
request: request | ||
}; | ||
@@ -105,3 +110,4 @@ if (this._last) { | ||
false, | ||
this._options.workerPath | ||
this._options.workerPath, | ||
this._options.setupArgs | ||
]); | ||
@@ -120,3 +126,3 @@ | ||
this._receive([ | ||
(_types || _load_types()).PARENT_MESSAGE_ERROR, | ||
(_types || _load_types()).PARENT_MESSAGE_CLIENT_ERROR, | ||
error.name, | ||
@@ -175,2 +181,4 @@ error.message, | ||
let error; | ||
switch (response[0]) { | ||
@@ -181,4 +189,4 @@ case (_types || _load_types()).PARENT_MESSAGE_OK: | ||
case (_types || _load_types()).PARENT_MESSAGE_ERROR: | ||
let error = response[4]; | ||
case (_types || _load_types()).PARENT_MESSAGE_CLIENT_ERROR: | ||
error = response[4]; | ||
@@ -204,2 +212,12 @@ if (error != null && typeof error === 'object') { | ||
case (_types || _load_types()).PARENT_MESSAGE_SETUP_ERROR: | ||
error = new Error('Error when calling setup: ' + response[2]); | ||
// $FlowFixMe: adding custom properties to errors. | ||
error.type = response[1]; | ||
error.stack = response[3]; | ||
onProcessEnd(error, null); | ||
break; | ||
default: | ||
@@ -206,0 +224,0 @@ throw new TypeError('Unexpected response from worker: ' + response[0]); |
{ | ||
"name": "jest-worker", | ||
"version": "23.2.0", | ||
"version": "24.0.0-alpha.0", | ||
"repository": { | ||
@@ -12,3 +12,4 @@ "type": "git", | ||
"merge-stream": "^1.0.1" | ||
} | ||
}, | ||
"gitHead": "22f67d49ffcce7a5b6d6891438b837b3b26ba9db" | ||
} |
@@ -76,2 +76,6 @@ # jest-worker | ||
#### `setupArgs: Array<mixed>` (optional) | ||
The arguments that will be passed to the `setup` method during initialization. | ||
## Worker | ||
@@ -95,2 +99,9 @@ | ||
## Setting up and tearing down the child process | ||
The child process can define two special methods (both of them can be asynchronous): | ||
- `setup()`: If defined, it's executed before the first call to any method in the child. | ||
- `teardown()`: If defined, it's executed when the farm ends. | ||
# More examples | ||
@@ -97,0 +108,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26497
7
545
204
2
5