/
specs-runner.ts
207 lines (200 loc) · 7.69 KB
/
specs-runner.ts
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import R from 'ramda';
import * as path from 'path';
import execa from 'execa';
import pEvent from 'p-event';
import { deserializeError } from 'serialize-error';
import { ForkLevel } from '../api/consumer/lib/test';
import { TESTS_FORK_LEVEL } from '../constants';
import { Analytics } from '../analytics/analytics';
import logger from '../logger/logger';
import ExternalErrors from '../error/external-errors';
import ExternalBuildErrors from '../consumer/component/exceptions/external-build-errors';
import ExternalTestErrors from '../consumer/component/exceptions/external-test-errors';
import { SpecsResultsWithMetaData, Results } from '../consumer/specs-results/specs-results';
import { BitId } from '../bit-id';
export type Tester = {
run: (filePath: string) => Promise<Results>;
globals: Record<string, any>;
modules: Record<string, any>;
};
export default (async function run({
ids,
forkLevel,
includeUnmodified = false,
verbose
}: {
ids: string[] | null | undefined;
forkLevel: ForkLevel;
includeUnmodified: boolean;
verbose: boolean | null | undefined;
}): Promise<SpecsResultsWithMetaData | null | undefined> {
if (!ids || R.isEmpty(ids)) {
Analytics.addBreadCrumb('specs-runner.run', 'running tests on one child process without ids');
logger.debug('specs-runner.run, running tests on one child process without ids');
return runOnChildProcess({
includeUnmodified,
verbose
});
}
if (forkLevel === TESTS_FORK_LEVEL.ONE) {
Analytics.addBreadCrumb('specs-runner.run', 'running tests on one child process with ids');
logger.debug('specs-runner.run, running tests on one child process with ids');
return runOnChildProcess({
ids,
includeUnmodified: false, // no meaning to pass this when there is specific ids
verbose
});
}
Analytics.addBreadCrumb('specs-runner.run', 'running tests on child process for each component');
logger.debug('specs-runner.run, running tests on child process for each component');
const allRunnersP = ids.map(id =>
runOnChildProcess({
ids: [id],
includeUnmodified: false, // no meaning to pass this when there is specific ids
verbose
})
);
const allRunnersResults = await Promise.all(allRunnersP);
if (!allRunnersResults || !allRunnersResults.length) return undefined;
const finalResults = allRunnersResults.reduce(
(acc, curr) => {
// if (!curr || !curr[0]) return acc;
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
if (curr.childOutput) {
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
acc.childOutput = `${acc.childOutput}\n${curr.childOutput}`;
}
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
if (curr.results && curr.results[0]) {
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
acc.results.push(curr.results[0]);
}
return acc;
},
{ type: 'results', childOutput: '', results: [] }
);
return finalResults;
});
async function runOnChildProcess({
ids,
includeUnmodified,
verbose
}: {
ids?: string[] | null | undefined;
includeUnmodified: boolean | null | undefined;
verbose: boolean | null | undefined;
}): Promise<SpecsResultsWithMetaData | null | undefined> {
// Check if we run from npm or from binary (pkg)
let args: string[] = [];
if (ids) {
args = args.concat(ids);
}
if (verbose) {
args.push('--verbose');
}
if (includeUnmodified) {
args.push('--all');
}
const baseEnv: Record<string, any> = {
__verbose__: verbose,
__includeUnmodified__: includeUnmodified
};
// Don't use ternary condition since if we put it as undefined
// It will pass to the fork as "undefined" (string) instad of not passing it at all
// __ids__: ids ? ids.join() : undefined,
if (ids) {
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
baseEnv.__ids__ = ids.join();
}
// Merge process.env from the main process
const env = Object.assign({}, process.env, baseEnv);
const workerPath = path.join(__dirname, 'worker.js');
// if (process.pkg) {
// const entryPoint = process.argv[1];
// workerPath = path.join(entryPoint, '../../dist/specs-runner/worker.js');
// }
const child = execa.node(workerPath, args, { env, nodeOptions: ['--no-warnings'] });
const result = await pEvent(child, 'message');
const childResult = await child;
if (!result) {
return null;
}
const deserializedResults = deserializeResults(result);
if (!deserializedResults) return null;
if (childResult.all) {
deserializedResults.childOutput = childResult.all;
}
if (deserializedResults.type === 'error') {
if (deserializedResults.error instanceof Error) {
throw deserializedResults.error;
}
throw new Error(deserializedResults.error);
}
return deserializedResults;
}
function deserializeResults(results): SpecsResultsWithMetaData | null | undefined {
if (!results) return undefined;
if (results.type === 'error') {
let deserializedError = deserializeError(results.error);
// Special desrialization for external errors
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
if (deserializedError.originalErrors) {
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
const deserializedOriginalErrors = deserializedError.originalErrors.map(deserializeError);
if (results.error.name === ExternalBuildErrors.name) {
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
deserializedError = new ExternalBuildErrors(deserializedError.id, deserializedOriginalErrors);
} else if (results.error.name === ExternalTestErrors.name) {
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
deserializedError = new ExternalTestErrors(deserializedError.id, deserializedOriginalErrors);
} else {
deserializedError = new ExternalErrors(deserializedOriginalErrors);
}
}
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
if (deserializedError.originalError) {
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
const deserializedOriginalError = deserializeError(deserializedError.originalError);
const compName =
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
deserializedError.compName && typeof deserializedError.compName === 'string' ? deserializedError.compName : '';
deserializedError = new ExternalTestErrors(compName, [deserializedOriginalError]);
}
const finalResults = {
type: 'error',
error: deserializedError
};
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
return finalResults;
}
const deserializeFailure = failure => {
if (!failure) return undefined;
const deserializedFailure = failure;
if (failure.err) {
try {
deserializedFailure.err = deserializeError(failure.err);
} catch (e) {
logger.debug(`fail parsing error ${deserializedFailure.err}`);
}
}
return deserializedFailure;
};
const deserializeResult = result => {
result.componentId = new BitId(result.componentId); // when BitId is received from a fork it loses its class and appears as an object
if (!result.failures) return result;
result.failures = result.failures.map(deserializeFailure);
return result;
};
const deserializedResults = results.results.map(deserializeResult);
return {
type: 'results',
results: deserializedResults
};
}