Skip to content

Commit b3093f6

Browse files
authored
Support outputFile option for listTests option (#14980)
1 parent cde5f7b commit b3093f6

File tree

4 files changed

+69
-4
lines changed

4 files changed

+69
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- `[@jest/core]` Group together open handles with the same stack trace ([#13417](https://github.com/jestjs/jest/pull/13417), & [#14789](https://github.com/jestjs/jest/pull/14789))
1313
- `[@jest/core]` Add `perfStats` to surface test setup overhead ([#14622](https://github.com/jestjs/jest/pull/14622))
1414
- `[@jest/core]` [**BREAKING**] Changed `--filter` to accept an object with shape `{ filtered: Array<string> }` to match [documentation](https://jestjs.io/docs/cli#--filterfile) ([#13319](https://github.com/jestjs/jest/pull/13319))
15+
- `[@jest/core]` Support `--outputFile` option for [`--listTests`](https://jestjs.io/docs/cli#--listtests) ([#14980](https://github.com/jestjs/jest/pull/14980))
1516
- `[@jest/core, @jest/test-sequencer]` [**BREAKING**] Exposes `globalConfig` & `contexts` to `TestSequencer` ([#14535](https://github.com/jestjs/jest/pull/14535), & [#14543](https://github.com/jestjs/jest/pull/14543))
1617
- `[jest-environment-jsdom]` [**BREAKING**] Upgrade JSDOM to v22 ([#13825](https://github.com/jestjs/jest/pull/13825))
1718
- `[@jest/environment-jsdom-abstract]` Introduce new package which abstracts over the `jsdom` environment, allowing usage of custom versions of JSDOM ([#14717](https://github.com/jestjs/jest/pull/14717))

e2e/__tests__/__snapshots__/listTests.test.ts.snap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`--listTests flag --outputFile flag causes tests to be saved in the file as JSON 1`] = `"["/MOCK_ABSOLUTE_PATH/e2e/list-tests/__tests__/dummy.test.js","/MOCK_ABSOLUTE_PATH/e2e/list-tests/__tests__/other.test.js"]"`;
4+
5+
exports[`--listTests flag --outputFile flag causes tests to be saved in the file in different lines 1`] = `
6+
"/MOCK_ABSOLUTE_PATH/e2e/list-tests/__tests__/dummy.test.js
7+
/MOCK_ABSOLUTE_PATH/e2e/list-tests/__tests__/other.test.js"
8+
`;
9+
310
exports[`--listTests flag causes tests to be printed in different lines 1`] = `
411
"/MOCK_ABSOLUTE_PATH/e2e/list-tests/__tests__/dummy.test.js
512
/MOCK_ABSOLUTE_PATH/e2e/list-tests/__tests__/other.test.js"

e2e/__tests__/listTests.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
import * as path from 'path';
9+
import * as fs from 'graceful-fs';
910
import runJest from '../runJest';
1011

1112
const testRootDir = path.resolve(__dirname, '..', '..');
@@ -36,4 +37,52 @@ describe('--listTests flag', () => {
3637
JSON.stringify(JSON.parse(stdout).map(normalizePaths).sort()),
3738
).toMatchSnapshot();
3839
});
40+
41+
describe('--outputFile flag', () => {
42+
const outputFilePath = path.resolve('.', 'test-lists.json');
43+
afterAll(() => {
44+
fs.unlinkSync(outputFilePath);
45+
});
46+
it('causes tests to be saved in the file as JSON', () => {
47+
const {exitCode, stdout} = runJest('list-tests', [
48+
'--listTests',
49+
'--json',
50+
'--outputFile',
51+
outputFilePath,
52+
]);
53+
54+
expect(exitCode).toBe(0);
55+
expect(stdout).toBe('');
56+
57+
const outputFileExists = fs.existsSync(outputFilePath);
58+
expect(outputFileExists).toBe(true);
59+
60+
const outputFileContent = fs.readFileSync(outputFilePath, 'utf8');
61+
expect(() => JSON.parse(outputFileContent)).not.toThrow();
62+
expect(
63+
JSON.stringify(
64+
JSON.parse(outputFileContent).map(normalizePaths).sort(),
65+
),
66+
).toMatchSnapshot();
67+
});
68+
69+
it('causes tests to be saved in the file in different lines', () => {
70+
const {exitCode, stdout} = runJest('list-tests', [
71+
'--listTests',
72+
'--outputFile',
73+
outputFilePath,
74+
]);
75+
76+
expect(exitCode).toBe(0);
77+
expect(stdout).toBe('');
78+
79+
const outputFileExists = fs.existsSync(outputFilePath);
80+
expect(outputFileExists).toBe(true);
81+
82+
const outputFileContent = fs.readFileSync(outputFilePath, 'utf8');
83+
expect(
84+
normalizePaths(outputFileContent).split('\n').sort().join('\n'),
85+
).toMatchSnapshot();
86+
});
87+
});
3988
});

packages/jest-core/src/runJest.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,21 @@ export default async function runJest({
214214

215215
if (globalConfig.listTests) {
216216
const testsPaths = [...new Set(allTests.map(test => test.path))];
217-
/* eslint-disable no-console */
217+
let testsListOutput;
218+
218219
if (globalConfig.json) {
219-
console.log(JSON.stringify(testsPaths));
220+
testsListOutput = JSON.stringify(testsPaths);
221+
} else {
222+
testsListOutput = testsPaths.join('\n');
223+
}
224+
225+
if (globalConfig.outputFile) {
226+
const outputFile = path.resolve(process.cwd(), globalConfig.outputFile);
227+
fs.writeFileSync(outputFile, testsListOutput, 'utf8');
220228
} else {
221-
console.log(testsPaths.join('\n'));
229+
// eslint-disable-next-line no-console
230+
console.log(testsListOutput);
222231
}
223-
/* eslint-enable */
224232

225233
onComplete && onComplete(makeEmptyAggregatedTestResult());
226234
return;

0 commit comments

Comments
 (0)