Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions .pnpm-patches/@wordpress__block-editor.patch

This file was deleted.

12 changes: 0 additions & 12 deletions .pnpm-patches/@wordpress__components.patch

This file was deleted.

310 changes: 154 additions & 156 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

15 changes: 0 additions & 15 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,3 @@ patchedDependencies:
# until Jetpack's minimum WordPress version requests the @wordpress/build 0.26 list,
# even if upstream fixes init-module handling first.
"@wordpress/build": .pnpm-patches/@wordpress__build.patch

# The CommonJS build of 44.0.0 resolves `BaseControl` for `useBaseControlProps`
# through a circular require that rolldown snapshots before `index.cjs` has set
# its exports, so every CJS consumer (jest) renders ToggleGroupControl, and the
# DataViews view options built on it, with `useInstanceId( undefined )` and dies
# with "Invalid value used as weak map key". The ESM build is unaffected.
# Remove once the fix in WordPress/gutenberg (base-control hooks) ships.
"@wordpress/components@44.0.0": .pnpm-patches/@wordpress__components.patch

# Same rolldown CommonJS snapshot in block-editor 20.0.0: inner-blocks and the
# auto block preview memoize block-list exports at load, before that circular
# module has set them, so InnerBlocks renders in jest die with "Element type
# is invalid" and `memo()` warns about an undefined component.
# Remove once the fix in WordPress/gutenberg ships.
"@wordpress/block-editor@20.0.0": .pnpm-patches/@wordpress__block-editor.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Jest config: extend the shared transform allowlist instead of replacing it.


7 changes: 3 additions & 4 deletions projects/js-packages/charts/tests/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const path = require( 'path' );
const baseConfig = require( 'jetpack-js-tools/jest/config.base.js' );
const { transformIgnorePattern } = require( 'jetpack-js-tools/jest/transform-ignore.js' );

module.exports = {
...baseConfig,
Expand All @@ -14,10 +15,8 @@ module.exports = {
require.resolve
),
},
// Transform d3-* ESM packages. See config.base.js for the `(?!.*/node_modules/)` anchor.
transformIgnorePatterns: [
'/node_modules/(?!.*/node_modules/)(?!d3-|internmap/|uuid/|@wordpress/theme/)',
],
// d3-* and internmap are esm-only.
transformIgnorePatterns: [ transformIgnorePattern( [ 'd3-', 'internmap/' ] ) ],
setupFilesAfterEnv: [
...( baseConfig.setupFilesAfterEnv || [] ),
path.join( __dirname, 'setup-element-size-mock.js' ),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Jest: load @wordpress/components through its ESM entry like the shared resolver does.


Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

const path = require( 'path' );
const { resolveMapperTarget } = require( 'jetpack-js-tools/jest/jest-resolver.js' );

// Where the copies come from: this package's own dependencies, so the suite runs the versions it
// declares. DataViews' prerelease asks for newer `components`/`ui` than that, and does not get them
Expand Down Expand Up @@ -54,7 +55,7 @@ function singleCopyModuleMapper( rootDir ) {
for ( const name of DEDUPED_PACKAGES ) {
for ( const from of searchDirs ) {
try {
mapper[ `^${ escapeRegExp( name ) }$` ] = require.resolve( name, { paths: [ from ] } );
mapper[ `^${ escapeRegExp( name ) }$` ] = resolveMapperTarget( name, from );
break;
} catch {
// Not reachable from here; try the package root, then leave the import alone.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Jest: load @wordpress/components through its ESM entry like the shared resolver does.


3 changes: 2 additions & 1 deletion projects/plugins/boost/tests/jest.base.config.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const path = require( 'path' );
const coverageConfig = require( 'jetpack-js-tools/jest/config.coverage.js' );
const baseConfig = require( 'jetpack-js-tools/jest/config.base.js' );
const { resolveMapperTarget } = require( 'jetpack-js-tools/jest/jest-resolver.js' );

module.exports = {
rootDir: path.join( __dirname, '..' ),
Expand Down Expand Up @@ -34,7 +35,7 @@ module.exports = {
// The build externalizes this to a single `wp.components`, so shared React
// context works across packages. pnpm gives the plugin and the components
// package separate copies, which would break that context here.
'^@wordpress/components$': require.resolve( '@wordpress/components' ),
'^@wordpress/components$': resolveMapperTarget( '@wordpress/components', __dirname ),
'^\\$lib/(.*)$': '<rootDir>/app/assets/src/js/lib/$1',
'^\\$features/(.*)$': '<rootDir>/app/assets/src/js/features/$1',
'^\\$layout/(.*)$': '<rootDir>/app/assets/src/js/layout/$1',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: other
Comment: Jest config: inherit the shared transform allowlist instead of replacing it.


8 changes: 0 additions & 8 deletions projects/plugins/jetpack/tests/jest.config.gui.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ module.exports = {
],
setupFilesAfterEnv: [ ...baseConfig.setupFilesAfterEnv, '<rootDir>/tests/jest-globals.gui.js' ],
coverageDirectory: baseConfig.coverageDirectory + '/gui',
// This is necessary to allow css from uplot, @wordpress/admin-ui, and @gravatar-com (the
// latter for the lifted Gravatar component's hovercard styles) to be imported, and
// @wordpress/theme's untranspiled ESM to be transformed (mirroring the base config —
// this override otherwise shadows the base pattern's exception).
transformIgnorePatterns: [
'/node_modules/(?!.*/node_modules/)(?!@automattic/|uuid/|@wordpress/theme/|uplot/.*\\.css|@wordpress/admin-ui/.*\\.css|@gravatar-com/.*\\.css)',
...baseConfig.transformIgnorePatterns,
],
collectCoverageFrom: [
'<rootDir>/_inc/**/*.{js,mjs,cjs,jsx,ts,tsx,mts,cts}',
'<rootDir>/3rd-party/**/*.{js,mjs,cjs,jsx,ts,tsx,mts,cts}',
Expand Down
13 changes: 2 additions & 11 deletions tools/js-tools/jest/config.base.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const path = require( 'path' );
const nodeConfig = require( './config.node.js' );
const { transformIgnorePattern } = require( './transform-ignore.js' );

module.exports = {
...nodeConfig,
Expand Down Expand Up @@ -29,17 +30,7 @@ module.exports = {
},
],
},
// Unignore certain node_modules
// - uplot: for packages/components
// - @wordpress/admin-ui: for the unified admin page header styles
// - @wordpress/theme: esm-only in 1.0.0
// - @gravatar-com: for the lifted Gravatar component's hovercard styles
// - marked: esm-only
// - uuid: v14 went esm-only, so it needs transforming
// `(?!.*/node_modules/)` handles pnpm's store with nested `node_modules` dirs.
transformIgnorePatterns: [
'/node_modules/(?!.*/node_modules/)(?!marked/|uuid/|@wordpress/theme/|uplot/.*\\.css|@wordpress/admin-ui/.*\\.css|@gravatar-com/.*\\.css)',
],
transformIgnorePatterns: [ transformIgnorePattern() ],
moduleNameMapper: {
jetpackConfig: path.join( __dirname, 'jest-jetpack-config.js' ),
},
Expand Down
44 changes: 44 additions & 0 deletions tools/js-tools/jest/jest-resolver.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { dirname, join } = require( 'path' );

// Some packages assume that a "browser" environment is esm or otherwise break in node.
// List them here and the resolver will adjust the conditions to resolve them as "node" instead.
// cf. https://github.com/microsoft/accessibility-insights-web/pull/5421#issuecomment-1109168149
Expand All @@ -6,6 +8,23 @@ const badBrowserPackages = new Set( [
'parsel-js',
] );

/**
* Load these packages as ESM: their CommonJS builds snapshot a circular
* import as `undefined`. `transform-ignore.js` lets babel-jest transpile them.
* Remove once WordPress/gutenberg#86322 and #86323 ship.
*
* @see https://github.com/WordPress/gutenberg/pull/86322
* @see https://github.com/WordPress/gutenberg/pull/86323
*/
const forceEsmPackages = new Set( [ '@wordpress/components', '@wordpress/block-editor' ] );

/**
* Custom jest resolver, adjusting resolution for some packages that break in jest.
*
* @param {string} path - Path to the module.
* @param {object} options - Jest resolver options, including `defaultResolver` and `conditions`.
* @return {string} Resolved path.
*/
module.exports = ( path, options ) => {
const basedir = options.basedir;
const conditions = options.conditions ? new Set( options.conditions ) : options.conditions;
Expand All @@ -19,6 +38,10 @@ module.exports = ( path, options ) => {
conditions.delete( 'browser' );
conditions.add( 'node' );
}
if ( conditions && forceEsmPackages.has( pkg ) ) {
conditions.delete( 'require' );
conditions.add( 'import' );
}

const resolver = options.defaultResolver;
const opts = {
Expand All @@ -29,3 +52,24 @@ module.exports = ( path, options ) => {

return resolver( path, opts );
};

/**
* Resolve the file a `moduleNameMapper` entry should point at.
*
* A mapped path skips the resolver above, so `forceEsmPackages` take their
* `import` entry here. Remove with that list, back to `require.resolve`.
*
* @param {string} name - Package name.
* @param {string} from - Directory to resolve from.
* @return {string} Absolute path of the module.
*/
module.exports.resolveMapperTarget = ( name, from ) => {
if ( forceEsmPackages.has( name ) ) {
const packageJson = require.resolve( `${ name }/package.json`, { paths: [ from ] } );
const entry = require( packageJson ).exports?.[ '.' ]?.import;
if ( typeof entry === 'string' ) {
return join( dirname( packageJson ), entry );
}
}
return require.resolve( name, { paths: [ from ] } );
};
38 changes: 38 additions & 0 deletions tools/js-tools/jest/transform-ignore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* The `transformIgnorePatterns` entry shared by every jest config.
*
* Jest ignores a file when any pattern matches, so a config cannot add
* exceptions by appending a second pattern; it has to extend this one.
*/

/** Paths under `node_modules` that babel-jest must transform anyway. */
const TRANSFORMED_NODE_MODULES = [
// esm-only
'marked/',
'uuid/',
'@wordpress/theme/',
// loaded as ESM through jest-resolver.js; remove with its forceEsmPackages
'@wordpress/components/',
'@wordpress/block-editor/',
// stylesheets imported by components
'uplot/.*\\.css',
'@wordpress/admin-ui/.*\\.css',
'@gravatar-com/.*\\.css',
];

/**
* Build the pattern, with extra exceptions for one project.
*
* The first lookahead keeps the match on the outermost `node_modules`, so a
* nested copy in pnpm's store is judged by the same exceptions.
*
* @param {string[]} extras - Regex fragments for more paths to transform.
* @return {string} Pattern for `transformIgnorePatterns`.
*/
function transformIgnorePattern( extras = [] ) {
return `/node_modules/(?!.*/node_modules/)(?!${ [ ...TRANSFORMED_NODE_MODULES, ...extras ].join(
'|'
) })`;
}

module.exports = { TRANSFORMED_NODE_MODULES, transformIgnorePattern };
Loading