Skip to content

Commit

Permalink
fix(test): enable nodejs compat for miniflare (#127)
Browse files Browse the repository at this point in the history
### Fix for Node.js Built-in Module Resolution in Cloudflare Worker
Miniflare Tests

#### **Overview**
This PR addresses an issue with resolving Node.js built-in modules
(`node:buffer`, `node:events`, `node:async_hooks`) in a Cloudflare
Worker Test environment. The Worker encountered errors in resolving
these built-in modules, which aren’t natively supported in Workers.

This issue was introduced when the [Opentelemetry PR was
merged](#125), but we didn't see
that because the Github Action that runs the `test:miniflare` was not
flagging the test failure.

#### **Error Description**
During testing, the following error appeared:
```
Unable to resolve "dist/worker.mjs" dependency "node:buffer": no matching module rules.
If you're trying to import a Node.js built-in module, or an npm package that uses Node.js built-ins, you'll either need to:
- Bundle your Worker, configuring your bundler to polyfill Node.js built-ins
- Configure your bundler to load Workers-compatible builds by changing the main fields/conditions
- Enable the `nodejs_compat` compatibility flag and use the `NodeJsCompatModule` module type
- Find an alternative package that doesn't require Node.js built-ins
```

#### **Solution**

1. To resolve this, I've enabled the `nodejs_compat` compatibility flag
in the `test/miniflare/freeway.spec.js` file - which allows the
Cloudflare Miniflare Worker to utilize Node.js runtime APIs, and set the
compatibility date to "2024-09-23", as suggested in their docs:
https://developers.cloudflare.com/workers/runtime-apis/nodejs/#get-started

2. Updated the Github Action to halt the execution in case of test
failures

3. Updated the wrangler package to clean up some warnings during the
integration tests.


#### **Note**
As of September 23, 2024, enabling `nodejs_compat` applies the same
behavior as `nodejs_compat_v2` for compatibility dates set to September
23, 2024 or later, meaning this configuration is fully aligned with the
latest runtime support for Node.js compatibility in Workers.
  • Loading branch information
fforbeck authored Nov 6, 2024
1 parent cde2abf commit 0165521
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 51 deletions.
15 changes: 12 additions & 3 deletions .github/actions/test/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,18 @@ runs:
- run: npm run test:unit
name: Unit Tests
shell: bash
- run: npm run test:miniflare
name: Miniflare Tests
shell: bash
- run: npm run test:integration
name: Integration Tests
shell: bash
- run: |
# If the Miniflare tests fail we need to capture the output and check for the error,
# because the error is not bubbled up from miniflare and the tests will pass.
npm run test:miniflare | tee test-output.log
# Check for the error in the output to fail the test action
if grep -q 'ERR_TEST_FAILURE' test-output.log; then
echo "Test failure detected: ERR_TEST_FAILURE"
exit 1
fi
name: Miniflare Tests
shell: bash
99 changes: 53 additions & 46 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"standard": "^17.1.0",
"tree-kill": "^1.2.2",
"typescript": "^5.6.3",
"wrangler": "^3.78.8"
"wrangler": "^3.84.1"
},
"standard": {
"ignore": [
Expand Down
3 changes: 2 additions & 1 deletion test/miniflare/freeway.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ describe('freeway', () => {
},
scriptPath: 'dist/worker.mjs',
modules: true,
compatibilityDate: '2023-06-17',
compatibilityFlags: ['nodejs_compat'],
compatibilityDate: '2024-09-23',
r2Buckets: ['CARPARK']
})

Expand Down

0 comments on commit 0165521

Please sign in to comment.