From a16397e83cf4b4e2a9998c25d13645f3e5c37c96 Mon Sep 17 00:00:00 2001 From: Manoraj K Date: Thu, 21 Mar 2024 09:12:16 +0530 Subject: [PATCH 1/5] Support outputFile option for listTests option --- CHANGELOG.md | 1 + .../__snapshots__/listTests.test.ts.snap | 11 ++++- e2e/__tests__/listTests.test.ts | 49 +++++++++++++++++++ packages/jest-core/src/runJest.ts | 16 ++++-- 4 files changed, 71 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13752c82a913..26464a53eac4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ - `[jest-snapshot]` [**BREAKING**] Add support for [Error causes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) in snapshots ([#13965](https://github.com/facebook/jest/pull/13965)) - `[jest-snapshot]` Support Prettier 3 ([#14566](https://github.com/facebook/jest/pull/14566)) - `[pretty-format]` [**BREAKING**] Do not render empty string children (`''`) in React plugin ([#14470](https://github.com/facebook/jest/pull/14470)) +- `[@jest/core]` Support `--outputFile` option for [listTests](https://jestjs.io/docs/cli#--listtests) ([#14944](https://github.com/facebook/jest/pull/14944)) ### Fixes diff --git a/e2e/__tests__/__snapshots__/listTests.test.ts.snap b/e2e/__tests__/__snapshots__/listTests.test.ts.snap index 519ed836efdb..d5fd749902d4 100644 --- a/e2e/__tests__/__snapshots__/listTests.test.ts.snap +++ b/e2e/__tests__/__snapshots__/listTests.test.ts.snap @@ -1,8 +1,15 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`--listTests flag causes tests to be printed in different lines 1`] = ` +exports[`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" `; -exports[`--listTests flag causes tests to be printed out as JSON when using the --json flag 1`] = `"["/MOCK_ABSOLUTE_PATH/e2e/list-tests/__tests__/dummy.test.js","/MOCK_ABSOLUTE_PATH/e2e/list-tests/__tests__/other.test.js"]"`; +exports[`causes tests to be printed out as JSON when using the --json flag 1`] = `"["/MOCK_ABSOLUTE_PATH/e2e/list-tests/__tests__/dummy.test.js","/MOCK_ABSOLUTE_PATH/e2e/list-tests/__tests__/other.test.js"]"`; + +exports[`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[`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" +`; diff --git a/e2e/__tests__/listTests.test.ts b/e2e/__tests__/listTests.test.ts index 811527848d4d..8dc2a84e8930 100644 --- a/e2e/__tests__/listTests.test.ts +++ b/e2e/__tests__/listTests.test.ts @@ -5,7 +5,9 @@ * LICENSE file in the root directory of this source tree. */ +import {describe} from 'node:test'; import * as path from 'path'; +import * as fs from 'graceful-fs'; import runJest from '../runJest'; const testRootDir = path.resolve(__dirname, '..', '..'); @@ -36,4 +38,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(); + }); + }); }); diff --git a/packages/jest-core/src/runJest.ts b/packages/jest-core/src/runJest.ts index 565cec8fc7f2..fb753aa7a06e 100644 --- a/packages/jest-core/src/runJest.ts +++ b/packages/jest-core/src/runJest.ts @@ -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; From 3f0a76d35dfe5c1ffde68ab51ad287f9b6db94f0 Mon Sep 17 00:00:00 2001 From: Manoraj K Date: Thu, 21 Mar 2024 09:20:10 +0530 Subject: [PATCH 2/5] Updated PR no in the CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 26464a53eac4..300817a38ab2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,7 +27,7 @@ - `[jest-snapshot]` [**BREAKING**] Add support for [Error causes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) in snapshots ([#13965](https://github.com/facebook/jest/pull/13965)) - `[jest-snapshot]` Support Prettier 3 ([#14566](https://github.com/facebook/jest/pull/14566)) - `[pretty-format]` [**BREAKING**] Do not render empty string children (`''`) in React plugin ([#14470](https://github.com/facebook/jest/pull/14470)) -- `[@jest/core]` Support `--outputFile` option for [listTests](https://jestjs.io/docs/cli#--listtests) ([#14944](https://github.com/facebook/jest/pull/14944)) +- `[@jest/core]` Support `--outputFile` option for [listTests](https://jestjs.io/docs/cli#--listtests) ([#14980](https://github.com/jestjs/jest/pull/14980)) ### Fixes From 3276fa6a7e62cc56038f845128356cd748a69e61 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 25 Mar 2024 07:52:40 +0100 Subject: [PATCH 3/5] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 15d4fabd292f..5639d3e3af23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 }` 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)) @@ -29,7 +30,6 @@ - `[jest-snapshot]` [**BREAKING**] Add support for [Error causes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) in snapshots ([#13965](https://github.com/facebook/jest/pull/13965)) - `[jest-snapshot]` Support Prettier 3 ([#14566](https://github.com/facebook/jest/pull/14566)) - `[pretty-format]` [**BREAKING**] Do not render empty string children (`''`) in React plugin ([#14470](https://github.com/facebook/jest/pull/14470)) -- `[@jest/core]` Support `--outputFile` option for [listTests](https://jestjs.io/docs/cli#--listtests) ([#14980](https://github.com/jestjs/jest/pull/14980)) ### Fixes From e11aca8d9d6e1f47a22c684896763bfda6abaa88 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 25 Mar 2024 07:54:25 +0100 Subject: [PATCH 4/5] remove broken describe import --- e2e/__tests__/__snapshots__/listTests.test.ts.snap | 12 ++++++------ e2e/__tests__/listTests.test.ts | 1 - 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/e2e/__tests__/__snapshots__/listTests.test.ts.snap b/e2e/__tests__/__snapshots__/listTests.test.ts.snap index d5fd749902d4..1440d531b30a 100644 --- a/e2e/__tests__/__snapshots__/listTests.test.ts.snap +++ b/e2e/__tests__/__snapshots__/listTests.test.ts.snap @@ -1,15 +1,15 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`causes tests to be printed in different lines 1`] = ` +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[`causes tests to be printed out as JSON when using the --json flag 1`] = `"["/MOCK_ABSOLUTE_PATH/e2e/list-tests/__tests__/dummy.test.js","/MOCK_ABSOLUTE_PATH/e2e/list-tests/__tests__/other.test.js"]"`; - -exports[`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[`causes tests to be saved in the file in different lines 1`] = ` +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" `; + +exports[`--listTests flag causes tests to be printed out as JSON when using the --json flag 1`] = `"["/MOCK_ABSOLUTE_PATH/e2e/list-tests/__tests__/dummy.test.js","/MOCK_ABSOLUTE_PATH/e2e/list-tests/__tests__/other.test.js"]"`; diff --git a/e2e/__tests__/listTests.test.ts b/e2e/__tests__/listTests.test.ts index 8dc2a84e8930..4e08588cd96e 100644 --- a/e2e/__tests__/listTests.test.ts +++ b/e2e/__tests__/listTests.test.ts @@ -5,7 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -import {describe} from 'node:test'; import * as path from 'path'; import * as fs from 'graceful-fs'; import runJest from '../runJest'; From 5a79268cee0d7e8b0b1e3669457d33b3234dbe00 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 25 Mar 2024 07:55:16 +0100 Subject: [PATCH 5/5] Update e2e/__tests__/listTests.test.ts --- e2e/__tests__/listTests.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/e2e/__tests__/listTests.test.ts b/e2e/__tests__/listTests.test.ts index 4e08588cd96e..7bf7708d6164 100644 --- a/e2e/__tests__/listTests.test.ts +++ b/e2e/__tests__/listTests.test.ts @@ -65,6 +65,7 @@ describe('--listTests flag', () => { ), ).toMatchSnapshot(); }); + it('causes tests to be saved in the file in different lines', () => { const {exitCode, stdout} = runJest('list-tests', [ '--listTests',