/
subprocess.js
145 lines (122 loc) 路 4.05 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
'use strict';
const currentlyUnhandled = require('currently-unhandled')();
/* eslint-disable import/no-unassigned-import */
require('./ensure-forked');
require('./load-chalk');
require('./consume-argv');
/* eslint-enable import/no-unassigned-import */
const ipc = require('./ipc');
ipc.send({type: 'ready-for-options'});
ipc.options.then(options => {
require('./options').set(options);
require('./fake-tty'); // eslint-disable-line import/no-unassigned-import
const nowAndTimers = require('../now-and-timers');
const Runner = require('../runner');
const serializeError = require('../serialize-error');
const dependencyTracking = require('./dependency-tracker');
const precompilerHook = require('./precompiler-hook');
async function exit(code) {
if (!process.exitCode) {
process.exitCode = code;
}
dependencyTracking.flush();
await ipc.flush();
process.exit(); // eslint-disable-line unicorn/no-process-exit
}
const runner = new Runner({
failFast: options.failFast,
failWithoutAssertions: options.failWithoutAssertions,
file: options.file,
match: options.match,
projectDir: options.projectDir,
recordNewSnapshots: options.recordNewSnapshots,
runOnlyExclusive: options.runOnlyExclusive,
serial: options.serial,
snapshotDir: options.snapshotDir,
updateSnapshots: options.updateSnapshots
});
ipc.peerFailed.then(() => { // eslint-disable-line promise/prefer-await-to-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', error => {
ipc.send({type: 'internal-error', err: serializeError('Internal runner error', false, error)});
exit(1);
});
runner.on('finish', () => {
try {
const touchedFiles = runner.saveSnapshotState();
if (touchedFiles) {
ipc.send({type: 'touched-files', files: touchedFiles});
}
} catch (error) {
ipc.send({type: 'internal-error', err: serializeError('Internal runner error', false, error)});
exit(1);
return;
}
nowAndTimers.setImmediate(() => {
currentlyUnhandled()
.filter(rejection => !attributedRejections.has(rejection.promise))
.forEach(rejection => {
ipc.send({type: 'unhandled-rejection', err: serializeError('Unhandled rejection', true, rejection.reason)});
});
exit(0);
});
});
process.on('uncaughtException', error => {
if (runner.attributeLeakedError(error)) {
return;
}
ipc.send({type: 'uncaught-exception', err: serializeError('Uncaught exception', true, error)});
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 {
for (const mod of (options.require || [])) {
const required = require(mod);
try {
if (required[Symbol.for('esm:package')] ||
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 (error) {
ipc.send({type: 'uncaught-exception', err: serializeError('Uncaught exception', true, error)});
exit(1);
}
}).catch(error => {
// There shouldn't be any errors, but if there are we may not have managed
// to bootstrap enough code to serialize them. Re-throw and let the process
// crash.
setImmediate(() => {
throw error;
});
});