Skip to content

Jest Preset Default: Support self-referencing via exports - #80837

Merged
manzoorwanijk merged 1 commit into
trunkfrom
fix/jest-preset-default-self-referencing
Jul 30, 2026
Merged

Jest Preset Default: Support self-referencing via exports#80837
manzoorwanijk merged 1 commit into
trunkfrom
fix/jest-preset-default-self-referencing

Conversation

@manzoorwanijk

Copy link
Copy Markdown
Member

What?

Follow up to #80767.

Adds an exports field to @wordpress/jest-preset-default so the package can be resolved by its own name from within itself, and simplifies how test/unit/jest.config.js points at the preset.

Why?

The test added in #80767 does require( '@wordpress/jest-preset-default' ) from inside the package. That only resolves because npm hoists every workspace into the root node_modules. Under an isolated/linked install strategy there is no root link, and the test fails with Cannot find module '@wordpress/jest-preset-default' — see the failing run on #75814.

Node's self-referencing is the supported way to import a package from within itself, and it requires an exports field.

How?

Adds a minimal exports map exposing only the package root, jest-preset, and package.json.

The ./jest-preset entry is load-bearing. jest-config resolves a preset given as a bare package name by looking up the extensionless <pkg>/jest-preset subpath, and extensions are not appended to exports subpaths. A map without that entry breaks every consumer using preset: "@wordpress/jest-preset-default", and ./jest-preset.js does not work as a substitute.

Since the package root resolves to jest-preset.js (main, as of #80767), test/unit/jest.config.js can now pass the resolved path straight to preset instead of deriving the containing directory from a subpath import.

This narrows the published surface: scripts/ and index.js still ship via files but are no longer resolvable as deep imports. Nothing in the repo relies on them — jest-preset.js reaches scripts/ through relative require.resolve — but it is a breaking change for anyone deep-importing them, so it is noted in the changelog.

Testing Instructions

  1. npm run test:unit -- packages/jest-preset-default/test/index.js passes.
  2. npm run test:unit passes.
  3. Confirm downstream preset usage still works — in a scratch package with @wordpress/jest-preset-default linked and a config of { preset: "@wordpress/jest-preset-default" }, Jest resolves the preset and runs. Use a fresh --cacheDirectory; a warm Jest cache will mask preset resolution changes.

Use of AI Tools

Investigated and drafted with Claude Code (Opus). The exports shapes were verified empirically against jest-resolve and Node, and end to end with a standalone consumer package. Please adjust this disclosure to match your own review.

@manzoorwanijk
manzoorwanijk marked this pull request as draft July 29, 2026 10:27
@github-actions

github-actions Bot commented Jul 29, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: manzoorwanijk <manzoorwanijk@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@manzoorwanijk manzoorwanijk self-assigned this Jul 29, 2026
@manzoorwanijk manzoorwanijk added the [Type] Code Quality Issues or PRs that relate to code quality label Jul 29, 2026
@manzoorwanijk
manzoorwanijk requested review from aduth and ciampo July 29, 2026 10:28
@manzoorwanijk
manzoorwanijk marked this pull request as ready for review July 29, 2026 10:29
@manzoorwanijk
manzoorwanijk force-pushed the fix/jest-preset-default-self-referencing branch from 0efb3d0 to 88a1bfc Compare July 29, 2026 10:29
@manzoorwanijk
manzoorwanijk marked this pull request as draft July 29, 2026 10:29
@manzoorwanijk
manzoorwanijk marked this pull request as ready for review July 29, 2026 10:30
@github-actions

github-actions Bot commented Jul 29, 2026

Copy link
Copy Markdown

Size Change: -82 B (0%)

Total Size: 7.76 MB

📦 View Changed
Filename Size Change
build/scripts/upload-media/index.min.js 15.5 kB -82 B (-0.53%)

compressed-size-action

`packages/jest-preset-default/test/index.js` requires the package by its own
name, which only resolves while npm hoists every workspace into the root
`node_modules`. Add an `exports` field so Node's self-referencing applies.

The `./jest-preset` entry is required: `jest-config` resolves a bare preset
name through the extensionless `<pkg>/jest-preset` subpath, and extensions are
not appended to `exports` subpaths.

With the package root resolving to `jest-preset.js`, `test/unit/jest.config.js`
can pass the resolved path straight to `preset`.
@manzoorwanijk
manzoorwanijk force-pushed the fix/jest-preset-default-self-referencing branch from 88a1bfc to 90fc37d Compare July 30, 2026 07:25

@Mamaduka Mamaduka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Followed testing instructions and everything looks to be in order ✅

@manzoorwanijk
manzoorwanijk enabled auto-merge (squash) July 30, 2026 08:01
@manzoorwanijk
manzoorwanijk merged commit 1871c38 into trunk Jul 30, 2026
44 checks passed
@manzoorwanijk
manzoorwanijk deleted the fix/jest-preset-default-self-referencing branch July 30, 2026 08:02
@github-actions github-actions Bot added this to the Gutenberg 23.8 milestone Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Type] Code Quality Issues or PRs that relate to code quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants