From 4e89debd0c3b778544decf2bfbe90158ec151d04 Mon Sep 17 00:00:00 2001 From: Christopher Allford <6451942+ObliviousHarmony@users.noreply.github.com> Date: Tue, 12 Dec 2023 09:58:13 -0800 Subject: [PATCH] Fix Jest Preset (#42707) When using a preset we need to keep in mind that the transformation paths are relative to the preset, not the consuming package. We get around this by using `` in the transform paths. However, doing this means fixing the root directory for all of the jest tests. This keeps the tests working in the same way but lets us fix the preset too. --- packages/js/ai/jest.config.json | 7 ++- packages/js/api/jest.config.js | 6 ++- packages/js/block-templates/jest.config.json | 7 ++- packages/js/components/jest.config.json | 7 ++- packages/js/csv-export/jest.config.json | 7 ++- packages/js/currency/jest.config.json | 7 ++- .../js/customer-effort-score/jest.config.json | 7 ++- packages/js/data/jest.config.json | 7 ++- packages/js/date/jest.config.json | 7 ++- packages/js/experimental/jest.config.json | 7 ++- packages/js/explat/jest.config.json | 7 ++- .../js/expression-evaluation/jest.config.json | 7 ++- packages/js/integrate-plugin/jest.config.json | 7 ++- packages/js/internal-js-tests/jest-preset.js | 48 +++++++++++-------- packages/js/navigation/jest.config.json | 7 ++- packages/js/number/jest.config.json | 7 ++- packages/js/onboarding/jest.config.json | 7 ++- packages/js/product-editor/jest.config.json | 7 ++- .../woocommerce-admin/client/jest.config.js | 7 +-- tools/monorepo-utils/jest.config.js | 5 +- 20 files changed, 120 insertions(+), 58 deletions(-) diff --git a/packages/js/ai/jest.config.json b/packages/js/ai/jest.config.json index 3d8108048f6..fa3347efcc7 100644 --- a/packages/js/ai/jest.config.json +++ b/packages/js/ai/jest.config.json @@ -1,4 +1,7 @@ { - "rootDir": "./src", - "preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js" + "rootDir": "./", + "roots": [ + "/src" + ], + "preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js" } diff --git a/packages/js/api/jest.config.js b/packages/js/api/jest.config.js index 6ee18469911..8c846efcfdd 100644 --- a/packages/js/api/jest.config.js +++ b/packages/js/api/jest.config.js @@ -1,7 +1,9 @@ module.exports = { preset: 'ts-jest', verbose: true, - rootDir: 'src', + rootDir: './', + roots: [ + '/src', + ], testEnvironment: 'node', - testPathIgnorePatterns: [ '/node_modules/', '/dist/' ], }; diff --git a/packages/js/block-templates/jest.config.json b/packages/js/block-templates/jest.config.json index 3d8108048f6..fa3347efcc7 100644 --- a/packages/js/block-templates/jest.config.json +++ b/packages/js/block-templates/jest.config.json @@ -1,4 +1,7 @@ { - "rootDir": "./src", - "preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js" + "rootDir": "./", + "roots": [ + "/src" + ], + "preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js" } diff --git a/packages/js/components/jest.config.json b/packages/js/components/jest.config.json index 3d8108048f6..fa3347efcc7 100644 --- a/packages/js/components/jest.config.json +++ b/packages/js/components/jest.config.json @@ -1,4 +1,7 @@ { - "rootDir": "./src", - "preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js" + "rootDir": "./", + "roots": [ + "/src" + ], + "preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js" } diff --git a/packages/js/csv-export/jest.config.json b/packages/js/csv-export/jest.config.json index 3d8108048f6..fa3347efcc7 100644 --- a/packages/js/csv-export/jest.config.json +++ b/packages/js/csv-export/jest.config.json @@ -1,4 +1,7 @@ { - "rootDir": "./src", - "preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js" + "rootDir": "./", + "roots": [ + "/src" + ], + "preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js" } diff --git a/packages/js/currency/jest.config.json b/packages/js/currency/jest.config.json index 3d8108048f6..fa3347efcc7 100644 --- a/packages/js/currency/jest.config.json +++ b/packages/js/currency/jest.config.json @@ -1,4 +1,7 @@ { - "rootDir": "./src", - "preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js" + "rootDir": "./", + "roots": [ + "/src" + ], + "preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js" } diff --git a/packages/js/customer-effort-score/jest.config.json b/packages/js/customer-effort-score/jest.config.json index 3d8108048f6..fa3347efcc7 100644 --- a/packages/js/customer-effort-score/jest.config.json +++ b/packages/js/customer-effort-score/jest.config.json @@ -1,4 +1,7 @@ { - "rootDir": "./src", - "preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js" + "rootDir": "./", + "roots": [ + "/src" + ], + "preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js" } diff --git a/packages/js/data/jest.config.json b/packages/js/data/jest.config.json index 3d8108048f6..fa3347efcc7 100644 --- a/packages/js/data/jest.config.json +++ b/packages/js/data/jest.config.json @@ -1,4 +1,7 @@ { - "rootDir": "./src", - "preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js" + "rootDir": "./", + "roots": [ + "/src" + ], + "preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js" } diff --git a/packages/js/date/jest.config.json b/packages/js/date/jest.config.json index 3d8108048f6..fa3347efcc7 100644 --- a/packages/js/date/jest.config.json +++ b/packages/js/date/jest.config.json @@ -1,4 +1,7 @@ { - "rootDir": "./src", - "preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js" + "rootDir": "./", + "roots": [ + "/src" + ], + "preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js" } diff --git a/packages/js/experimental/jest.config.json b/packages/js/experimental/jest.config.json index 3d8108048f6..fa3347efcc7 100644 --- a/packages/js/experimental/jest.config.json +++ b/packages/js/experimental/jest.config.json @@ -1,4 +1,7 @@ { - "rootDir": "./src", - "preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js" + "rootDir": "./", + "roots": [ + "/src" + ], + "preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js" } diff --git a/packages/js/explat/jest.config.json b/packages/js/explat/jest.config.json index 3d8108048f6..fa3347efcc7 100644 --- a/packages/js/explat/jest.config.json +++ b/packages/js/explat/jest.config.json @@ -1,4 +1,7 @@ { - "rootDir": "./src", - "preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js" + "rootDir": "./", + "roots": [ + "/src" + ], + "preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js" } diff --git a/packages/js/expression-evaluation/jest.config.json b/packages/js/expression-evaluation/jest.config.json index 3d8108048f6..fa3347efcc7 100644 --- a/packages/js/expression-evaluation/jest.config.json +++ b/packages/js/expression-evaluation/jest.config.json @@ -1,4 +1,7 @@ { - "rootDir": "./src", - "preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js" + "rootDir": "./", + "roots": [ + "/src" + ], + "preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js" } diff --git a/packages/js/integrate-plugin/jest.config.json b/packages/js/integrate-plugin/jest.config.json index 3d8108048f6..fa3347efcc7 100644 --- a/packages/js/integrate-plugin/jest.config.json +++ b/packages/js/integrate-plugin/jest.config.json @@ -1,4 +1,7 @@ { - "rootDir": "./src", - "preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js" + "rootDir": "./", + "roots": [ + "/src" + ], + "preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js" } diff --git a/packages/js/internal-js-tests/jest-preset.js b/packages/js/internal-js-tests/jest-preset.js index f9b1046316e..ef55ec5f8d0 100644 --- a/packages/js/internal-js-tests/jest-preset.js +++ b/packages/js/internal-js-tests/jest-preset.js @@ -4,14 +4,14 @@ const path = require( 'path' ); // These modules need to be transformed because they are not transpiled to CommonJS. -const transformModules = [ 'is-plain-obj' ]; -// Ignore all node_modules except for the ones we need to transform. -const transformIgnorePatterns = [ - `node_modules/(?!.pnpm/${ transformModules.join( - '|.pnpm/' - ) }|${ transformModules.join( '|' ) })`, - '/build/', -]; +// The top-level keys are the names of the packages and the values are the file +// regexes that need to be transformed. Note that these are relative to the +// package root and should be treated as such. +const transformModules = { + 'is-plain-obj': { + 'index\\.js$': 'babel-jest', + }, +}; module.exports = { moduleNameMapper: { @@ -35,8 +35,8 @@ module.exports = { 'build/mocks/style-mock.js' ), // Force some modulse to resolve with the CJS entry point, because Jest does not support package.json.exports. - 'uuid': require.resolve('uuid'), - 'memize': require.resolve('memize'), + uuid: require.resolve( 'uuid' ), + memize: require.resolve( 'memize' ), }, restoreMocks: true, setupFiles: [ @@ -51,17 +51,25 @@ module.exports = { '**/test/*.[jt]s?(x)', '**/?(*.)test.[jt]s?(x)', ], - testPathIgnorePatterns: [ - '/node_modules/', - '/.*/build/', - '/.*/build-module/', - '/tests/e2e/', + // The keys for the transformed modules contains the name of the packages that should be transformed. + transformIgnorePatterns: [ + 'node_modules/(?!(?:\\.pnpm|' + Object.keys( transformModules ).join( '|' ) + ')/)', + __dirname ], - transformIgnorePatterns, - transform: { - '^.+\\is-plain-obj/index\\.js$': 'babel-jest', - '^.+\\.[jt]sx?$': 'ts-jest', - }, + // The values for the transformed modules contain an object with the transforms to apply. + transform: Object.entries( transformModules ).reduce( + ( acc, [ moduleName, transform ] ) => { + for ( const key in transform ) { + acc[ `node_modules/${ moduleName }/${ key }` ] = + transform[ key ]; + } + + return acc; + }, + { + '(?:src|client|assets/js)/.*\\.[jt]sx?$': 'ts-jest', + } + ), testEnvironment: 'jest-environment-jsdom', timers: 'modern', verbose: true, diff --git a/packages/js/navigation/jest.config.json b/packages/js/navigation/jest.config.json index 3d8108048f6..fa3347efcc7 100644 --- a/packages/js/navigation/jest.config.json +++ b/packages/js/navigation/jest.config.json @@ -1,4 +1,7 @@ { - "rootDir": "./src", - "preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js" + "rootDir": "./", + "roots": [ + "/src" + ], + "preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js" } diff --git a/packages/js/number/jest.config.json b/packages/js/number/jest.config.json index 3d8108048f6..fa3347efcc7 100644 --- a/packages/js/number/jest.config.json +++ b/packages/js/number/jest.config.json @@ -1,4 +1,7 @@ { - "rootDir": "./src", - "preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js" + "rootDir": "./", + "roots": [ + "/src" + ], + "preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js" } diff --git a/packages/js/onboarding/jest.config.json b/packages/js/onboarding/jest.config.json index 3d8108048f6..fa3347efcc7 100644 --- a/packages/js/onboarding/jest.config.json +++ b/packages/js/onboarding/jest.config.json @@ -1,4 +1,7 @@ { - "rootDir": "./src", - "preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js" + "rootDir": "./", + "roots": [ + "/src" + ], + "preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js" } diff --git a/packages/js/product-editor/jest.config.json b/packages/js/product-editor/jest.config.json index 3d8108048f6..fa3347efcc7 100644 --- a/packages/js/product-editor/jest.config.json +++ b/packages/js/product-editor/jest.config.json @@ -1,4 +1,7 @@ { - "rootDir": "./src", - "preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js" + "rootDir": "./", + "roots": [ + "/src" + ], + "preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js" } diff --git a/plugins/woocommerce-admin/client/jest.config.js b/plugins/woocommerce-admin/client/jest.config.js index 2532b7004c2..b099b037c80 100644 --- a/plugins/woocommerce-admin/client/jest.config.js +++ b/plugins/woocommerce-admin/client/jest.config.js @@ -1,6 +1,7 @@ module.exports = { - rootDir: './', - preset: '../node_modules/@woocommerce/internal-js-tests/jest-preset.js', + rootDir: '../', + roots: [ '/client' ], + preset: './node_modules/@woocommerce/internal-js-tests/jest-preset.js', globals: { 'ts-jest': { diagnostics: { @@ -11,7 +12,7 @@ module.exports = { 18003, ], }, - tsconfig: '/tsconfig.test.json', + tsconfig: '/client/tsconfig.test.json', }, }, }; diff --git a/tools/monorepo-utils/jest.config.js b/tools/monorepo-utils/jest.config.js index da5d4c982ec..242609e6df3 100644 --- a/tools/monorepo-utils/jest.config.js +++ b/tools/monorepo-utils/jest.config.js @@ -2,5 +2,8 @@ module.exports = { preset: 'ts-jest', testEnvironment: 'node', - testPathIgnorePatterns: [ '/dist/', '/node_modules/' ], + rootDir: './', + roots: [ + '/src', + ], };