1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 | function crypto() {
|
8 | const data = _interopRequireWildcard(require('crypto'));
|
9 | crypto = function () {
|
10 | return data;
|
11 | };
|
12 | return data;
|
13 | }
|
14 | function path() {
|
15 | const data = _interopRequireWildcard(require('path'));
|
16 | path = function () {
|
17 | return data;
|
18 | };
|
19 | return data;
|
20 | }
|
21 | function fs() {
|
22 | const data = _interopRequireWildcard(require('graceful-fs'));
|
23 | fs = function () {
|
24 | return data;
|
25 | };
|
26 | return data;
|
27 | }
|
28 | function _slash() {
|
29 | const data = _interopRequireDefault(require('slash'));
|
30 | _slash = function () {
|
31 | return data;
|
32 | };
|
33 | return data;
|
34 | }
|
35 | function _jestHasteMap() {
|
36 | const data = _interopRequireDefault(require('jest-haste-map'));
|
37 | _jestHasteMap = function () {
|
38 | return data;
|
39 | };
|
40 | return data;
|
41 | }
|
42 | function _interopRequireDefault(obj) {
|
43 | return obj && obj.__esModule ? obj : {default: obj};
|
44 | }
|
45 | function _getRequireWildcardCache(nodeInterop) {
|
46 | if (typeof WeakMap !== 'function') return null;
|
47 | var cacheBabelInterop = new WeakMap();
|
48 | var cacheNodeInterop = new WeakMap();
|
49 | return (_getRequireWildcardCache = function (nodeInterop) {
|
50 | return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
51 | })(nodeInterop);
|
52 | }
|
53 | function _interopRequireWildcard(obj, nodeInterop) {
|
54 | if (!nodeInterop && obj && obj.__esModule) {
|
55 | return obj;
|
56 | }
|
57 | if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
|
58 | return {default: obj};
|
59 | }
|
60 | var cache = _getRequireWildcardCache(nodeInterop);
|
61 | if (cache && cache.has(obj)) {
|
62 | return cache.get(obj);
|
63 | }
|
64 | var newObj = {};
|
65 | var hasPropertyDescriptor =
|
66 | Object.defineProperty && Object.getOwnPropertyDescriptor;
|
67 | for (var key in obj) {
|
68 | if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
|
69 | var desc = hasPropertyDescriptor
|
70 | ? Object.getOwnPropertyDescriptor(obj, key)
|
71 | : null;
|
72 | if (desc && (desc.get || desc.set)) {
|
73 | Object.defineProperty(newObj, key, desc);
|
74 | } else {
|
75 | newObj[key] = obj[key];
|
76 | }
|
77 | }
|
78 | }
|
79 | newObj.default = obj;
|
80 | if (cache) {
|
81 | cache.set(obj, newObj);
|
82 | }
|
83 | return newObj;
|
84 | }
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | const FAIL = 0;
|
93 | const SUCCESS = 1;
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | class TestSequencer {
|
108 | _cache = new Map();
|
109 | _getCachePath(testContext) {
|
110 | const {config} = testContext;
|
111 | const HasteMapClass = _jestHasteMap().default.getStatic(config);
|
112 | return HasteMapClass.getCacheFilePath(
|
113 | config.cacheDirectory,
|
114 | `perf-cache-${config.id}`
|
115 | );
|
116 | }
|
117 | _getCache(test) {
|
118 | const {context} = test;
|
119 | if (!this._cache.has(context) && context.config.cache) {
|
120 | const cachePath = this._getCachePath(context);
|
121 | if (fs().existsSync(cachePath)) {
|
122 | try {
|
123 | this._cache.set(
|
124 | context,
|
125 | JSON.parse(fs().readFileSync(cachePath, 'utf8'))
|
126 | );
|
127 | } catch {}
|
128 | }
|
129 | }
|
130 | let cache = this._cache.get(context);
|
131 | if (!cache) {
|
132 | cache = {};
|
133 | this._cache.set(context, cache);
|
134 | }
|
135 | return cache;
|
136 | }
|
137 | _shardPosition(options) {
|
138 | const shardRest = options.suiteLength % options.shardCount;
|
139 | const ratio = options.suiteLength / options.shardCount;
|
140 | return new Array(options.shardIndex)
|
141 | .fill(true)
|
142 | .reduce((acc, _, shardIndex) => {
|
143 | const dangles = shardIndex < shardRest;
|
144 | const shardSize = dangles ? Math.ceil(ratio) : Math.floor(ratio);
|
145 | return acc + shardSize;
|
146 | }, 0);
|
147 | }
|
148 |
|
149 | |
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | shard(tests, options) {
|
171 | const shardStart = this._shardPosition({
|
172 | shardCount: options.shardCount,
|
173 | shardIndex: options.shardIndex - 1,
|
174 | suiteLength: tests.length
|
175 | });
|
176 | const shardEnd = this._shardPosition({
|
177 | shardCount: options.shardCount,
|
178 | shardIndex: options.shardIndex,
|
179 | suiteLength: tests.length
|
180 | });
|
181 | return tests
|
182 | .map(test => {
|
183 | const relativeTestPath = path().posix.relative(
|
184 | (0, _slash().default)(test.context.config.rootDir),
|
185 | (0, _slash().default)(test.path)
|
186 | );
|
187 | return {
|
188 | hash: crypto()
|
189 | .createHash('sha1')
|
190 | .update(relativeTestPath)
|
191 | .digest('hex'),
|
192 | test
|
193 | };
|
194 | })
|
195 | .sort((a, b) => (a.hash < b.hash ? -1 : a.hash > b.hash ? 1 : 0))
|
196 | .slice(shardStart, shardEnd)
|
197 | .map(result => result.test);
|
198 | }
|
199 |
|
200 | |
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 | sort(tests) {
|
215 | |
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 | const stats = {};
|
234 | const fileSize = ({path, context: {hasteFS}}) =>
|
235 | stats[path] || (stats[path] = hasteFS.getSize(path) ?? 0);
|
236 | tests.forEach(test => {
|
237 | test.duration = this.time(test);
|
238 | });
|
239 | return tests.sort((testA, testB) => {
|
240 | const failedA = this.hasFailed(testA);
|
241 | const failedB = this.hasFailed(testB);
|
242 | const hasTimeA = testA.duration != null;
|
243 | if (failedA !== failedB) {
|
244 | return failedA ? -1 : 1;
|
245 | } else if (hasTimeA != (testB.duration != null)) {
|
246 |
|
247 | return hasTimeA ? 1 : -1;
|
248 | } else if (testA.duration != null && testB.duration != null) {
|
249 | return testA.duration < testB.duration ? 1 : -1;
|
250 | } else {
|
251 | return fileSize(testA) < fileSize(testB) ? 1 : -1;
|
252 | }
|
253 | });
|
254 | }
|
255 | allFailedTests(tests) {
|
256 | return this.sort(tests.filter(test => this.hasFailed(test)));
|
257 | }
|
258 | cacheResults(tests, results) {
|
259 | const map = Object.create(null);
|
260 | tests.forEach(test => (map[test.path] = test));
|
261 | results.testResults.forEach(testResult => {
|
262 | const test = map[testResult.testFilePath];
|
263 | if (test != null && !testResult.skipped) {
|
264 | const cache = this._getCache(test);
|
265 | const perf = testResult.perfStats;
|
266 | const testRuntime =
|
267 | perf.runtime ?? test.duration ?? perf.end - perf.start;
|
268 | cache[testResult.testFilePath] = [
|
269 | testResult.numFailingTests > 0 ? FAIL : SUCCESS,
|
270 | testRuntime || 0
|
271 | ];
|
272 | }
|
273 | });
|
274 | this._cache.forEach((cache, context) =>
|
275 | fs().writeFileSync(this._getCachePath(context), JSON.stringify(cache))
|
276 | );
|
277 | }
|
278 | hasFailed(test) {
|
279 | const cache = this._getCache(test);
|
280 | return cache[test.path]?.[0] === FAIL;
|
281 | }
|
282 | time(test) {
|
283 | const cache = this._getCache(test);
|
284 | return cache[test.path]?.[1];
|
285 | }
|
286 | }
|
287 | exports.default = TestSequencer;
|