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

Support outputFile option for listTests option #14944

1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- `[@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))
- `[@jest/core]` Add `perfStats` to surface test setup overhead ([#14622](https://github.com/jestjs/jest/pull/14622))
- `[@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))
- `[@jest/core]` Support `--outputFile` option for [listTests](https://jestjs.io/docs/cli#--listtests) ([#14944](https://github.com/facebook/jest/pull/14944))
- `[@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))
- `[jest-environment-jsdom]` [**BREAKING**] Upgrade JSDOM to v22 ([#13825](https://github.com/jestjs/jest/pull/13825))
- `[@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))
Expand Down
7 changes: 7 additions & 0 deletions e2e/__tests__/__snapshots__/listTests.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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"]"`;

exports[`--listTests flag --outputFile flag causes tests to be saved in the file in different lines 1`] = `
"/MOCK_ABSOLUTE_PATH/e2e/list-tests/__tests__/dummy.test.js
/MOCK_ABSOLUTE_PATH/e2e/list-tests/__tests__/other.test.js"
`;

exports[`--listTests flag causes tests to be printed in different lines 1`] = `
"/MOCK_ABSOLUTE_PATH/e2e/list-tests/__tests__/dummy.test.js
/MOCK_ABSOLUTE_PATH/e2e/list-tests/__tests__/other.test.js"
Expand Down
48 changes: 48 additions & 0 deletions e2e/__tests__/listTests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import * as path from 'path';
import * as fs from 'graceful-fs';
import runJest from '../runJest';

const testRootDir = path.resolve(__dirname, '..', '..');
Expand Down Expand Up @@ -36,4 +37,51 @@ describe('--listTests flag', () => {
JSON.stringify(JSON.parse(stdout).map(normalizePaths).sort()),
).toMatchSnapshot();
});

describe('--outputFile flag', () => {
const outputFilePath = path.resolve('.', 'test-lists.json');
afterAll(() => {
fs.unlinkSync(outputFilePath);
});
it('causes tests to be saved in the file as JSON', () => {
const {exitCode, stdout} = runJest('list-tests', [
'--listTests',
'--json',
'--outputFile',
outputFilePath,
]);

expect(exitCode).toBe(0);
expect(stdout).toBe('');

const outputFileExists = fs.existsSync(outputFilePath);
expect(outputFileExists).toBe(true);

const outputFileContent = fs.readFileSync(outputFilePath, 'utf8');
expect(() => JSON.parse(outputFileContent)).not.toThrow();
expect(
JSON.stringify(
JSON.parse(outputFileContent).map(normalizePaths).sort(),
),
).toMatchSnapshot();
});
it('causes tests to be saved in the file in different lines', () => {
const {exitCode, stdout} = runJest('list-tests', [
'--listTests',
'--outputFile',
outputFilePath,
]);

expect(exitCode).toBe(0);
expect(stdout).toBe('');

const outputFileExists = fs.existsSync(outputFilePath);
expect(outputFileExists).toBe(true);

const outputFileContent = fs.readFileSync(outputFilePath, 'utf8');
expect(
normalizePaths(outputFileContent).split('\n').sort().join('\n'),
).toMatchSnapshot();
});
});
});
16 changes: 10 additions & 6 deletions packages/jest-core/src/runJest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,19 @@ export default async function runJest({

if (globalConfig.listTests) {
const testsPaths = [...new Set(allTests.map(test => test.path))];
/* eslint-disable no-console */
if (globalConfig.json) {
console.log(JSON.stringify(testsPaths));
const testsListOutput = globalConfig.json
? JSON.stringify(testsPaths)
: testsPaths.join('\n');

if (globalConfig.outputFile) {
const outputFile = path.resolve(process.cwd(), globalConfig.outputFile);
fs.writeFileSync(outputFile, testsListOutput, 'utf8');
} else {
console.log(testsPaths.join('\n'));
// eslint-disable-next-line no-console
console.log(testsListOutput);
}
/* eslint-enable */

onComplete && onComplete(makeEmptyAggregatedTestResult());
onComplete(makeEmptyAggregatedTestResult());
return;
}

Expand Down
Loading