-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathsubprocess.js
130 lines (109 loc) · 3.42 KB
/
subprocess.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict';
const currentlyUnhandled = require('currently-unhandled')();
/* eslint-disable unicorn/no-process-exit */
/* eslint-disable import/no-unassigned-import */
require('./ensure-forked');
require('./load-chalk');
require('./consume-argv');
require('./fake-tty');
const nowAndTimers = require('../now-and-timers');
const Runner = require('../runner');
const serializeError = require('../serialize-error');
const dependencyTracking = require('./dependency-tracker');
const ipc = require('./ipc');
const options = require('./options').get();
const precompilerHook = require('./precompiler-hook');
function exit(code) {
if (!process.exitCode) {
process.exitCode = code;
}
dependencyTracking.flush();
return ipc.flush().then(() => process.exit());
}
const runner = new Runner({
failFast: options.failFast,
failWithoutAssertions: options.failWithoutAssertions,
file: options.file,
match: options.match,
projectDir: options.projectDir,
runOnlyExclusive: options.runOnlyExclusive,
serial: options.serial,
snapshotDir: options.snapshotDir,
updateSnapshots: options.updateSnapshots
});
ipc.peerFailed.then(() => {
runner.interrupt();
});
const attributedRejections = new Set();
process.on('unhandledRejection', (reason, promise) => {
if (runner.attributeLeakedError(reason)) {
attributedRejections.add(promise);
}
});
runner.on('dependency', dependencyTracking.track);
runner.on('stateChange', state => ipc.send(state));
runner.on('error', err => {
ipc.send({type: 'internal-error', err: serializeError('Internal runner error', false, err)});
exit(1);
});
runner.on('finish', () => {
try {
const touchedFiles = runner.saveSnapshotState();
if (touchedFiles) {
ipc.send({type: 'touched-files', files: touchedFiles});
}
} catch (err) {
ipc.send({type: 'internal-error', err: serializeError('Internal runner error', false, err)});
exit(1);
return;
}
nowAndTimers.setImmediate(() => {
currentlyUnhandled().filter(rejection => {
return !attributedRejections.has(rejection.promise);
}).forEach(rejection => {
ipc.send({type: 'unhandled-rejection', err: serializeError('Unhandled rejection', true, rejection.reason)});
});
exit(0);
});
});
process.on('uncaughtException', err => {
if (runner.attributeLeakedError(err)) {
return;
}
ipc.send({type: 'uncaught-exception', err: serializeError('Uncaught exception', true, err)});
exit(1);
});
let accessedRunner = false;
exports.getRunner = () => {
accessedRunner = true;
return runner;
};
// Store value in case to prevent required modules from modifying it.
const testPath = options.file;
// Install before processing options.require, so if helpers are added to the
// require configuration the *compiled* helper will be loaded.
dependencyTracking.install(testPath);
precompilerHook.install();
try {
(options.require || []).forEach(x => {
const required = require(x);
try {
if (required[Symbol.for('esm\u200D:package')]) {
require = required(module); // eslint-disable-line no-global-assign
}
} catch (_) {}
});
require(testPath);
if (accessedRunner) {
// Unreference the IPC channel if the test file required AVA. This stops it
// from keeping the event loop busy, which means the `beforeExit` event can be
// used to detect when tests stall.
ipc.unref();
} else {
ipc.send({type: 'missing-ava-import'});
exit(1);
}
} catch (err) {
ipc.send({type: 'uncaught-exception', err: serializeError('Uncaught exception', true, err)});
exit(1);
}