-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
index.ts
253 lines (228 loc) · 7.78 KB
/
index.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import * as crypto from 'crypto';
import * as path from 'path';
import * as fs from 'graceful-fs';
import slash = require('slash');
import type {AggregatedResult, Test, TestContext} from '@jest/test-result';
import type {Config} from '@jest/types';
import HasteMap from 'jest-haste-map';
const FAIL = 0;
const SUCCESS = 1;
export type TestSequencerOptions = {
contexts: ReadonlyArray<TestContext>;
globalConfig: Config.GlobalConfig;
};
type Cache = {
[key: string]:
| [testStatus: typeof FAIL | typeof SUCCESS, testDuration: number]
| undefined;
};
export type ShardOptions = {
shardIndex: number;
shardCount: number;
};
type ShardPositionOptions = ShardOptions & {
suiteLength: number;
};
/**
* The TestSequencer will ultimately decide which tests should run first.
* It is responsible for storing and reading from a local cache
* map that stores context information for a given test, such as how long it
* took to run during the last run and if it has failed or not.
* Such information is used on:
* TestSequencer.sort(tests: Array<Test>)
* to sort the order of the provided tests.
*
* After the results are collected,
* TestSequencer.cacheResults(tests: Array<Test>, results: AggregatedResult)
* is called to store/update this information on the cache map.
*/
export default class TestSequencer {
private readonly _cache = new Map<TestContext, Cache>();
// eslint-disable-next-line @typescript-eslint/no-empty-function,@typescript-eslint/no-useless-constructor
constructor(_options: TestSequencerOptions) {}
_getCachePath(testContext: TestContext): string {
const {config} = testContext;
const HasteMapClass = HasteMap.getStatic(config);
return HasteMapClass.getCacheFilePath(
config.cacheDirectory,
`perf-cache-${config.id}`,
);
}
_getCache(test: Test): Cache {
const {context} = test;
if (!this._cache.has(context) && context.config.cache) {
const cachePath = this._getCachePath(context);
if (fs.existsSync(cachePath)) {
try {
this._cache.set(
context,
JSON.parse(fs.readFileSync(cachePath, 'utf8')) as Cache,
);
} catch {}
}
}
let cache = this._cache.get(context);
if (!cache) {
cache = {};
this._cache.set(context, cache);
}
return cache;
}
private _shardPosition(options: ShardPositionOptions): number {
const shardRest = options.suiteLength % options.shardCount;
const ratio = options.suiteLength / options.shardCount;
return Array.from({length: options.shardIndex}).reduce<number>(
(acc, _, shardIndex) => {
const dangles = shardIndex < shardRest;
const shardSize = dangles ? Math.ceil(ratio) : Math.floor(ratio);
return acc + shardSize;
},
0,
);
}
/**
* Select tests for shard requested via --shard=shardIndex/shardCount
* Sharding is applied before sorting
*
* @param tests All tests
* @param options shardIndex and shardIndex to select
*
* @example
* ```typescript
* class CustomSequencer extends Sequencer {
* shard(tests, { shardIndex, shardCount }) {
* const shardSize = Math.ceil(tests.length / options.shardCount);
* const shardStart = shardSize * (options.shardIndex - 1);
* const shardEnd = shardSize * options.shardIndex;
* return [...tests]
* .sort((a, b) => (a.path > b.path ? 1 : -1))
* .slice(shardStart, shardEnd);
* }
* }
* ```
*/
shard(
tests: Array<Test>,
options: ShardOptions,
): Array<Test> | Promise<Array<Test>> {
const shardStart = this._shardPosition({
shardCount: options.shardCount,
shardIndex: options.shardIndex - 1,
suiteLength: tests.length,
});
const shardEnd = this._shardPosition({
shardCount: options.shardCount,
shardIndex: options.shardIndex,
suiteLength: tests.length,
});
return tests
.map(test => {
const relativeTestPath = path.posix.relative(
slash(test.context.config.rootDir),
slash(test.path),
);
return {
hash: crypto
.createHash('sha1')
.update(relativeTestPath)
.digest('hex'),
test,
};
})
.sort((a, b) => (a.hash < b.hash ? -1 : a.hash > b.hash ? 1 : 0))
.slice(shardStart, shardEnd)
.map(result => result.test);
}
/**
* Sort test to determine order of execution
* Sorting is applied after sharding
* @param tests
*
* ```typescript
* class CustomSequencer extends Sequencer {
* sort(tests) {
* const copyTests = Array.from(tests);
* return [...tests].sort((a, b) => (a.path > b.path ? 1 : -1));
* }
* }
* ```
*/
sort(tests: Array<Test>): Array<Test> | Promise<Array<Test>> {
/**
* Sorting tests is very important because it has a great impact on the
* user-perceived responsiveness and speed of the test run.
*
* If such information is on cache, tests are sorted based on:
* -> Has it failed during the last run ?
* Since it's important to provide the most expected feedback as quickly
* as possible.
* -> How long it took to run ?
* Because running long tests first is an effort to minimize worker idle
* time at the end of a long test run.
* And if that information is not available they are sorted based on file size
* since big test files usually take longer to complete.
*
* Note that a possible improvement would be to analyse other information
* from the file other than its size.
*
*/
const stats: {[path: string]: number} = {};
const fileSize = ({path, context: {hasteFS}}: Test) =>
stats[path] || (stats[path] = hasteFS.getSize(path) ?? 0);
for (const test of tests) {
test.duration = this.time(test);
}
return tests.sort((testA, testB) => {
const failedA = this.hasFailed(testA);
const failedB = this.hasFailed(testB);
const hasTimeA = testA.duration != null;
const hasTimeB = testB.duration != null;
if (failedA !== failedB) {
return failedA ? -1 : 1;
} else if (hasTimeA !== hasTimeB) {
// If only one of two tests has timing information, run it last
return hasTimeA ? 1 : -1;
} else if (testA.duration != null && testB.duration != null) {
return testA.duration < testB.duration ? 1 : -1;
} else {
return fileSize(testA) < fileSize(testB) ? 1 : -1;
}
});
}
allFailedTests(tests: Array<Test>): Array<Test> | Promise<Array<Test>> {
return this.sort(tests.filter(test => this.hasFailed(test)));
}
cacheResults(tests: Array<Test>, results: AggregatedResult): void {
const map = Object.create(null) as Record<string, Test | undefined>;
for (const test of tests) map[test.path] = test;
for (const testResult of results.testResults) {
const test = map[testResult.testFilePath];
if (test != null && !testResult.skipped) {
const cache = this._getCache(test);
const perf = testResult.perfStats;
const testRuntime =
perf.runtime ?? test.duration ?? perf.end - perf.start;
cache[testResult.testFilePath] = [
testResult.numFailingTests > 0 ? FAIL : SUCCESS,
testRuntime || 0,
];
}
}
for (const [context, cache] of this._cache.entries())
fs.writeFileSync(this._getCachePath(context), JSON.stringify(cache));
}
private hasFailed(test: Test) {
const cache = this._getCache(test);
return cache[test.path]?.[0] === FAIL;
}
private time(test: Test) {
const cache = this._getCache(test);
return cache[test.path]?.[1];
}
}