1 | 'use strict';
|
2 |
|
3 | function _jestUtil() {
|
4 | const data = require('jest-util');
|
5 |
|
6 | _jestUtil = function () {
|
7 | return data;
|
8 | };
|
9 |
|
10 | return data;
|
11 | }
|
12 |
|
13 | var _types = require('../types');
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | let file = null;
|
22 | let setupArgs = [];
|
23 | let initialized = false;
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | const messageListener = request => {
|
39 | switch (request[0]) {
|
40 | case _types.CHILD_MESSAGE_INITIALIZE:
|
41 | const init = request;
|
42 | file = init[2];
|
43 | setupArgs = request[3];
|
44 | break;
|
45 |
|
46 | case _types.CHILD_MESSAGE_CALL:
|
47 | const call = request;
|
48 | execMethod(call[2], call[3]);
|
49 | break;
|
50 |
|
51 | case _types.CHILD_MESSAGE_END:
|
52 | end();
|
53 | break;
|
54 |
|
55 | case _types.CHILD_MESSAGE_MEM_USAGE:
|
56 | reportMemoryUsage();
|
57 | break;
|
58 |
|
59 | default:
|
60 | throw new TypeError(
|
61 | `Unexpected request from parent process: ${request[0]}`
|
62 | );
|
63 | }
|
64 | };
|
65 |
|
66 | process.on('message', messageListener);
|
67 |
|
68 | function reportSuccess(result) {
|
69 | if (!process || !process.send) {
|
70 | throw new Error('Child can only be used on a forked process');
|
71 | }
|
72 |
|
73 | process.send([_types.PARENT_MESSAGE_OK, result]);
|
74 | }
|
75 |
|
76 | function reportClientError(error) {
|
77 | return reportError(error, _types.PARENT_MESSAGE_CLIENT_ERROR);
|
78 | }
|
79 |
|
80 | function reportInitializeError(error) {
|
81 | return reportError(error, _types.PARENT_MESSAGE_SETUP_ERROR);
|
82 | }
|
83 |
|
84 | function reportMemoryUsage() {
|
85 | if (!process || !process.send) {
|
86 | throw new Error('Child can only be used on a forked process');
|
87 | }
|
88 |
|
89 | const msg = [_types.PARENT_MESSAGE_MEM_USAGE, process.memoryUsage().heapUsed];
|
90 | process.send(msg);
|
91 | }
|
92 |
|
93 | function reportError(error, type) {
|
94 | if (!process || !process.send) {
|
95 | throw new Error('Child can only be used on a forked process');
|
96 | }
|
97 |
|
98 | if (error == null) {
|
99 | error = new Error('"null" or "undefined" thrown');
|
100 | }
|
101 |
|
102 | process.send([
|
103 | type,
|
104 | error.constructor && error.constructor.name,
|
105 | error.message,
|
106 | error.stack,
|
107 | typeof error === 'object' ? {...error} : error
|
108 | ]);
|
109 | }
|
110 |
|
111 | function end() {
|
112 | const main = require(file);
|
113 |
|
114 | if (!main.teardown) {
|
115 | exitProcess();
|
116 | return;
|
117 | }
|
118 |
|
119 | execFunction(main.teardown, main, [], exitProcess, exitProcess);
|
120 | }
|
121 |
|
122 | function exitProcess() {
|
123 |
|
124 | process.removeListener('message', messageListener);
|
125 | }
|
126 |
|
127 | function execMethod(method, args) {
|
128 | const main = require(file);
|
129 |
|
130 | let fn;
|
131 |
|
132 | if (method === 'default') {
|
133 | fn = main.__esModule ? main['default'] : main;
|
134 | } else {
|
135 | fn = main[method];
|
136 | }
|
137 |
|
138 | function execHelper() {
|
139 | execFunction(fn, main, args, reportSuccess, reportClientError);
|
140 | }
|
141 |
|
142 | if (initialized || !main.setup) {
|
143 | execHelper();
|
144 | return;
|
145 | }
|
146 |
|
147 | initialized = true;
|
148 | execFunction(main.setup, main, setupArgs, execHelper, reportInitializeError);
|
149 | }
|
150 |
|
151 | function execFunction(fn, ctx, args, onResult, onError) {
|
152 | let result;
|
153 |
|
154 | try {
|
155 | result = fn.apply(ctx, args);
|
156 | } catch (err) {
|
157 | onError(err);
|
158 | return;
|
159 | }
|
160 |
|
161 | if ((0, _jestUtil().isPromise)(result)) {
|
162 | result.then(onResult, onError);
|
163 | } else {
|
164 | onResult(result);
|
165 | }
|
166 | }
|