From f449179757a9146374f33a1e5faaa8b764537f7e Mon Sep 17 00:00:00 2001 From: notaphplover Date: Sun, 8 Dec 2024 08:41:33 +0100 Subject: [PATCH] fix(jest-config): update test config roots updated roots to avoid duplicated jest haste map entries --- .../lib/config/getJestJsProjectConfig.js | 7 ++++++- .../lib/config/getJestProjectConfig.js | 14 ++++++++++---- .../lib/config/getJestTsProjectConfig.js | 7 ++++++- .../jest-config/lib/config/getProjectRoot.js | 13 +++++++++++++ .../tools/jest-config/lib/config/getTestMatch.js | 15 +++------------ .../tools/jest-config/lib/config/projectRoot.js | 3 --- 6 files changed, 38 insertions(+), 21 deletions(-) create mode 100644 packages/foundation/tools/jest-config/lib/config/getProjectRoot.js delete mode 100644 packages/foundation/tools/jest-config/lib/config/projectRoot.js diff --git a/packages/foundation/tools/jest-config/lib/config/getJestJsProjectConfig.js b/packages/foundation/tools/jest-config/lib/config/getJestJsProjectConfig.js index f137317..0854eaa 100644 --- a/packages/foundation/tools/jest-config/lib/config/getJestJsProjectConfig.js +++ b/packages/foundation/tools/jest-config/lib/config/getJestJsProjectConfig.js @@ -14,7 +14,12 @@ function getJestJsProjectConfig( ) { const testMatch = getTestMatch(extension, false); - return getJestProjectConfig(projectName, testMatch, testPathIgnorePatterns); + return getJestProjectConfig( + projectName, + false, + testMatch, + testPathIgnorePatterns, + ); } export default getJestJsProjectConfig; diff --git a/packages/foundation/tools/jest-config/lib/config/getJestProjectConfig.js b/packages/foundation/tools/jest-config/lib/config/getJestProjectConfig.js index 978f4e5..fc6555b 100644 --- a/packages/foundation/tools/jest-config/lib/config/getJestProjectConfig.js +++ b/packages/foundation/tools/jest-config/lib/config/getJestProjectConfig.js @@ -1,15 +1,20 @@ -import projectRoot from './projectRoot.js'; +import getProjectRoot from './getProjectRoot.js'; /** * @param { !string } projectName Jest project's name + * @param { !boolean } isTargetingSource True if test are under the source folder * @param { !Array } testMatch Expressions to match to test file paths * @param { !Array } testPathIgnorePatterns Expressions to match to ignored file paths by jest * @returns { !import("@jest/types").Config.InitialProjectOptions } Jest config */ -function getJestProjectConfig(projectName, testMatch, testPathIgnorePatterns) { +function getJestProjectConfig( + projectName, + isTargetingSource, + testMatch, + testPathIgnorePatterns, +) { /** @type { !import("@jest/types").Config.InitialProjectOptions } */ const projectConfig = { - displayName: projectName, coveragePathIgnorePatterns: ['/fixtures/', '/node_modules/'], coverageThreshold: { global: { @@ -19,9 +24,10 @@ function getJestProjectConfig(projectName, testMatch, testPathIgnorePatterns) { statements: 70, }, }, + displayName: projectName, moduleFileExtensions: ['ts', 'js', 'json'], rootDir: '.', - roots: [projectRoot], + roots: [getProjectRoot(isTargetingSource)], testEnvironment: 'node', testMatch: testMatch, testPathIgnorePatterns: testPathIgnorePatterns, diff --git a/packages/foundation/tools/jest-config/lib/config/getJestTsProjectConfig.js b/packages/foundation/tools/jest-config/lib/config/getJestTsProjectConfig.js index 1f52c90..227d6fd 100644 --- a/packages/foundation/tools/jest-config/lib/config/getJestTsProjectConfig.js +++ b/packages/foundation/tools/jest-config/lib/config/getJestTsProjectConfig.js @@ -15,7 +15,12 @@ function getJestTsProjectConfig( const testMatch = getTestMatch(extension, true); return { - ...getJestProjectConfig(projectName, testMatch, testPathIgnorePatterns), + ...getJestProjectConfig( + projectName, + true, + testMatch, + testPathIgnorePatterns, + ), transform: { '^.+\\.ts?$': 'ts-jest', }, diff --git a/packages/foundation/tools/jest-config/lib/config/getProjectRoot.js b/packages/foundation/tools/jest-config/lib/config/getProjectRoot.js new file mode 100644 index 0000000..f599fef --- /dev/null +++ b/packages/foundation/tools/jest-config/lib/config/getProjectRoot.js @@ -0,0 +1,13 @@ +const projectRoot = ''; + +/** + * @param { !boolean } isTargetingSource True if test are under the source folder + * @returns {!string } Project root path + */ +export default function getProjectRoot(isTargetingSource) { + if (isTargetingSource) { + return `${projectRoot}/src`; + } else { + return `${projectRoot}/lib/cjs`; + } +} diff --git a/packages/foundation/tools/jest-config/lib/config/getTestMatch.js b/packages/foundation/tools/jest-config/lib/config/getTestMatch.js index 2438c66..c29e38d 100644 --- a/packages/foundation/tools/jest-config/lib/config/getTestMatch.js +++ b/packages/foundation/tools/jest-config/lib/config/getTestMatch.js @@ -1,4 +1,4 @@ -import projectRoot from './projectRoot.js'; +import getProjectRoot from './getProjectRoot.js'; /** * @param { !string } testExtension Test extension files @@ -6,18 +6,9 @@ import projectRoot from './projectRoot.js'; * @returns { !Array. } */ function getTestMatch(testExtension, isTargetingSource) { - let testMatch; + const projectRoot = getProjectRoot(isTargetingSource); - if (isTargetingSource) { - testMatch = [`${projectRoot}/src/**/*${testExtension}`]; - } else { - testMatch = [ - `${projectRoot}/{dist,lib}/*${testExtension}`, - `${projectRoot}/{dist,lib}/!(esm)/**/*${testExtension}`, - ]; - } - - return testMatch; + return [`${projectRoot}/**/*${testExtension}`]; } export default getTestMatch; diff --git a/packages/foundation/tools/jest-config/lib/config/projectRoot.js b/packages/foundation/tools/jest-config/lib/config/projectRoot.js deleted file mode 100644 index f303c08..0000000 --- a/packages/foundation/tools/jest-config/lib/config/projectRoot.js +++ /dev/null @@ -1,3 +0,0 @@ -const projectRoot = ''; - -export default projectRoot;