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', 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)) 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(() => { diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index 43d82bfb8c64..e54011a8752a 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': @@ -938,7 +942,6 @@ export default async function normalize( case 'outputFile': case 'passWithNoTests': case 'replname': - case 'reporters': case 'resetMocks': case 'resetModules': case 'restoreMocks':