Add PHPUnit tests for script module collision behavior#48095
Add PHPUnit tests for script module collision behavior#48095retrofox wants to merge 28 commits intoupdate/wp-build-package-sourcesfrom
Conversation
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
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: 🔴 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: Follow this PR Review Process:
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. |
Code Coverage SummaryCannot generate coverage summary while tests are failing. 🤐 Please fix the tests, or re-run the Code coverage job if it was something being flaky. |
There was a problem hiding this comment.
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-environmentas arequire-devdependency 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. |
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.
7ccaa1a to
e9b7a96
Compare
covers deregister-register last-wins, first-wins default, consumer dependency survival, and enqueue state side-effects
e71a459 to
fe28063
Compare
e9b7a96 to
a4b0c04
Compare
25c7409 to
1b3fb87
Compare
What?
Adds PHPUnit tests to
premium-analyticsthat document and verify the behavior of script module collisions when multiple plugins register the samepackageSourcesentry (e.g.,@automattic/number-formatters).Addresses WOOA7S-1346.
Follows up on the concern raised by @anomiex in PR #48089.
Why?
The generated
modules.phpfrom wp-build uses a deregister-before-register pattern to override Core defaults. When two plugins ship the samepackageSourcesentry, this creates a last-plugin-wins collision. These tests verify the exact behavior and document the trade-offs.How?
automattic/jetpack-test-environmenttopremium-analyticsdev dependencies for WordPress function access in testsScript_Module_Collision_Test.phpwith 7 tests covering:wp_register_script_module()is first-winswp_deregister_script_module()also dequeues the moduleFindings
pnpm-lock.yamlexternalNamespacesalready makes with@wordpress/*packagesTesting
cd projects/packages/premium-analytics composer update composer phpunitAll 8 tests pass (1 existing + 7 new, 19 assertions).