diff --git a/integration_tests/__tests__/__snapshots__/list_tests.test.js.snap b/integration_tests/__tests__/__snapshots__/list_tests.test.js.snap new file mode 100644 index 000000000000..c5353c8bccc1 --- /dev/null +++ b/integration_tests/__tests__/__snapshots__/list_tests.test.js.snap @@ -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"]`; diff --git a/integration_tests/__tests__/list_tests.test.js b/integration_tests/__tests__/list_tests.test.js index 169b2658c32d..4b91bcc55dbb 100644 --- a/integration_tests/__tests__/list_tests.test.js +++ b/integration_tests/__tests__/list_tests.test.js @@ -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(); }); }); diff --git a/integration_tests/list_tests/__tests__/other.test.js b/integration_tests/list_tests/__tests__/other.test.js new file mode 100644 index 000000000000..79a9fbe8bc9c --- /dev/null +++ b/integration_tests/list_tests/__tests__/other.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/packages/jest-cli/src/run_jest.js b/packages/jest-cli/src/run_jest.js index 85d1dcf9c066..d8ad8095bbc7 100644 --- a/packages/jest-cli/src/run_jest.js +++ b/packages/jest-cli/src/run_jest.js @@ -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; }