Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --listTests flag #3441

Merged
merged 2 commits into from
May 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/en/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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`

Logs the heap usage after every test. Useful to debug memory leaks. Use together with `--runInBand` and `--expose-gc` in node.
Expand Down
21 changes: 21 additions & 0 deletions integration_tests/__tests__/list_tests-test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
14 changes: 14 additions & 0 deletions integration_tests/list_tests/__tests__/dummy-test.js
Original file line number Diff line number Diff line change
@@ -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);
});
5 changes: 5 additions & 0 deletions integration_tests/list_tests/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
5 changes: 5 additions & 0 deletions packages/jest-cli/src/cli/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ' +
Expand Down
4 changes: 3 additions & 1 deletion packages/jest-cli/src/cli/runCLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion packages/jest-cli/src/runJest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -175,6 +175,13 @@ const runJest = async (
);

allTests = sequencer.sort(allTests);

if (argv.listTests) {
console.log(JSON.stringify(allTests.map(test => test.path)));
onComplete && onComplete({success: true});
return null;
}

if (!allTests.length) {
new Console(pipe, pipe).log(getNoTestsFoundMessage(testRunData, pattern));
} else if (
Expand Down