Skip to content

Commit

Permalink
feat: determine if test is slow (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
lokshunhung authored Mar 8, 2022
1 parent 3312c2f commit cf0d2d6
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 2 deletions.
2 changes: 2 additions & 0 deletions integrationTests/__fixtures__/slow/__src__/file1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// ⚔️🏃
console.log();
5 changes: 5 additions & 0 deletions integrationTests/__fixtures__/slow/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
runner: require.resolve('../../runner'),
testMatch: ['**/__src__/**/*.js'],
slowTestThreshold: -1, // Set this to negative so all tests are slow
};
12 changes: 12 additions & 0 deletions integrationTests/__snapshots__/slow.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Works when it has slow tests 1`] = `
"PASS integrationTests/__fixtures__/slow/__src__/file1.js ()
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time:
Ran all test suites.
"
`;
4 changes: 4 additions & 0 deletions integrationTests/slow.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const runJest = require('./runJest');

it('Works when it has slow tests', () =>
expect(runJest('slow')).resolves.toMatchSnapshot());
14 changes: 14 additions & 0 deletions lib/createJestRunner.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import type { Config } from '@jest/types';
import type { TestResult } from '@jest/test-result';
import type * as JestRunner from 'jest-runner';
import { Worker } from 'jest-worker';
import throat from 'throat';
import type { CreateRunnerOptions, Path, TestRunner } from './types';

function determineSlowTestResult(
test: JestRunner.Test,
result: TestResult,
): TestResult {
// See: https://github.com/facebook/jest/blob/acd7c83c8365140f4ecf44a456ff7366ffa31fa2/packages/jest-runner/src/runTest.ts#L287
if (result.perfStats.runtime / 1000 > test.context.config.slowTestThreshold) {
return { ...result, perfStats: { ...result.perfStats, slow: true } };
}
return result;
}

class CancelRun extends Error {
constructor(message?: string) {
super(message);
Expand Down Expand Up @@ -89,6 +101,7 @@ export default function createRunner<
return runner(baseOptions);
});
})
.then(result => determineSlowTestResult(test, result))
.then(result => onResult(test, result))
.catch(err => onFailure(test, err)),
),
Expand Down Expand Up @@ -146,6 +159,7 @@ export default function createRunner<
const runAllTests = Promise.all(
tests.map(test =>
runTestInWorker(test)
.then(result => determineSlowTestResult(test, result))
.then(testResult => onResult(test, testResult))
.catch(error => onFailure(test, error)),
),
Expand Down
3 changes: 1 addition & 2 deletions lib/toTestResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ function getPerfStats({ stats }: Options): TestResult['perfStats'] {
const start = new Date(stats.start).getTime();
const end = new Date(stats.end).getTime();
const runtime = end - start;
// TODO: determine `slow` by using `runtime / 1000 > config.slowTestThreshold`
// See: https://github.com/facebook/jest/blob/acd7c83c8365140f4ecf44a456ff7366ffa31fa2/packages/jest-runner/src/runTest.ts#L287
// Note: this flag is set in 'lib/createJestRunner.ts'
const slow = false;
return { start, end, runtime, slow };
}
Expand Down

0 comments on commit cf0d2d6

Please sign in to comment.