From 77558491541559b2b24d7c83da6db7ab8c3312b0 Mon Sep 17 00:00:00 2001 From: Karol Kuczmarski Date: Tue, 2 May 2017 14:43:04 -0700 Subject: [PATCH 1/2] Add --listTests flag --- docs/en/CLI.md | 4 ++++ .../__tests__/list_tests-test.js | 21 +++++++++++++++++++ .../list_tests/__tests__/dummy-test.js | 14 +++++++++++++ integration_tests/list_tests/package.json | 5 +++++ packages/jest-cli/src/cli/args.js | 5 +++++ packages/jest-cli/src/runJest.js | 8 +++++++ packages/jest-config/src/index.js | 1 + packages/jest-config/src/normalize.js | 1 + packages/jest-config/src/validConfig.js | 1 + .../src/__tests__/fixtures/jestConfig.js | 1 + types/Config.js | 1 + 11 files changed, 62 insertions(+) create mode 100644 integration_tests/__tests__/list_tests-test.js create mode 100644 integration_tests/list_tests/__tests__/dummy-test.js create mode 100644 integration_tests/list_tests/package.json diff --git a/docs/en/CLI.md b/docs/en/CLI.md index ac04bcf3a26d..686a958b494d 100644 --- a/docs/en/CLI.md +++ b/docs/en/CLI.md @@ -133,6 +133,10 @@ Write test results to a file when the `--json` option is also specified. Will run all tests affected by file changes in the last commit made. +### `--listTests` + +Lists all tests Jest will run given the other arguments, and exits. + ### `--logHeapUsage` Logs the heap usage after every test. Useful to debug memory leaks. Use together with `--runInBand` and `--expose-gc` in node. diff --git a/integration_tests/__tests__/list_tests-test.js b/integration_tests/__tests__/list_tests-test.js new file mode 100644 index 000000000000..3b1d3952e2bf --- /dev/null +++ b/integration_tests/__tests__/list_tests-test.js @@ -0,0 +1,21 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @emails oncall+jsinfra + */ +'use strict'; + +const runJest = require('../runJest'); + +describe('--listTests flag', () => { + it('causes tests to be printed out as JSON', () => { + const {status, stdout} = runJest('list_tests', ['--listTests']); + + expect(status).toBe(0); + expect(() => JSON.parse(stdout)).not.toThrow(); + }); +}); diff --git a/integration_tests/list_tests/__tests__/dummy-test.js b/integration_tests/list_tests/__tests__/dummy-test.js new file mode 100644 index 000000000000..79a9fbe8bc9c --- /dev/null +++ b/integration_tests/list_tests/__tests__/dummy-test.js @@ -0,0 +1,14 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +'use strict'; + +it("isn't actually run", () => { + // (because it is only used for --listTests) + expect(true).toBe(false); +}); diff --git a/integration_tests/list_tests/package.json b/integration_tests/list_tests/package.json new file mode 100644 index 000000000000..148788b25446 --- /dev/null +++ b/integration_tests/list_tests/package.json @@ -0,0 +1,5 @@ +{ + "jest": { + "testEnvironment": "node" + } +} diff --git a/packages/jest-cli/src/cli/args.js b/packages/jest-cli/src/cli/args.js index 2b57b7aa1282..4d88b00ff6ee 100644 --- a/packages/jest-cli/src/cli/args.js +++ b/packages/jest-cli/src/cli/args.js @@ -196,6 +196,11 @@ const options = { 'commit made.', type: 'boolean', }, + listTests: { + default: false, + description: 'Lists all tests Jest will run given the arguments and exits.', + type: 'boolean', + }, logHeapUsage: { default: undefined, description: 'Logs the heap usage after every test. Useful to debug ' + diff --git a/packages/jest-cli/src/runJest.js b/packages/jest-cli/src/runJest.js index b83cfc5879d5..968a65301099 100644 --- a/packages/jest-cli/src/runJest.js +++ b/packages/jest-cli/src/runJest.js @@ -175,6 +175,14 @@ const runJest = async ( ); allTests = sequencer.sort(allTests); + + // With --listTests, simply print the test info as JSON and exit. + if (globalConfig.listTests) { + const testsJson = JSON.stringify(allTests, null, ' '); + new Console(pipe, pipe).log(testsJson); + process.exit(0); + } + if (!allTests.length) { new Console(pipe, pipe).log(getNoTestsFoundMessage(testRunData, pattern)); } else if ( diff --git a/packages/jest-config/src/index.js b/packages/jest-config/src/index.js index 9e3d1b808d77..68145bbfcc2b 100644 --- a/packages/jest-config/src/index.js +++ b/packages/jest-config/src/index.js @@ -69,6 +69,7 @@ const getConfigs = ( coverageThreshold: options.coverageThreshold, expand: options.expand, forceExit: options.forceExit, + listTests: options.listTests, logHeapUsage: options.logHeapUsage, mapCoverage: options.mapCoverage, noStackTrace: options.noStackTrace, diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index 05813f54f961..606b967407a2 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -376,6 +376,7 @@ function normalize(options: InitialOptions, argv: Object = {}) { case 'coverageThreshold': case 'expand': case 'globals': + case 'listTests': case 'logHeapUsage': case 'mapCoverage': case 'moduleDirectories': diff --git a/packages/jest-config/src/validConfig.js b/packages/jest-config/src/validConfig.js index 67f4a3ca014a..55a977edd812 100644 --- a/packages/jest-config/src/validConfig.js +++ b/packages/jest-config/src/validConfig.js @@ -43,6 +43,7 @@ module.exports = ({ haste: { providesModuleNodeModules: ['react', 'react-native'], }, + listTests: false, logHeapUsage: true, mapCoverage: false, moduleDirectories: ['node_modules'], diff --git a/packages/jest-validate/src/__tests__/fixtures/jestConfig.js b/packages/jest-validate/src/__tests__/fixtures/jestConfig.js index 3b7d3757e57b..de401153c11a 100644 --- a/packages/jest-validate/src/__tests__/fixtures/jestConfig.js +++ b/packages/jest-validate/src/__tests__/fixtures/jestConfig.js @@ -85,6 +85,7 @@ const validConfig = { haste: { providesModuleNodeModules: ['react', 'react-native'], }, + listTests: true, logHeapUsage: true, moduleDirectories: ['node_modules'], moduleFileExtensions: ['js', 'json', 'jsx', 'node'], diff --git a/types/Config.js b/types/Config.js index 24df602ee164..0423a3883ee9 100644 --- a/types/Config.js +++ b/types/Config.js @@ -134,6 +134,7 @@ export type GlobalConfig = {| coverageThreshold: {global: {[key: string]: number}}, expand: boolean, forceExit: boolean, + listTests: boolean, logHeapUsage: boolean, mapCoverage: boolean, noStackTrace: boolean, From 533885b10f0e19c1b9a1fdfefc69d0e7c185266d Mon Sep 17 00:00:00 2001 From: cpojer Date: Wed, 3 May 2017 08:14:46 +0100 Subject: [PATCH 2/2] Fixes. --- docs/en/CLI.md | 2 +- packages/jest-cli/src/cli/runCLI.js | 4 +++- packages/jest-cli/src/runJest.js | 11 +++++------ packages/jest-config/src/index.js | 1 - packages/jest-config/src/normalize.js | 1 - packages/jest-config/src/validConfig.js | 1 - .../src/__tests__/fixtures/jestConfig.js | 1 - types/Config.js | 1 - 8 files changed, 9 insertions(+), 13 deletions(-) diff --git a/docs/en/CLI.md b/docs/en/CLI.md index 686a958b494d..b46d556b9c25 100644 --- a/docs/en/CLI.md +++ b/docs/en/CLI.md @@ -135,7 +135,7 @@ Will run all tests affected by file changes in the last commit made. ### `--listTests` -Lists all tests Jest will run given the other arguments, and exits. +Lists all tests as JSON that Jest will run given the arguments, and exits. This can be used together with `--findRelatedTests` to know which tests Jest will run. ### `--logHeapUsage` diff --git a/packages/jest-cli/src/cli/runCLI.js b/packages/jest-cli/src/cli/runCLI.js index cbe8643766d6..101e4a001b6f 100644 --- a/packages/jest-cli/src/cli/runCLI.js +++ b/packages/jest-cli/src/cli/runCLI.js @@ -75,7 +75,9 @@ module.exports = async ( return watch(globalConfig, contexts, argv, pipe, hasteMapInstances); } else { const startRun = () => { - preRunMessage.print(pipe); + if (!argv.listTests) { + preRunMessage.print(pipe); + } runJest( globalConfig, contexts, diff --git a/packages/jest-cli/src/runJest.js b/packages/jest-cli/src/runJest.js index 968a65301099..a6824f4f5f9e 100644 --- a/packages/jest-cli/src/runJest.js +++ b/packages/jest-cli/src/runJest.js @@ -154,7 +154,7 @@ const runJest = async ( pipe: stream$Writable | tty$WriteStream, testWatcher: TestWatcher, startRun: () => *, - onComplete: (testResults: any) => void, + onComplete: (testResults: any) => any, ) => { const maxWorkers = getMaxWorkers(argv); const pattern = getTestPathPattern(argv); @@ -176,11 +176,10 @@ const runJest = async ( allTests = sequencer.sort(allTests); - // With --listTests, simply print the test info as JSON and exit. - if (globalConfig.listTests) { - const testsJson = JSON.stringify(allTests, null, ' '); - new Console(pipe, pipe).log(testsJson); - process.exit(0); + if (argv.listTests) { + console.log(JSON.stringify(allTests.map(test => test.path))); + onComplete && onComplete({success: true}); + return null; } if (!allTests.length) { diff --git a/packages/jest-config/src/index.js b/packages/jest-config/src/index.js index 68145bbfcc2b..9e3d1b808d77 100644 --- a/packages/jest-config/src/index.js +++ b/packages/jest-config/src/index.js @@ -69,7 +69,6 @@ const getConfigs = ( coverageThreshold: options.coverageThreshold, expand: options.expand, forceExit: options.forceExit, - listTests: options.listTests, logHeapUsage: options.logHeapUsage, mapCoverage: options.mapCoverage, noStackTrace: options.noStackTrace, diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index 606b967407a2..05813f54f961 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -376,7 +376,6 @@ function normalize(options: InitialOptions, argv: Object = {}) { case 'coverageThreshold': case 'expand': case 'globals': - case 'listTests': case 'logHeapUsage': case 'mapCoverage': case 'moduleDirectories': diff --git a/packages/jest-config/src/validConfig.js b/packages/jest-config/src/validConfig.js index 55a977edd812..67f4a3ca014a 100644 --- a/packages/jest-config/src/validConfig.js +++ b/packages/jest-config/src/validConfig.js @@ -43,7 +43,6 @@ module.exports = ({ haste: { providesModuleNodeModules: ['react', 'react-native'], }, - listTests: false, logHeapUsage: true, mapCoverage: false, moduleDirectories: ['node_modules'], diff --git a/packages/jest-validate/src/__tests__/fixtures/jestConfig.js b/packages/jest-validate/src/__tests__/fixtures/jestConfig.js index de401153c11a..3b7d3757e57b 100644 --- a/packages/jest-validate/src/__tests__/fixtures/jestConfig.js +++ b/packages/jest-validate/src/__tests__/fixtures/jestConfig.js @@ -85,7 +85,6 @@ const validConfig = { haste: { providesModuleNodeModules: ['react', 'react-native'], }, - listTests: true, logHeapUsage: true, moduleDirectories: ['node_modules'], moduleFileExtensions: ['js', 'json', 'jsx', 'node'], diff --git a/types/Config.js b/types/Config.js index 0423a3883ee9..24df602ee164 100644 --- a/types/Config.js +++ b/types/Config.js @@ -134,7 +134,6 @@ export type GlobalConfig = {| coverageThreshold: {global: {[key: string]: number}}, expand: boolean, forceExit: boolean, - listTests: boolean, logHeapUsage: boolean, mapCoverage: boolean, noStackTrace: boolean,