Skip to content

Add PHPUnit tests for script module collision behavior#48095

Open
retrofox wants to merge 28 commits intoupdate/wp-build-package-sourcesfrom
update/script-module-collision-tests
Open

Add PHPUnit tests for script module collision behavior#48095
retrofox wants to merge 28 commits intoupdate/wp-build-package-sourcesfrom
update/script-module-collision-tests

Conversation

@retrofox
Copy link
Copy Markdown
Contributor

What?

Adds PHPUnit tests to premium-analytics that document and verify the behavior of script module collisions when multiple plugins register the same packageSources entry (e.g., @automattic/number-formatters).

Addresses WOOA7S-1346.

Follows up on the concern raised by @anomiex in PR #48089.

Why?

The generated modules.php from wp-build uses a deregister-before-register pattern to override Core defaults. When two plugins ship the same packageSources entry, this creates a last-plugin-wins collision. These tests verify the exact behavior and document the trade-offs.

How?

  • Added automattic/jetpack-test-environment to premium-analytics dev dependencies for WordPress function access in tests
  • Created Script_Module_Collision_Test.php with 7 tests covering:
    • Last-wins: with deregister-then-register, the last plugin to load wins
    • Older version risk: an older version overwrites a newer one if its plugin loads last
    • First-wins default: without deregister, WordPress native wp_register_script_module() is first-wins
    • Consumer dependency survival: route dependencies survive re-registration of the shared module
    • Multiple consumers: consumers from different plugins all resolve to the winning version
    • Dequeue side-effect: wp_deregister_script_module() also dequeues the module
    • Dynamic imports: dynamic import dependency types are preserved after re-registration

Findings

  1. Within the Jetpack monorepo, collision risk is low because all plugins share the same pnpm-lock.yaml
  2. The real risk is cross-release version skew (plugin A on release N, plugin B on release N-1)
  3. This is the same trade-off that externalNamespaces already makes with @wordpress/* packages

Testing

cd projects/packages/premium-analytics
composer update
composer phpunit

All 8 tests pass (1 existing + 7 new, 19 assertions).

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 14, 2026

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • 🔴 Add a "[Status]" label (In Progress, Needs Review, ...).
  • 🔴 Add testing instructions.
  • 🔴 Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


🔴 Action required: Please include detailed testing steps, explaining how to test your change, like so:

## Testing instructions:

* Go to '..'
*

🔴 Action required: We would recommend that you add a section to the PR description to specify whether this PR includes any changes to data or privacy, like so:

## Does this pull request change what data or activity we track or use?

My PR adds *x* and *y*.

Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!


Premium Analytics plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

@github-actions github-actions bot added the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Apr 14, 2026
@retrofox retrofox requested a review from Copilot April 14, 2026 19:12
@jp-launch-control
Copy link
Copy Markdown

jp-launch-control bot commented Apr 14, 2026

Code Coverage Summary

Cannot generate coverage summary while tests are failing. 🤐

Please fix the tests, or re-run the Code coverage job if it was something being flaky.

Full summary · PHP report

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

Adds PHPUnit coverage to the premium-analytics package to document and lock in expected WordPress script module behavior when multiple plugins register the same packageSources module ID, particularly around the wp-build “deregister then register” override pattern.

Changes:

  • Initialize the shared Jetpack WorDBless-based WordPress test environment in the PHPUnit bootstrap.
  • Add a new PHPUnit test class covering multiple collision scenarios (last-wins override, first-wins default behavior, dependency survival, dequeue side-effects, dynamic imports).
  • Add automattic/jetpack-test-environment as a require-dev dependency for the package.

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated no comments.

File Description
projects/packages/premium-analytics/tests/php/bootstrap.php Initializes the shared WP test environment so WP script-module APIs/classes are available to tests.
projects/packages/premium-analytics/tests/php/Script_Module_Collision_Test.php Adds targeted tests that simulate modules.php override behavior and assert the resulting registry/queue outcomes.
projects/packages/premium-analytics/composer.json Adds the dev dependency needed to provide the WP test environment for this package’s PHPUnit suite.

@retrofox retrofox added [Status] Needs Review This PR is ready for review. and removed [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. labels Apr 14, 2026
@github-actions github-actions bot added [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. [Package] Analytics and removed [Status] Needs Review This PR is ready for review. labels Apr 14, 2026
retrofox and others added 15 commits April 15, 2026 14:48
Co-authored-by: Brad Jorsch <anomiex@users.noreply.github.com>
default remains 'Analytics'; plugin passes 'Premium Analytics'
Composer package (automattic/jetpack-analytics) that renders a
full-page admin dashboard using @wordpress/boot. Designed to be
consumed by any plugin via Analytics::init().
Patches @wordpress/build@0.11.0 to support packageSources config
for cross-directory package discovery. Per-package externals instead
of namespace-wide inference, skip transpilation for external sources.
Adds wpScriptModuleExports to number-formatters.
Upstream: WordPress/gutenberg#77226
Configures @automattic/number-formatters as a named source in
wpPlugin.packageSources. Adds @wordpress/element and @types/react
as dependencies for route components.
Demonstrates both import patterns in the dashboard route:
static import for formatNumber, dynamic import() for
formatNumberCompact. Validates packageSources externalization
and module dependency tracking in .asset.php.
@retrofox retrofox force-pushed the update/wp-build-package-sources branch from 7ccaa1a to e9b7a96 Compare April 15, 2026 16:54
@retrofox retrofox requested a review from a team as a code owner April 15, 2026 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants