Skip to content

Commit 1ba37a3

Browse files
authored
chore: run type lint on @jest/test-sequencer (#13413)
1 parent fb2f161 commit 1ba37a3

File tree

4 files changed

+49
-29
lines changed

4 files changed

+49
-29
lines changed

packages/jest-regex-util/src/__tests__/index.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('replacePathSepForRegex()', () => {
1111
describe('posix', () => {
1212
beforeAll(() => {
1313
jest.mock('path', () => ({
14-
...jest.createMockFromModule('path'),
14+
...jest.createMockFromModule<typeof import('path')>('path'),
1515
sep: '/',
1616
}));
1717
jest.isolateModules(() => {
@@ -21,14 +21,14 @@ describe('replacePathSepForRegex()', () => {
2121

2222
it('should return the path', () => {
2323
const expected = {};
24-
expect(replacePathSepForRegex(expected as any)).toBe(expected);
24+
expect(replacePathSepForRegex(expected as string)).toBe(expected);
2525
});
2626
});
2727

2828
describe('win32', () => {
2929
beforeAll(() => {
3030
jest.mock('path', () => ({
31-
...jest.createMockFromModule('path'),
31+
...jest.createMockFromModule<typeof import('path')>('path'),
3232
sep: '\\',
3333
}));
3434
jest.isolateModules(() => {

packages/jest-test-sequencer/src/__tests__/test_sequencer.test.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
import * as path from 'path';
99
import * as mockedFs from 'graceful-fs';
10-
import type {Test, TestContext} from '@jest/test-result';
10+
import type {AggregatedResult, Test, TestContext} from '@jest/test-result';
1111
import {makeProjectConfig} from '@jest/test-utils';
1212
import TestSequencer from '../index';
1313

1414
jest.mock('graceful-fs', () => ({
15-
...jest.createMockFromModule('fs'),
15+
...jest.createMockFromModule<typeof import('fs')>('fs'),
1616
existsSync: jest.fn(() => true),
1717
readFileSync: jest.fn(() => '{}'),
1818
}));
@@ -161,7 +161,9 @@ test('writes the cache based on results without existing cache', async () => {
161161
},
162162
],
163163
});
164-
const fileData = JSON.parse(fs.writeFileSync.mock.calls[0][1]);
164+
const fileData = JSON.parse(
165+
fs.writeFileSync.mock.calls[0][1],
166+
) as AggregatedResult;
165167
expect(fileData).toEqual({
166168
'/test-a.js': [SUCCESS, 1],
167169
'/test-c.js': [FAIL, 3],
@@ -219,7 +221,9 @@ test('writes the cache based on the results', async () => {
219221
},
220222
],
221223
});
222-
const fileData = JSON.parse(fs.writeFileSync.mock.calls[0][1]);
224+
const fileData = JSON.parse(
225+
fs.writeFileSync.mock.calls[0][1],
226+
) as AggregatedResult;
223227
expect(fileData).toEqual({
224228
'/test-a.js': [SUCCESS, 1],
225229
'/test-b.js': [FAIL, 1],
@@ -228,16 +232,20 @@ test('writes the cache based on the results', async () => {
228232
});
229233

230234
test('works with multiple contexts', async () => {
231-
fs.readFileSync.mockImplementationOnce(cacheName =>
232-
cacheName.startsWith(`${path.sep}cache${path.sep}`)
235+
fs.readFileSync.mockImplementationOnce(cacheName => {
236+
if (typeof cacheName !== 'string') {
237+
throw new Error('Must be called with a string');
238+
}
239+
240+
return cacheName.startsWith(`${path.sep}cache${path.sep}`)
233241
? JSON.stringify({
234242
'/test-a.js': [SUCCESS, 5],
235243
'/test-b.js': [FAIL, 1],
236244
})
237245
: JSON.stringify({
238246
'/test-c.js': [FAIL],
239-
}),
240-
);
247+
});
248+
});
241249

242250
const testPaths = [
243251
{context, duration: null, path: '/test-a.js'},
@@ -270,12 +278,16 @@ test('works with multiple contexts', async () => {
270278
},
271279
],
272280
});
273-
const fileDataA = JSON.parse(fs.writeFileSync.mock.calls[0][1]);
281+
const fileDataA = JSON.parse(
282+
fs.writeFileSync.mock.calls[0][1],
283+
) as AggregatedResult;
274284
expect(fileDataA).toEqual({
275285
'/test-a.js': [SUCCESS, 1],
276286
'/test-b.js': [FAIL, 1],
277287
});
278-
const fileDataB = JSON.parse(fs.writeFileSync.mock.calls[1][1]);
288+
const fileDataB = JSON.parse(
289+
fs.writeFileSync.mock.calls[1][1],
290+
) as AggregatedResult;
279291
expect(fileDataB).toEqual({
280292
'/test-c.js': [SUCCESS, 3],
281293
});

packages/jest-test-sequencer/src/index.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const FAIL = 0;
1616
const SUCCESS = 1;
1717

1818
type Cache = {
19-
[key: string]: [0 | 1, number];
19+
[key: string]: [0 | 1, number] | undefined;
2020
};
2121

2222
export type ShardOptions = {
@@ -57,7 +57,7 @@ export default class TestSequencer {
5757
try {
5858
this._cache.set(
5959
context,
60-
JSON.parse(fs.readFileSync(cachePath, 'utf8')),
60+
JSON.parse(fs.readFileSync(cachePath, 'utf8')) as Cache,
6161
);
6262
} catch {}
6363
}
@@ -156,21 +156,17 @@ export default class TestSequencer {
156156
*/
157157
const stats: {[path: string]: number} = {};
158158
const fileSize = ({path, context: {hasteFS}}: Test) =>
159-
stats[path] || (stats[path] = hasteFS.getSize(path) || 0);
160-
const hasFailed = (cache: Cache, test: Test) =>
161-
cache[test.path] && cache[test.path][0] === FAIL;
162-
const time = (cache: Cache, test: Test) =>
163-
cache[test.path] && cache[test.path][1];
159+
stats[path] || (stats[path] = hasteFS.getSize(path) ?? 0);
164160

165-
tests.forEach(test => (test.duration = time(this._getCache(test), test)));
161+
tests.forEach(test => {
162+
test.duration = this.time(test);
163+
});
166164
return tests.sort((testA, testB) => {
167-
const cacheA = this._getCache(testA);
168-
const cacheB = this._getCache(testB);
169-
const failedA = hasFailed(cacheA, testA);
170-
const failedB = hasFailed(cacheB, testB);
165+
const failedA = this.hasFailed(testA);
166+
const failedB = this.hasFailed(testB);
171167
const hasTimeA = testA.duration != null;
172168
if (failedA !== failedB) {
173-
return failedA ? -1 : 1;
169+
return failedA === true ? -1 : 1;
174170
} else if (hasTimeA != (testB.duration != null)) {
175171
// If only one of two tests has timing information, run it last
176172
return hasTimeA ? 1 : -1;
@@ -191,11 +187,12 @@ export default class TestSequencer {
191187
}
192188

193189
cacheResults(tests: Array<Test>, results: AggregatedResult): void {
194-
const map = Object.create(null);
190+
const map = Object.create(null) as Record<string, Test | undefined>;
195191
tests.forEach(test => (map[test.path] = test));
196192
results.testResults.forEach(testResult => {
197-
if (testResult && map[testResult.testFilePath] && !testResult.skipped) {
198-
const cache = this._getCache(map[testResult.testFilePath]);
193+
const test = map[testResult.testFilePath];
194+
if (test != null && !testResult.skipped) {
195+
const cache = this._getCache(test);
199196
const perf = testResult.perfStats;
200197
cache[testResult.testFilePath] = [
201198
testResult.numFailingTests ? FAIL : SUCCESS,
@@ -208,4 +205,14 @@ export default class TestSequencer {
208205
fs.writeFileSync(this._getCachePath(context), JSON.stringify(cache)),
209206
);
210207
}
208+
209+
private hasFailed(test: Test) {
210+
const cache = this._getCache(test);
211+
return cache[test.path]?.[0] === FAIL;
212+
}
213+
214+
private time(test: Test) {
215+
const cache = this._getCache(test);
216+
return cache[test.path]?.[1];
217+
}
211218
}

scripts/lintTs.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const packagesToTest = [
3535
'jest-schemas',
3636
'jest-source-map',
3737
'jest-test-result',
38+
'jest-test-sequencer',
3839
'jest-transform',
3940
'jest-types',
4041
'test-globals',

0 commit comments

Comments
 (0)