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

Make --listTests return a new line separated list when not using --json #4229

Merged
merged 3 commits into from
Aug 10, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`--listTests flag causes tests to be printed in different lines 1`] = `
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's even better! <3

/MOCK_ABOLUTE_PATH/integration_tests/list_tests/__tests__/dummy.test.js
/MOCK_ABOLUTE_PATH/integration_tests/list_tests/__tests__/other.test.js
`;

exports[`--listTests flag causes tests to be printed out as JSON when using the --json flag 1`] = `["/MOCK_ABOLUTE_PATH/integration_tests/list_tests/__tests__/dummy.test.js","/MOCK_ABOLUTE_PATH/integration_tests/list_tests/__tests__/other.test.js"]`;
19 changes: 17 additions & 2 deletions integration_tests/__tests__/list_tests.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,27 @@
'use strict';

const runJest = require('../runJest');
const path = require('path');

const testRootDir = path.resolve(__dirname, '..', '..');
expect.addSnapshotSerializer({
print: val => val.replace(new RegExp(testRootDir, 'g'), '/MOCK_ABOLUTE_PATH'),
test: val => typeof val === 'string' && val.includes(testRootDir),
});

describe('--listTests flag', () => {
it('causes tests to be printed out as JSON', () => {
it('causes tests to be printed in different lines', () => {
const {status, stdout} = runJest('list_tests', ['--listTests']);

expect(status).toBe(0);
expect(() => JSON.parse(stdout)).not.toThrow();
expect(stdout).toMatchSnapshot();
});

it('causes tests to be printed out as JSON when using the --json flag', () => {
const {status, stderr} = runJest('list_tests', ['--listTests', '--json']);

expect(status).toBe(0);
expect(() => JSON.parse(stderr)).not.toThrow();
expect(stderr).toMatchSnapshot();
});
});
14 changes: 14 additions & 0 deletions integration_tests/list_tests/__tests__/other.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);
});
8 changes: 7 additions & 1 deletion packages/jest-cli/src/run_jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,13 @@ const runJest = async ({
allTests = sequencer.sort(allTests);

if (globalConfig.listTests) {
console.log(JSON.stringify(allTests.map(test => test.path)));
const testsPaths = allTests.map(test => test.path);
if (globalConfig.json) {
outputStream.write(JSON.stringify(testsPaths));
} else {
outputStream.write(testsPaths.join('\n'));
}

onComplete && onComplete(makeEmptyAggregatedTestResult());
return null;
}
Expand Down