Skip to content

Commit

Permalink
chore: run type lint on @jest/test-sequencer (#13413)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Oct 8, 2022
1 parent fb2f161 commit 1ba37a3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 29 deletions.
6 changes: 3 additions & 3 deletions packages/jest-regex-util/src/__tests__/index.test.ts
Expand Up @@ -11,7 +11,7 @@ describe('replacePathSepForRegex()', () => {
describe('posix', () => {
beforeAll(() => {
jest.mock('path', () => ({
...jest.createMockFromModule('path'),
...jest.createMockFromModule<typeof import('path')>('path'),
sep: '/',
}));
jest.isolateModules(() => {
Expand All @@ -21,14 +21,14 @@ describe('replacePathSepForRegex()', () => {

it('should return the path', () => {
const expected = {};
expect(replacePathSepForRegex(expected as any)).toBe(expected);
expect(replacePathSepForRegex(expected as string)).toBe(expected);
});
});

describe('win32', () => {
beforeAll(() => {
jest.mock('path', () => ({
...jest.createMockFromModule('path'),
...jest.createMockFromModule<typeof import('path')>('path'),
sep: '\\',
}));
jest.isolateModules(() => {
Expand Down
32 changes: 22 additions & 10 deletions packages/jest-test-sequencer/src/__tests__/test_sequencer.test.ts
Expand Up @@ -7,12 +7,12 @@

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

jest.mock('graceful-fs', () => ({
...jest.createMockFromModule('fs'),
...jest.createMockFromModule<typeof import('fs')>('fs'),
existsSync: jest.fn(() => true),
readFileSync: jest.fn(() => '{}'),
}));
Expand Down Expand Up @@ -161,7 +161,9 @@ test('writes the cache based on results without existing cache', async () => {
},
],
});
const fileData = JSON.parse(fs.writeFileSync.mock.calls[0][1]);
const fileData = JSON.parse(
fs.writeFileSync.mock.calls[0][1],
) as AggregatedResult;
expect(fileData).toEqual({
'/test-a.js': [SUCCESS, 1],
'/test-c.js': [FAIL, 3],
Expand Down Expand Up @@ -219,7 +221,9 @@ test('writes the cache based on the results', async () => {
},
],
});
const fileData = JSON.parse(fs.writeFileSync.mock.calls[0][1]);
const fileData = JSON.parse(
fs.writeFileSync.mock.calls[0][1],
) as AggregatedResult;
expect(fileData).toEqual({
'/test-a.js': [SUCCESS, 1],
'/test-b.js': [FAIL, 1],
Expand All @@ -228,16 +232,20 @@ test('writes the cache based on the results', async () => {
});

test('works with multiple contexts', async () => {
fs.readFileSync.mockImplementationOnce(cacheName =>
cacheName.startsWith(`${path.sep}cache${path.sep}`)
fs.readFileSync.mockImplementationOnce(cacheName => {
if (typeof cacheName !== 'string') {
throw new Error('Must be called with a string');
}

return cacheName.startsWith(`${path.sep}cache${path.sep}`)
? JSON.stringify({
'/test-a.js': [SUCCESS, 5],
'/test-b.js': [FAIL, 1],
})
: JSON.stringify({
'/test-c.js': [FAIL],
}),
);
});
});

const testPaths = [
{context, duration: null, path: '/test-a.js'},
Expand Down Expand Up @@ -270,12 +278,16 @@ test('works with multiple contexts', async () => {
},
],
});
const fileDataA = JSON.parse(fs.writeFileSync.mock.calls[0][1]);
const fileDataA = JSON.parse(
fs.writeFileSync.mock.calls[0][1],
) as AggregatedResult;
expect(fileDataA).toEqual({
'/test-a.js': [SUCCESS, 1],
'/test-b.js': [FAIL, 1],
});
const fileDataB = JSON.parse(fs.writeFileSync.mock.calls[1][1]);
const fileDataB = JSON.parse(
fs.writeFileSync.mock.calls[1][1],
) as AggregatedResult;
expect(fileDataB).toEqual({
'/test-c.js': [SUCCESS, 3],
});
Expand Down
39 changes: 23 additions & 16 deletions packages/jest-test-sequencer/src/index.ts
Expand Up @@ -16,7 +16,7 @@ const FAIL = 0;
const SUCCESS = 1;

type Cache = {
[key: string]: [0 | 1, number];
[key: string]: [0 | 1, number] | undefined;
};

export type ShardOptions = {
Expand Down Expand Up @@ -57,7 +57,7 @@ export default class TestSequencer {
try {
this._cache.set(
context,
JSON.parse(fs.readFileSync(cachePath, 'utf8')),
JSON.parse(fs.readFileSync(cachePath, 'utf8')) as Cache,
);
} catch {}
}
Expand Down Expand Up @@ -156,21 +156,17 @@ export default class TestSequencer {
*/
const stats: {[path: string]: number} = {};
const fileSize = ({path, context: {hasteFS}}: Test) =>
stats[path] || (stats[path] = hasteFS.getSize(path) || 0);
const hasFailed = (cache: Cache, test: Test) =>
cache[test.path] && cache[test.path][0] === FAIL;
const time = (cache: Cache, test: Test) =>
cache[test.path] && cache[test.path][1];
stats[path] || (stats[path] = hasteFS.getSize(path) ?? 0);

tests.forEach(test => (test.duration = time(this._getCache(test), test)));
tests.forEach(test => {
test.duration = this.time(test);
});
return tests.sort((testA, testB) => {
const cacheA = this._getCache(testA);
const cacheB = this._getCache(testB);
const failedA = hasFailed(cacheA, testA);
const failedB = hasFailed(cacheB, testB);
const failedA = this.hasFailed(testA);
const failedB = this.hasFailed(testB);
const hasTimeA = testA.duration != null;
if (failedA !== failedB) {
return failedA ? -1 : 1;
return failedA === true ? -1 : 1;
} else if (hasTimeA != (testB.duration != null)) {
// If only one of two tests has timing information, run it last
return hasTimeA ? 1 : -1;
Expand All @@ -191,11 +187,12 @@ export default class TestSequencer {
}

cacheResults(tests: Array<Test>, results: AggregatedResult): void {
const map = Object.create(null);
const map = Object.create(null) as Record<string, Test | undefined>;
tests.forEach(test => (map[test.path] = test));
results.testResults.forEach(testResult => {
if (testResult && map[testResult.testFilePath] && !testResult.skipped) {
const cache = this._getCache(map[testResult.testFilePath]);
const test = map[testResult.testFilePath];
if (test != null && !testResult.skipped) {
const cache = this._getCache(test);
const perf = testResult.perfStats;
cache[testResult.testFilePath] = [
testResult.numFailingTests ? FAIL : SUCCESS,
Expand All @@ -208,4 +205,14 @@ export default class TestSequencer {
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];
}
}
1 change: 1 addition & 0 deletions scripts/lintTs.mjs
Expand Up @@ -35,6 +35,7 @@ const packagesToTest = [
'jest-schemas',
'jest-source-map',
'jest-test-result',
'jest-test-sequencer',
'jest-transform',
'jest-types',
'test-globals',
Expand Down

0 comments on commit 1ba37a3

Please sign in to comment.