From 0708dafce34ad7704e3733859453897af1495a68 Mon Sep 17 00:00:00 2001 From: Sibiraj <20282546+sibiraj-s@users.noreply.github.com> Date: Fri, 31 Jul 2020 15:16:51 +0530 Subject: [PATCH] fix(jest-cli): use correct filename to override config (#10337) --- CHANGELOG.md | 2 ++ .../jest-cli/src/init/__tests__/init.test.js | 2 ++ packages/jest-cli/src/init/index.ts | 26 +++++++++---------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 392a977ffa31..c2f6e40f09f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Fixes +- `[jest-cli]` Use correct file name to override existing jest config on init ([#10337](https://github.com/facebook/jest/pull/10337)) + ### Chore & Maintenance ### Performance diff --git a/packages/jest-cli/src/init/__tests__/init.test.js b/packages/jest-cli/src/init/__tests__/init.test.js index 74012354c51a..0824c70f43a1 100644 --- a/packages/jest-cli/src/init/__tests__/init.test.js +++ b/packages/jest-cli/src/init/__tests__/init.test.js @@ -175,8 +175,10 @@ describe('init', () => { expect(prompts.mock.calls[0][0]).toMatchSnapshot(); + const jestConfigFileName = fs.writeFileSync.mock.calls[0][0]; const writtenJestConfig = fs.writeFileSync.mock.calls[0][1]; + expect(jestConfigFileName).toBe(`jest.config.${extension}`); expect(writtenJestConfig).toBeDefined(); }); diff --git a/packages/jest-cli/src/init/index.ts b/packages/jest-cli/src/init/index.ts index c39ca0b68f69..707b41df12bc 100644 --- a/packages/jest-cli/src/init/index.ts +++ b/packages/jest-cli/src/init/index.ts @@ -61,21 +61,21 @@ export default async ( hasJestProperty = true; } - const existingJestConfigPath = JEST_CONFIG_EXT_ORDER.find(ext => + const existingJestConfigExt = JEST_CONFIG_EXT_ORDER.find(ext => fs.existsSync(path.join(rootDir, getConfigFilename(ext))), ); - const jestConfigPath = - existingJestConfigPath || - path.join( - rootDir, - getConfigFilename( - projectPackageJson.type === 'module' - ? JEST_CONFIG_EXT_MJS - : JEST_CONFIG_EXT_JS, - ), - ); - - if (hasJestProperty || existingJestConfigPath) { + const jestConfigPath = existingJestConfigExt + ? getConfigFilename(existingJestConfigExt) + : path.join( + rootDir, + getConfigFilename( + projectPackageJson.type === 'module' + ? JEST_CONFIG_EXT_MJS + : JEST_CONFIG_EXT_JS, + ), + ); + + if (hasJestProperty || existingJestConfigExt) { const result: {continue: boolean} = await prompts({ initial: true, message: