From 8048c543cd0a10bb346a7bc3c6afaa9dbb052114 Mon Sep 17 00:00:00 2001 From: Draco-Umbratilis Date: Mon, 30 Dec 2019 17:56:18 +0100 Subject: [PATCH] add long test time threshold option --- TestUtils.ts | 1 + docs/CLI.md | 4 ++++ docs/Configuration.md | 6 +++++ .../__snapshots__/showConfig.test.ts.snap | 1 + packages/jest-cli/src/cli/args.ts | 7 ++++++ packages/jest-config/src/Defaults.ts | 1 + packages/jest-config/src/ValidConfig.ts | 1 + packages/jest-config/src/index.ts | 1 + packages/jest-config/src/normalize.ts | 6 +++++ .../log_debug_messages.test.ts.snap | 1 + .../src/__tests__/get_result_header.test.js | 22 +++++++++++++++++++ .../jest-reporters/src/get_result_header.ts | 2 +- packages/jest-types/src/Config.ts | 3 +++ 13 files changed, 55 insertions(+), 1 deletion(-) diff --git a/TestUtils.ts b/TestUtils.ts index 1398da6b1ba3..3d0edcb98fb7 100644 --- a/TestUtils.ts +++ b/TestUtils.ts @@ -33,6 +33,7 @@ const DEFAULT_GLOBAL_CONFIG: Config.GlobalConfig = { lastCommit: false, listTests: false, logHeapUsage: false, + longTestThreshold: 5, maxConcurrency: 5, maxWorkers: 2, noSCM: null, diff --git a/docs/CLI.md b/docs/CLI.md index eab4a69e7025..ac09d785c740 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -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=` + +A number of seconds after which a test is considered as long running and reported as such in the results. + ### `--maxConcurrency=` Prevents Jest from executing more than the specified amount of tests at the same time. Only affects tests that use `test.concurrent`. diff --git a/docs/Configuration.md b/docs/Configuration.md index 50e03f28b87a..649b2c1fca3a 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -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` diff --git a/e2e/__tests__/__snapshots__/showConfig.test.ts.snap b/e2e/__tests__/__snapshots__/showConfig.test.ts.snap index 7f3d73efee7c..1cc7adef7674 100644 --- a/e2e/__tests__/__snapshots__/showConfig.test.ts.snap +++ b/e2e/__tests__/__snapshots__/showConfig.test.ts.snap @@ -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, diff --git a/packages/jest-cli/src/cli/args.ts b/packages/jest-cli/src/cli/args.ts index a0d3f191b722..d18639616028 100644 --- a/packages/jest-cli/src/cli/args.ts +++ b/packages/jest-cli/src/cli/args.ts @@ -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: diff --git a/packages/jest-config/src/Defaults.ts b/packages/jest-config/src/Defaults.ts index af912c569d2c..6cead803a108 100644 --- a/packages/jest-config/src/Defaults.ts +++ b/packages/jest-config/src/Defaults.ts @@ -33,6 +33,7 @@ const defaultOptions: Config.DefaultOptions = { providesModuleNodeModules: [], throwOnModuleCollision: false, }, + longTestThreshold: 5, maxConcurrency: 5, maxWorkers: '50%', moduleDirectories: ['node_modules'], diff --git a/packages/jest-config/src/ValidConfig.ts b/packages/jest-config/src/ValidConfig.ts index b07cdb441f2c..93cbb9120627 100644 --- a/packages/jest-config/src/ValidConfig.ts +++ b/packages/jest-config/src/ValidConfig.ts @@ -63,6 +63,7 @@ const initialOptions: Config.InitialOptions = { json: false, lastCommit: false, logHeapUsage: true, + longTestThreshold: 5, maxConcurrency: 5, maxWorkers: '50%', moduleDirectories: ['node_modules'], diff --git a/packages/jest-config/src/index.ts b/packages/jest-config/src/index.ts index faf1a3deb1a0..0d378b1ec930 100644 --- a/packages/jest-config/src/index.ts +++ b/packages/jest-config/src/index.ts @@ -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, diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index f0cf0213eee0..40764b0c56b6 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -852,6 +852,7 @@ export default function normalize( case 'listTests': case 'logHeapUsage': case 'maxConcurrency': + case 'longTestThreshold': case 'mapCoverage': case 'name': case 'noStackTrace': @@ -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, diff --git a/packages/jest-core/src/lib/__tests__/__snapshots__/log_debug_messages.test.ts.snap b/packages/jest-core/src/lib/__tests__/__snapshots__/log_debug_messages.test.ts.snap index 3be6c4ebb716..5877c80ee929 100644 --- a/packages/jest-core/src/lib/__tests__/__snapshots__/log_debug_messages.test.ts.snap +++ b/packages/jest-core/src/lib/__tests__/__snapshots__/log_debug_messages.test.ts.snap @@ -90,6 +90,7 @@ exports[`prints the config object 1`] = ` "lastCommit": false, "listTests": false, "logHeapUsage": false, + "longTestThreshold": 5, "maxConcurrency": 5, "maxWorkers": 2, "noSCM": null, diff --git a/packages/jest-reporters/src/__tests__/get_result_header.test.js b/packages/jest-reporters/src/__tests__/get_result_header.test.js index 3c0c3351ae3e..8980aa29a06e 100644 --- a/packages/jest-reporters/src/__tests__/get_result_header.test.js +++ b/packages/jest-reporters/src/__tests__/get_result_header.test.js @@ -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', }; @@ -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`); +}); diff --git a/packages/jest-reporters/src/get_result_header.ts b/packages/jest-reporters/src/get_result_header.ts index 827b7f84833d..56ac867f5aee 100644 --- a/packages/jest-reporters/src/get_result_header.ts +++ b/packages/jest-reporters/src/get_result_header.ts @@ -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')); } diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index a69546d8b271..ad17eb67a029 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -46,6 +46,7 @@ export type DefaultOptions = { forceCoverageMatch: Array; globals: ConfigGlobals; haste: HasteConfig; + longTestThreshold: 5; maxConcurrency: number; maxWorkers: number | string; moduleDirectories: Array; @@ -136,6 +137,7 @@ export type InitialOptions = Partial<{ logHeapUsage: boolean; lastCommit: boolean; listTests: boolean; + longTestThreshold: number; mapCoverage: boolean; maxConcurrency: number; maxWorkers: number | string; @@ -259,6 +261,7 @@ export type GlobalConfig = { globalTeardown?: string; lastCommit: boolean; logHeapUsage: boolean; + longTestThreshold: number; listTests: boolean; maxConcurrency: number; maxWorkers: number;