Conversation
Explains that the build step downloads the pinned Gutenberg artifact from ghcr.io and copies the block editor files into src/, which the tests load. Also notes engine-strict and that the production build is not needed for PHPUnit. Fixes WordPress#243. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
| #### Why NodeJS is needed | ||
|
|
||
| The PHPUnit tests are PHP, but they do not run on a plain checkout of `wordpress-develop`. Some files that WordPress loads are not in version control. A build step creates them. | ||
|
|
||
| `prepare.php` runs `npm install && npm run build` in the checkout. For the tests, the important part is the Gutenberg step (`build:gutenberg` in the [Gruntfile](https://github.com/WordPress/wordpress-develop/blob/trunk/Gruntfile.js)): | ||
|
|
||
| 1. It downloads the built Gutenberg artifact for the version pinned in `package.json` (`gutenberg.sha`). The download comes from `ghcr.io`, so the server must be able to connect to it. | ||
| 2. It copies the block editor PHP files, routes, blocks, scripts, styles and `theme.json` into `src/`. | ||
|
|
||
| The tests load WordPress from `src/` (`ABSPATH` in `wp-tests-config.php`). These files were removed from version control in [changeset 61438](https://core.trac.wordpress.org/changeset/61438). Without the build, the tests fail when they load WordPress, for example on a missing `src/wp-includes/build/routes.php` ([#292](https://github.com/WordPress/phpunit-test-runner/issues/292)). | ||
|
|
||
| Also: | ||
|
|
||
| - Use the Node.js and npm versions in the `engines` field of `wordpress-develop/package.json`. Its `.npmrc` sets `engine-strict = true`, so `npm install` stops on older versions. | ||
| - `npm run build` also makes the production build: it copies files to `build/` and minifies JavaScript and CSS. The PHPUnit tests do not use these files. WordPress Core runs its own PHPUnit workflow after `npm ci` and `npm run build:dev`. [#244](https://github.com/WordPress/phpunit-test-runner/issues/244) tracks ways to make this step smaller. | ||
|
|
There was a problem hiding this comment.
Why Node.js is needed
The PHPUnit test suite itself is written in PHP, but Node.js is required to prepare the WordPress source code before the tests can run.
The test runner uses prepare.php to set up a WordPress checkout and prepare it for PHPUnit. As part of this process, it runs the WordPress build tasks, including the Gutenberg build step. This is necessary because some files used by WordPress Core are generated or assembled as part of the build process rather than being available directly in the wordpress-develop checkout.
The Gutenberg build step:
- Uses the Gutenberg version pinned by WordPress Core.
- Downloads the corresponding pre-built Gutenberg artifact from GitHub Container Registry (
ghcr.io). - Copies the required Gutenberg files into the WordPress
src/directory. - Makes those files available to the WordPress installation that PHPUnit loads during the test run.
The PHPUnit test suite loads WordPress from the src/ directory. Therefore, these build steps must complete successfully before the tests can run. If the Gutenberg files have not been prepared, the test suite can fail while loading WordPress because required files are missing.
This is why Node.js and npm are requirements for the test runner even though the tests themselves are written in PHP.
The Node.js and npm versions must also be compatible with the versions specified in the engines field of wordpress-develop/package.json. The WordPress development repository enables npm's engine-strict setting, so using an unsupported Node.js or npm version can cause npm install to fail.
It is also worth noting that npm run build performs more work than is required by PHPUnit. The complete WordPress build also creates the production build/ directory and performs tasks such as JavaScript and CSS minification. The PHPUnit tests primarily need the files prepared in src/. The additional build work is currently part of the preparation process.
Make it a standalone ### section, open with why a PHP suite needs Node.js, list the Gutenberg steps in order, and add a summary sentence (from the review). Keep the ghcr.io network requirement, the changeset and issue references, and the build:dev note. Mention that devEngines also enforces the npm version.
|
Thanks @dhruvang21, your version reads better. I reworked the section in 3632464 and used your structure:
I kept a few things that your text left out, because hosts need them:
I also added that |
Fixes #243.
Purpose
Hosts ask why a PHP test suite needs NodeJS. This adds a short "Why NodeJS is needed" section under the NodeJS installation steps in the README.
What it explains
prepare.phprunsnpm install && npm run build. The step the tests need isbuild:gutenberg. It downloads the pinned Gutenberg artifact fromghcr.ioand copies the block editor PHP files, routes, blocks, scripts, styles andtheme.jsonintosrc/.src/. Those files left version control in changeset 61438, so without the build, WordPress fails to load (example: PHPUnit tests fail: routes.php generated under build/ but tests bootstrap from src/ #292).ghcr.io. This is new information for firewalled hosts.engine-strict = trueinwordpress-develop/.npmrcmeans thatnpm installstops on an older Node.js version.npm run build(copy tobuild/, minify) is not used by PHPUnit. Core's own PHPUnit workflow usesnpm ciandnpm run build:dev. Links to Find ways to reduce the need for NodeJS #244 for the follow-up.Sources (wordpress-develop trunk)
Gruntfile.js: thebuild,build:gutenbergandgutenberg:downloadtasks, and the comment that references changeset 61438 and ticket 64393tools/gutenberg/utils.jsanddownload.js: version check, auto-download,ghcr.ioURLspackage.json(gutenberg.sha,engines) and.npmrc(engine-strict).github/workflows/reusable-phpunit-tests-v3.yml:npm ciandnpm run build:devbefore PHPUnitwp-tests-config-sample.php:ABSPATHissrc/Contributed at WordCamp Contributor Day.