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 `<rootDir>` 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.
This commit is contained in:
Christopher Allford 2023-12-12 09:58:13 -08:00 committed by GitHub
parent 7692fa5430
commit 4e89debd0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 120 additions and 58 deletions

View File

@ -1,4 +1,7 @@
{
"rootDir": "./src",
"preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js"
"rootDir": "./",
"roots": [
"<rootDir>/src"
],
"preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js"
}

View File

@ -1,7 +1,9 @@
module.exports = {
preset: 'ts-jest',
verbose: true,
rootDir: 'src',
rootDir: './',
roots: [
'<rootDir>/src',
],
testEnvironment: 'node',
testPathIgnorePatterns: [ '/node_modules/', '/dist/' ],
};

View File

@ -1,4 +1,7 @@
{
"rootDir": "./src",
"preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js"
"rootDir": "./",
"roots": [
"<rootDir>/src"
],
"preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js"
}

View File

@ -1,4 +1,7 @@
{
"rootDir": "./src",
"preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js"
"rootDir": "./",
"roots": [
"<rootDir>/src"
],
"preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js"
}

View File

@ -1,4 +1,7 @@
{
"rootDir": "./src",
"preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js"
"rootDir": "./",
"roots": [
"<rootDir>/src"
],
"preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js"
}

View File

@ -1,4 +1,7 @@
{
"rootDir": "./src",
"preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js"
"rootDir": "./",
"roots": [
"<rootDir>/src"
],
"preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js"
}

View File

@ -1,4 +1,7 @@
{
"rootDir": "./src",
"preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js"
"rootDir": "./",
"roots": [
"<rootDir>/src"
],
"preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js"
}

View File

@ -1,4 +1,7 @@
{
"rootDir": "./src",
"preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js"
"rootDir": "./",
"roots": [
"<rootDir>/src"
],
"preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js"
}

View File

@ -1,4 +1,7 @@
{
"rootDir": "./src",
"preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js"
"rootDir": "./",
"roots": [
"<rootDir>/src"
],
"preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js"
}

View File

@ -1,4 +1,7 @@
{
"rootDir": "./src",
"preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js"
"rootDir": "./",
"roots": [
"<rootDir>/src"
],
"preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js"
}

View File

@ -1,4 +1,7 @@
{
"rootDir": "./src",
"preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js"
"rootDir": "./",
"roots": [
"<rootDir>/src"
],
"preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js"
}

View File

@ -1,4 +1,7 @@
{
"rootDir": "./src",
"preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js"
"rootDir": "./",
"roots": [
"<rootDir>/src"
],
"preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js"
}

View File

@ -1,4 +1,7 @@
{
"rootDir": "./src",
"preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js"
"rootDir": "./",
"roots": [
"<rootDir>/src"
],
"preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js"
}

View File

@ -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/',
'<rootDir>/.*/build/',
'<rootDir>/.*/build-module/',
'<rootDir>/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,

View File

@ -1,4 +1,7 @@
{
"rootDir": "./src",
"preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js"
"rootDir": "./",
"roots": [
"<rootDir>/src"
],
"preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js"
}

View File

@ -1,4 +1,7 @@
{
"rootDir": "./src",
"preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js"
"rootDir": "./",
"roots": [
"<rootDir>/src"
],
"preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js"
}

View File

@ -1,4 +1,7 @@
{
"rootDir": "./src",
"preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js"
"rootDir": "./",
"roots": [
"<rootDir>/src"
],
"preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js"
}

View File

@ -1,4 +1,7 @@
{
"rootDir": "./src",
"preset": "../node_modules/@woocommerce/internal-js-tests/jest-preset.js"
"rootDir": "./",
"roots": [
"<rootDir>/src"
],
"preset": "./node_modules/@woocommerce/internal-js-tests/jest-preset.js"
}

View File

@ -1,6 +1,7 @@
module.exports = {
rootDir: './',
preset: '../node_modules/@woocommerce/internal-js-tests/jest-preset.js',
rootDir: '../',
roots: [ '<rootDir>/client' ],
preset: './node_modules/@woocommerce/internal-js-tests/jest-preset.js',
globals: {
'ts-jest': {
diagnostics: {
@ -11,7 +12,7 @@ module.exports = {
18003,
],
},
tsconfig: '<rootDir>/tsconfig.test.json',
tsconfig: '<rootDir>/client/tsconfig.test.json',
},
},
};

View File

@ -2,5 +2,8 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: [ '<rootDir>/dist/', '<rootDir>/node_modules/' ],
rootDir: './',
roots: [
'<rootDir>/src',
],
};