From b1b38956256c696cf4001dbee0bbf4015f82edf3 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Fri, 29 Apr 2022 09:51:46 +0300 Subject: [PATCH 1/5] fix(jest-config): normalize reporters from presets --- packages/jest-config/src/normalize.ts | 32 +++++++++++++++------------ 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index 43d82bfb8c64..9e859517e9fc 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -375,14 +375,19 @@ const normalizeRootDir = ( }; }; -const normalizeReporters = (options: Config.InitialOptionsWithRootDir) => { - const reporters = options.reporters; +const normalizeReporters = ({ + reporters, + rootDir, +}: Config.InitialOptionsWithRootDir): + | Array + | undefined => { if (!reporters || !Array.isArray(reporters)) { - return options; + return undefined; } validateReporters(reporters); - options.reporters = reporters.map(reporterConfig => { + + return reporters.map(reporterConfig => { const normalizedReporterConfig: Config.ReporterConfig = typeof reporterConfig === 'string' ? // if reporter config is a string, we wrap it in an array @@ -392,13 +397,13 @@ const normalizeReporters = (options: Config.InitialOptionsWithRootDir) => { : reporterConfig; const reporterPath = replaceRootDirInPath( - options.rootDir, + rootDir, normalizedReporterConfig[0], ); if (!['default', 'github-actions', 'summary'].includes(reporterPath)) { const reporter = Resolver.findNodeModule(reporterPath, { - basedir: options.rootDir, + basedir: rootDir, }); if (!reporter) { throw new Resolver.ModuleNotFoundError( @@ -410,8 +415,6 @@ const normalizeReporters = (options: Config.InitialOptionsWithRootDir) => { } return normalizedReporterConfig; }); - - return options; }; const buildTestPathPattern = (argv: Config.Argv): string => { @@ -536,12 +539,10 @@ export default async function normalize( ], }); - let options = normalizeReporters( - normalizeMissingOptions( - normalizeRootDir(setFromArgv(initialOptions, argv)), - configPath, - projectIndex, - ), + let options = normalizeMissingOptions( + normalizeRootDir(setFromArgv(initialOptions, argv)), + configPath, + projectIndex, ); if (options.preset) { @@ -749,6 +750,9 @@ export default async function normalize( ]; }); break; + case 'reporters': + value = normalizeReporters(oldOptions); + break; case 'coveragePathIgnorePatterns': case 'modulePathIgnorePatterns': case 'testPathIgnorePatterns': From 5030033897c883b6779813b409a211c8a9b7cfac Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Fri, 29 Apr 2022 10:09:28 +0300 Subject: [PATCH 2/5] add chnagelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 096c72a4bf18..fb13f95fdecb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Fixes +- `[jest-config]` Normalize `reporters` option defined in presets ([#12769](https://github.com/facebook/jest/pull/12769)) - `[@jest/reporters]` Fix trailing slash in matching `coverageThreshold` key ([#12714](https://github.com/facebook/jest/pull/12714)) - `[@jest/transform]` Throw better error if an invalid return value if encountered ([#12764](https://github.com/facebook/jest/pull/12764)) From 1ddd54b7e9cbe49a531db6ebaef257bc3198a16f Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Fri, 29 Apr 2022 10:19:46 +0300 Subject: [PATCH 3/5] add unit tests --- .../src/__tests__/normalize.test.ts | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/packages/jest-config/src/__tests__/normalize.test.ts b/packages/jest-config/src/__tests__/normalize.test.ts index a480b4b4229e..4ec6c24d5963 100644 --- a/packages/jest-config/src/__tests__/normalize.test.ts +++ b/packages/jest-config/src/__tests__/normalize.test.ts @@ -1416,6 +1416,55 @@ describe.each(['setupFiles', 'setupFilesAfterEnv'])( }, ); +describe("preset with 'reporters' option", () => { + beforeEach(() => { + const Resolver = require('jest-resolve').default; + Resolver.findNodeModule = jest.fn(name => { + if (name === 'with-reporters/jest-preset') { + return '/node_modules/with-reporters/jest-preset.json'; + } + + return `/node_modules/${name}`; + }); + jest.doMock( + '/node_modules/with-reporters/jest-preset.json', + () => ({ + reporters: ['default'], + }), + {virtual: true}, + ); + }); + + afterEach(() => { + jest.dontMock('/node_modules/with-reporters/jest-preset.json'); + }); + + test("normalizes 'reporters' option defined in preset", async () => { + const {options} = await normalize( + { + preset: 'with-reporters', + rootDir: '/root/', + }, + {} as Config.Argv, + ); + + expect(options.reporters).toEqual([['default', {}]]); + }); + + test("overrides 'reporters' option defined in preset", async () => { + const {options} = await normalize( + { + preset: 'with-reporters', + reporters: ['summary'], + rootDir: '/root/', + }, + {} as Config.Argv, + ); + + expect(options.reporters).toEqual([['summary', {}]]); + }); +}); + describe('runner', () => { let Resolver; beforeEach(() => { From 7f94b2cac587b34c0501bde64189f91665c9dd7f Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Fri, 29 Apr 2022 10:34:57 +0300 Subject: [PATCH 4/5] remove case --- packages/jest-config/src/normalize.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index 9e859517e9fc..e54011a8752a 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -942,7 +942,6 @@ export default async function normalize( case 'outputFile': case 'passWithNoTests': case 'replname': - case 'reporters': case 'resetMocks': case 'resetModules': case 'restoreMocks': From b2cf83f6e3ee35ab89790d01b9d286bbc33403d6 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Fri, 29 Apr 2022 10:39:09 +0300 Subject: [PATCH 5/5] error on 'no-duplicate-case' --- .eslintrc.cjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 44d9227dfa97..ea25dd29269b 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -383,7 +383,7 @@ module.exports = { 'no-dupe-args': 'error', 'no-dupe-class-members': 'error', 'no-dupe-keys': 'error', - 'no-duplicate-case': 'warn', + 'no-duplicate-case': 'error', 'no-else-return': 'off', 'no-empty': 'off', 'no-empty-character-class': 'warn',