Skip to content

Commit

Permalink
Make --listTests return a new line separated list when not using --js…
Browse files Browse the repository at this point in the history
…on (jestjs#4229)

* Make `--listTests` return a new line separated list when not using `--json`

* Fix tests

* Sort dummy test files
  • Loading branch information
rogeliog authored and cpojer committed Aug 10, 2017
1 parent 9e67813 commit 887d102
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
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`] = `
/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.split('\n').sort().join('\n')).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(JSON.stringify(JSON.parse(stderr).sort())).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

0 comments on commit 887d102

Please sign in to comment.