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 #14980

Merged
Show file tree
Hide file tree
Changes from 5 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
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) ([#14980](https://github.com/jestjs/jest/pull/14980))
- `[@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', () => {
SimenB marked this conversation as resolved.
Show resolved Hide resolved
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: 12 additions & 4 deletions packages/jest-core/src/runJest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,21 @@ export default async function runJest({

if (globalConfig.listTests) {
const testsPaths = [...new Set(allTests.map(test => test.path))];
/* eslint-disable no-console */
let testsListOutput;

if (globalConfig.json) {
console.log(JSON.stringify(testsPaths));
testsListOutput = JSON.stringify(testsPaths);
} else {
testsListOutput = 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());
return;
Expand Down
Loading