diff --git a/test/unit/jest.config.js b/test/unit/jest.config.js index 9df9e66c98545e..d60d797b4f86ca 100644 --- a/test/unit/jest.config.js +++ b/test/unit/jest.config.js @@ -21,19 +21,48 @@ const transpiledPackageNames = glob( return relative.split( path.sep )[ 1 ]; } ); +const dependenciesToTransform = [ + '@ariakit/test', + '@ariakit/utils', + '@preact', + 'comctx', + 'docker-compose', + 'marked', + 'parsel-js', + 'preact', + 'uuid', + 'yaml', +]; + // Make sure the tests run in UTC timezone, regardless of the system timezone. process.env.TZ = 'UTC'; +/* + * Resolved rather than hardcoded to `/node_modules`, + * which is empty under non-hoisting installs. + */ +const ariakitTestDir = path.dirname( + require.resolve( '@ariakit/test/package.json', { + paths: [ path.join( ROOT_DIR, 'packages/components' ) ], + } ) +); +const ariakitUtilsDir = path.dirname( + require.resolve( '@ariakit/utils/package.json', { + paths: [ ariakitTestDir ], + } ) +); + module.exports = { - rootDir: '../../', + rootDir: ROOT_DIR, moduleNameMapper: { + /** + * Specific mappings first (before generic patterns) + */ // Jest resolves dependencies from CommonJS and cannot select import-only // package exports. Map Ariakit's ESM test helpers explicitly. - '^@ariakit/test$': '/node_modules/@ariakit/test/dist/index.js', - '^@ariakit/test/react$': - '/node_modules/@ariakit/test/dist/react.js', - '^@ariakit/utils$': - '/node_modules/@ariakit/utils/dist/index.js', + '^@ariakit/test$': path.join( ariakitTestDir, 'dist/index.js' ), + '^@ariakit/test/react$': path.join( ariakitTestDir, 'dist/react.js' ), + '^@ariakit/utils$': path.join( ariakitUtilsDir, 'dist/index.js' ), // Mock @wordpress/vips/worker before the general pattern so it doesn't try to load the real file. // The worker-code.ts file is auto-generated during full builds and is gitignored. '@wordpress/vips/worker': @@ -42,15 +71,20 @@ module.exports = { // The worker-code.ts file is auto-generated during full builds and is gitignored. '@wordpress/video-conversion/worker': '/test/unit/config/video-conversion-worker-code-stub.js', - [ `@wordpress\\/(${ transpiledPackageNames.join( '|' ) })$` ]: - 'packages/$1/src', '@wordpress/theme/design-tokens.js': '/packages/theme/prebuilt/js/design-tokens.mjs', '@wordpress/block-library/build-module/(.*).mjs': '/packages/block-library/src/$1.js', '.+\\.wasm$': '/test/unit/config/wasm-stub.js', + // Map deep paths (e.g., @wordpress/block-editor/src/hooks/list-view) + [ `@wordpress\\/(${ transpiledPackageNames.join( '|' ) })\\/(.+)$` ]: + 'packages/$1/$2', + // Then map exact package imports (e.g., @wordpress/compose) + [ `@wordpress\\/(${ transpiledPackageNames.join( '|' ) })$` ]: + 'packages/$1/src', }, preset: require.resolve( '@wordpress/jest-preset-default' ), + testEnvironment: require.resolve( 'jest-environment-jsdom' ), setupFiles: [ '/test/unit/config/global-mocks.js', '/test/unit/config/gutenberg-env.js', @@ -78,7 +112,7 @@ module.exports = { '^.+\\.m?[jt]sx?$': '/test/unit/scripts/babel-transformer.js', }, transformIgnorePatterns: [ - '/node_modules/(?!(docker-compose|yaml|preact|@preact|parsel-js|comctx|uuid|marked|@ariakit/(test|utils))/)', + `/node_modules/(?!(${ dependenciesToTransform.join( '|' ) })/)`, '\\.pnp\\.[^\\/]+$', ], snapshotSerializers: [