-
Notifications
You must be signed in to change notification settings - Fork 386
[CI] Add test for @php-wasm/node direct usage in Jest #3099
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
[CI] Add test for @php-wasm/node direct usage in Jest #3099
Conversation
Add a test that verifies @php-wasm/node can be used directly in Jest without requiring --experimental-vm-modules or spawning a child process. This test currently fails due to ESM syntax (import.meta.url) in the per-version packages breaking in Jest's CommonJS sandbox.
074e47a to
f765ae6
Compare
|
I've been exploring a fix for this issue, which checks if the environment supports dynamic imports and uses require otherwise, by using simple As mentioned in the PR description, the Jest VM doesn't allow dynamic imports by default; to make it work, Jest needs to run with a --experimental-vm-modules flag. Dynamic imports work in both ESM and CommonJS, so from what I can see, this is a Jest-specific issue. I don't think that we should address it in PHP-wasm by switching between import and require, and see two potential paths forward.
Looking at both options, my suggestion is to require the Is there a third option that I'm missing? Are there other places besides Jest where dynamic loading is an issue? @adamziel @brandonpayton @mho22, what do you think? |
|
@bgrgicak I fully agree with you. I think we have another complex option that could make this work. Print separate banners in CJS and ESM during build. This works with ESBuild or Vite. A Vite plugin could be created to add banners for tests. The last missing piece would be running the different packages locally, probably inside However, this is a lot more complicated process compared to adding the |
🚧 WIP
Motivation for the change, related issues
After upgrading from
@php-wasm/nodev3.0.22 to v3.0.39, Jest tests that useloadNodeRuntime()directly fail with ESM-related errors.The per-version packages introduced in #3062 contain JavaScript files that use ESM syntax (
import.meta.url), which breaks when loaded in Jest's CommonJS sandbox.Error:
Or alternatively:
This affects downstream projects (like WordPress Studio) that use
@php-wasm/nodedirectly in their Jest test suites.Implementation details
This PR adds a test that verifies
@php-wasm/nodecan be used directly in Jest without:--experimental-vm-modulesrunCLI()The test currently fails - it demonstrates the issue and will pass once a fix is implemented.
Root Cause
The per-version packages (
@php-wasm/node-8-3, etc.) contain ESM files:When
@php-wasm/node/index.cjsdynamically imports these packages, Jest cannot handle the ESM dynamic imports in its CommonJS sandbox.Related PRs
Testing Instructions (or ideally a Blueprint)
Run the test suite for
commonjs-and-jest:The new test
php-wasm-node.spec.tswill fail with the error above, demonstrating the issue.