Skip to content

Commit

Permalink
Support outputFile option for listTests option (#14980)
Browse files Browse the repository at this point in the history
  • Loading branch information
manoraj authored Mar 25, 2024
1 parent cde5f7b commit b3093f6
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
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
49 changes: 49 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,52 @@ 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: 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

0 comments on commit b3093f6

Please sign in to comment.