Skip to content

Commit cf0d2d6

Browse files
authored
feat: determine if test is slow (#91)
1 parent 3312c2f commit cf0d2d6

File tree

6 files changed

+38
-2
lines changed

6 files changed

+38
-2
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// ⚔️🏃
2+
console.log();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
runner: require.resolve('../../runner'),
3+
testMatch: ['**/__src__/**/*.js'],
4+
slowTestThreshold: -1, // Set this to negative so all tests are slow
5+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Works when it has slow tests 1`] = `
4+
"PASS integrationTests/__fixtures__/slow/__src__/file1.js ()
5+
6+
Test Suites: 1 passed, 1 total
7+
Tests: 1 passed, 1 total
8+
Snapshots: 0 total
9+
Time:
10+
Ran all test suites.
11+
"
12+
`;

integrationTests/slow.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const runJest = require('./runJest');
2+
3+
it('Works when it has slow tests', () =>
4+
expect(runJest('slow')).resolves.toMatchSnapshot());

lib/createJestRunner.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
import type { Config } from '@jest/types';
2+
import type { TestResult } from '@jest/test-result';
23
import type * as JestRunner from 'jest-runner';
34
import { Worker } from 'jest-worker';
45
import throat from 'throat';
56
import type { CreateRunnerOptions, Path, TestRunner } from './types';
67

8+
function determineSlowTestResult(
9+
test: JestRunner.Test,
10+
result: TestResult,
11+
): TestResult {
12+
// See: https://github.com/facebook/jest/blob/acd7c83c8365140f4ecf44a456ff7366ffa31fa2/packages/jest-runner/src/runTest.ts#L287
13+
if (result.perfStats.runtime / 1000 > test.context.config.slowTestThreshold) {
14+
return { ...result, perfStats: { ...result.perfStats, slow: true } };
15+
}
16+
return result;
17+
}
18+
719
class CancelRun extends Error {
820
constructor(message?: string) {
921
super(message);
@@ -89,6 +101,7 @@ export default function createRunner<
89101
return runner(baseOptions);
90102
});
91103
})
104+
.then(result => determineSlowTestResult(test, result))
92105
.then(result => onResult(test, result))
93106
.catch(err => onFailure(test, err)),
94107
),
@@ -146,6 +159,7 @@ export default function createRunner<
146159
const runAllTests = Promise.all(
147160
tests.map(test =>
148161
runTestInWorker(test)
162+
.then(result => determineSlowTestResult(test, result))
149163
.then(testResult => onResult(test, testResult))
150164
.catch(error => onFailure(test, error)),
151165
),

lib/toTestResult.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ function getPerfStats({ stats }: Options): TestResult['perfStats'] {
2525
const start = new Date(stats.start).getTime();
2626
const end = new Date(stats.end).getTime();
2727
const runtime = end - start;
28-
// TODO: determine `slow` by using `runtime / 1000 > config.slowTestThreshold`
29-
// See: https://github.com/facebook/jest/blob/acd7c83c8365140f4ecf44a456ff7366ffa31fa2/packages/jest-runner/src/runTest.ts#L287
28+
// Note: this flag is set in 'lib/createJestRunner.ts'
3029
const slow = false;
3130
return { start, end, runtime, slow };
3231
}

0 commit comments

Comments
 (0)