Skip to content

refactor: migrate tests/unit to npm workspace @wordpress/unit-tests#77063

Merged
manzoorwanijk merged 13 commits intoWordPress:trunkfrom
USERSATOSHI:try/migrate-unit-tests-to-workspace
Apr 21, 2026
Merged

refactor: migrate tests/unit to npm workspace @wordpress/unit-tests#77063
manzoorwanijk merged 13 commits intoWordPress:trunkfrom
USERSATOSHI:try/migrate-unit-tests-to-workspace

Conversation

@USERSATOSHI
Copy link
Copy Markdown
Contributor

What?

Part of #75041

Why?

The issue provides more context, but in short, this PR ensures the workspace package does not rely on root dependencies once dependency isolation is enforced.

How?

This PR converts tests/unit into a new workspace @wordpress/unit-tests while still relating the functionality.

Testing Instructions

Testing Instructions for Keyboard

run all npm run unit:* scripts.

Comment thread .github/workflows/unit-test.yml
Read IS_GUTENBERG_PLUGIN directly from root package.json instead of
relying
on npm_package_config_* env vars, which are unreliable in npm
workspaces.
Set globals early in Jest config to ensure correct module-level guards.
@USERSATOSHI USERSATOSHI marked this pull request as ready for review April 13, 2026 11:45
@github-actions
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: USERSATOSHI <tusharbharti@git.wordpress.org>
Co-authored-by: manzoorwanijk <manzoorwanijk@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 added the [Type] Code Quality Issues or PRs that relate to code quality label Apr 13, 2026
Copy link
Copy Markdown
Member

@manzoorwanijk manzoorwanijk left a comment

Choose a reason for hiding this comment

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

Thank you for working on this. Let us clean things up a bit.

Comment thread test/unit/package.json Outdated
Comment thread test/unit/tsconfig.json Outdated
Comment thread package.json
@manzoorwanijk
Copy link
Copy Markdown
Member

Claude Code review

Migration changes look correct

  • New test/unit/package.json workspace correctly hosts @emotion/jest, @testing-library/react, @testing-library/user-event, jest-jasmine2, jest-junit, resize-observer-polyfill (plus duplicates of jest, babel-jest, etc. that root still needs).
  • test/unit/jest.config.js chdir's to repo root so glob('packages/*/src/...') and Babel config resolve correctly.

Unused unit-test deps still in root devDependencies

These exist only in root package.json declarations — no root code or script references them, and any consumer is a workspace package that already declares its own copy:

  1. jest-environment-jsdom — only used by packages/scripts, packages/react-native-editor, and now test/unit. No root usage.
  2. jest-message-util — only consumed by packages/report-flaky-tests/src/markdown.ts, and that package declares it.
  3. snapshot-diff — only used inside test/unit/config/matchers/to-match-style-diff-snapshot.js and packages/components (which declares it). The only root reference is a doc-comment URL in typings/gutenberg-test-env/index.d.ts.

Safe to drop from root.

Ones to leave in root

  • jest, babel-jest, jest-watch-typeahead — still used by test:native (root invokes jest directly against test/native/jest.config.js, which references babel-jest and jest-watch-typeahead/*).
  • @testing-library/jest-dom — imported by packages/block-library/src/utils/test/waveform-utils.js but packages/block-library/package.json does not declare it. It's a phantom dep currently leaning on the root-hoisted version. Either keep the root entry or (better) add it to block-library's own devDependencies and then drop root.

Loose ends in this branch

  • test:unit:profile in root package.json was not migrated and still runs wp-scripts test-unit-js --config test/unit/jest.config.js from root. With the deps removed from root this likely breaks; migrate it to the workspace like the sibling scripts.
  • fixtures:generate does npm run test:unit test/integration/full-content/ — passing a positional path arg without --. Worth verifying the path still forwards through the new npm run --workspace @wordpress/unit-tests test:unit indirection (npm 7+ generally forwards trailing args, but it's worth one manual run to confirm).
  • The @emotion/jest and jest-junit entries removed from root are fine only because they get hoisted from test/unit's install. But test/native/jest.config.js references @emotion/jest/serializer and jest-junit from a root-invoked jest run — so root now has phantom deps on packages declared by an unrelated workspace. Hoisting will paper over it locally, but it's fragile. Cleanest fix: declare them in root devDependencies (or migrate test:native to its own workspace too).

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR migrates the existing test/unit Jest setup into a dedicated npm workspace package (@wordpress/unit-tests) so unit-test tooling and dependencies can be isolated from the monorepo root, in line with the broader workspace conversion effort.

Changes:

  • Added test/unit/package.json to define the new private workspace @wordpress/unit-tests and its test scripts/dependencies.
  • Updated test/unit/jest.config.js to resolve repo-root paths correctly when running from the workspace context.
  • Updated root package.json scripts and workspaces to run unit tests via the new workspace and adjusted related dependency placements/lockfile.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.

File Description
test/unit/package.json Introduces the @wordpress/unit-tests workspace package definition and unit test scripts/dependencies.
test/unit/jest.config.js Adjusts root path handling for workspace execution (repo-root resolution and globbing).
package.json Routes unit-test scripts through the @wordpress/unit-tests workspace and updates workspaces/deps accordingly.
package-lock.json Reflects workspace addition and dependency graph changes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread test/unit/package.json
Copy link
Copy Markdown
Member

@manzoorwanijk manzoorwanijk left a comment

Choose a reason for hiding this comment

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

Thank you for working on this.

Next, we have test/native and test/performance, which would help us further clean up the root package.json.

@manzoorwanijk manzoorwanijk enabled auto-merge (squash) April 21, 2026 04:41
@manzoorwanijk manzoorwanijk merged commit 78321b3 into WordPress:trunk Apr 21, 2026
49 of 52 checks passed
@github-actions github-actions Bot added this to the Gutenberg 23.1 milestone Apr 21, 2026
@Mamaduka
Copy link
Copy Markdown
Member

It looks like some of the unit tests are failing on trunk after this got merged https://github.com/WordPress/gutenberg/actions/runs/24705229142/job/72256949732.

@manzoorwanijk
Copy link
Copy Markdown
Member

It looks like some of the unit tests are failing on trunk after this got merged https://github.com/WordPress/gutenberg/actions/runs/24705229142/job/72256949732.

Looking into it. It's strange that PR is green but trunk fails 😄

@manzoorwanijk
Copy link
Copy Markdown
Member

OK, they are failing on Node 24, which we don't run for PRs. I think it's better to run that for PRs as well.

CC: @aduth

@Mamaduka
Copy link
Copy Markdown
Member

Yeah, happens from time to time. We run a limited unit test matrix on PRs to save some time.

@manzoorwanijk
Copy link
Copy Markdown
Member

#77512 fixes it. I verified it by temporarily running the CI checks on v24 on that PR.

ciampo pushed a commit that referenced this pull request Apr 21, 2026
Co-authored-by: USERSATOSHI <tusharbharti@git.wordpress.org>
Co-authored-by: manzoorwanijk <manzoorwanijk@git.wordpress.org>
@alecgeatches
Copy link
Copy Markdown
Contributor

alecgeatches commented Apr 21, 2026

Note here: because npm run test:unit now delegates to another internal npm command, using -- to pass arguments to the test runner doesn't work the same:

# Ignores --testPathPattern, runs all tests:

npm run test:unit -- --testPathPattern='test/crdt.ts'

Instead, you either need to pass -- -- before additional options or copy the workspace out of the test command:

npm run test:unit -- -- --testPathPattern='test/crdt.ts'
# or
npm run --workspace @wordpress/unit-tests test:unit -- --testPathPattern='test/crdt.ts'

It seems like this was already noticed in this PR and a similar workaround was added to the fixtures:generate script.

@Mamaduka
Copy link
Copy Markdown
Member

I think a similar fix needs to be applied here - #77083. That said, we need to test this workflow migration PRs more thoroughly.

@manzoorwanijk
Copy link
Copy Markdown
Member

Thank you for the catch. I have created #77541 to fix it.

manzoorwanijk added a commit that referenced this pull request Apr 22, 2026
Follow-up to #77063. After the test/unit workspace conversion, flags
passed via `npm run test:unit -- ...` were swallowed by the nested
`npm run --workspace` call. For example:

    npm run test:unit -- --testPathPattern='test/crdt.ts'

…ran the entire suite instead of filtering.

Append a trailing `--` to each workspace-delegating script so npm
forwards all subsequent user args (both flags and positionals) to the
workspace command untouched. Also drop the `-- --` workaround in
`fixtures:generate` since a single `--` now works end-to-end.
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.

5 participants