Skip to content

Commit

Permalink
add long test time threshold option
Browse files Browse the repository at this point in the history
  • Loading branch information
Draco-Umbratilis committed Jan 5, 2020
1 parent 9419034 commit 8048c54
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions TestUtils.ts
Expand Up @@ -33,6 +33,7 @@ const DEFAULT_GLOBAL_CONFIG: Config.GlobalConfig = {
lastCommit: false,
listTests: false,
logHeapUsage: false,
longTestThreshold: 5,
maxConcurrency: 5,
maxWorkers: 2,
noSCM: null,
Expand Down
4 changes: 4 additions & 0 deletions docs/CLI.md
Expand Up @@ -218,6 +218,10 @@ Lists all tests as JSON that Jest will run given the arguments, and exits. This

Logs the heap usage after every test. Useful to debug memory leaks. Use together with `--runInBand` and `--expose-gc` in node.

### `--longTestThreshold=<num>`

A number of seconds after which a test is considered as long running and reported as such in the results.

### `--maxConcurrency=<num>`

Prevents Jest from executing more than the specified amount of tests at the same time. Only affects tests that use `test.concurrent`.
Expand Down
6 changes: 6 additions & 0 deletions docs/Configuration.md
Expand Up @@ -438,6 +438,12 @@ _Note: A global teardown module configured in a project (using multi-project run

_Note: The same caveat concerning transformation of `node_modules` as for `globalSetup` applies to `globalTeardown`._

### `longTestThreshold` [number]

Default: `5`

A number of seconds after which a test is considered as long running and reported as such in the results.

### `maxConcurrency` [number]

Default: `5`
Expand Down
1 change: 1 addition & 0 deletions e2e/__tests__/__snapshots__/showConfig.test.ts.snap
Expand Up @@ -101,6 +101,7 @@ exports[`--showConfig outputs config info and exits 1`] = `
"lastCommit": false,
"listTests": false,
"logHeapUsage": false,
"longTestThreshold": 5,
"maxConcurrency": 5,
"maxWorkers": "[maxWorkers]",
"noStackTrace": false,
Expand Down
7 changes: 7 additions & 0 deletions packages/jest-cli/src/cli/args.ts
Expand Up @@ -336,6 +336,13 @@ export const options = {
'node.',
type: 'boolean',
},
longTestThreshold: {
default: undefined,
description:
'A number of seconds after which a test is considered as long running ' +
'and reported as such in the results.',
type: 'number',
},
mapCoverage: {
default: undefined,
description:
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/Defaults.ts
Expand Up @@ -33,6 +33,7 @@ const defaultOptions: Config.DefaultOptions = {
providesModuleNodeModules: [],
throwOnModuleCollision: false,
},
longTestThreshold: 5,
maxConcurrency: 5,
maxWorkers: '50%',
moduleDirectories: ['node_modules'],
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/ValidConfig.ts
Expand Up @@ -63,6 +63,7 @@ const initialOptions: Config.InitialOptions = {
json: false,
lastCommit: false,
logHeapUsage: true,
longTestThreshold: 5,
maxConcurrency: 5,
maxWorkers: '50%',
moduleDirectories: ['node_modules'],
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/index.ts
Expand Up @@ -128,6 +128,7 @@ const groupOptions = (
lastCommit: options.lastCommit,
listTests: options.listTests,
logHeapUsage: options.logHeapUsage,
longTestThreshold: options.longTestThreshold,
maxConcurrency: options.maxConcurrency,
maxWorkers: options.maxWorkers,
noSCM: undefined,
Expand Down
6 changes: 6 additions & 0 deletions packages/jest-config/src/normalize.ts
Expand Up @@ -852,6 +852,7 @@ export default function normalize(
case 'listTests':
case 'logHeapUsage':
case 'maxConcurrency':
case 'longTestThreshold':
case 'mapCoverage':
case 'name':
case 'noStackTrace':
Expand Down Expand Up @@ -978,6 +979,11 @@ export default function normalize(
? 'all'
: 'new';

newOptions.longTestThreshold = parseInt(
(newOptions.longTestThreshold as unknown) as string,
10,
);

newOptions.maxConcurrency = parseInt(
(newOptions.maxConcurrency as unknown) as string,
10,
Expand Down
Expand Up @@ -90,6 +90,7 @@ exports[`prints the config object 1`] = `
"lastCommit": false,
"listTests": false,
"logHeapUsage": false,
"longTestThreshold": 5,
"maxConcurrency": 5,
"maxWorkers": 2,
"noSCM": null,
Expand Down
22 changes: 22 additions & 0 deletions packages/jest-reporters/src/__tests__/get_result_header.test.js
Expand Up @@ -11,7 +11,14 @@ const terminalLink = require('terminal-link');

jest.mock('terminal-link', () => jest.fn(() => 'wannabehyperlink'));

const endTime = 1577717671160;
const testTime = 5500;

const testResult = {
perfStats: {
end: endTime,
start: endTime - testTime,
},
testFilePath: '/foo',
};

Expand All @@ -36,3 +43,18 @@ test('should render the terminal link', () => {

expect(result).toContain('wannabehyperlink');
});

test('should display long running test time', () => {
const result = getResultHeader(testResult, globalConfig);

expect(result).toContain(`${testTime / 1000}s`);
});

test('should not display long running test time since it is not over the custom threshold', () => {
const result = getResultHeader(
testResult,
makeGlobalConfig({longTestThreshold: 6}),
);

expect(result).not.toContain(`${testTime / 1000}s`);
});
2 changes: 1 addition & 1 deletion packages/jest-reporters/src/get_result_header.ts
Expand Up @@ -46,7 +46,7 @@ export default (
: null;

const testDetail = [];
if (runTime !== null && runTime > 5) {
if (runTime !== null && runTime > globalConfig.longTestThreshold) {
testDetail.push(LONG_TEST_COLOR(runTime + 's'));
}

Expand Down
3 changes: 3 additions & 0 deletions packages/jest-types/src/Config.ts
Expand Up @@ -46,6 +46,7 @@ export type DefaultOptions = {
forceCoverageMatch: Array<Glob>;
globals: ConfigGlobals;
haste: HasteConfig;
longTestThreshold: 5;
maxConcurrency: number;
maxWorkers: number | string;
moduleDirectories: Array<string>;
Expand Down Expand Up @@ -136,6 +137,7 @@ export type InitialOptions = Partial<{
logHeapUsage: boolean;
lastCommit: boolean;
listTests: boolean;
longTestThreshold: number;
mapCoverage: boolean;
maxConcurrency: number;
maxWorkers: number | string;
Expand Down Expand Up @@ -259,6 +261,7 @@ export type GlobalConfig = {
globalTeardown?: string;
lastCommit: boolean;
logHeapUsage: boolean;
longTestThreshold: number;
listTests: boolean;
maxConcurrency: number;
maxWorkers: number;
Expand Down

0 comments on commit 8048c54

Please sign in to comment.