Skip to content

[vitest-pool-workers] Fix JSON require() in module fallback#11917

Merged
vicb merged 4 commits intocloudflare:mainfrom
ksawaneh:fix/vitest-pool-workers-json-require
Jan 26, 2026
Merged

[vitest-pool-workers] Fix JSON require() in module fallback#11917
vicb merged 4 commits intocloudflare:mainfrom
ksawaneh:fix/vitest-pool-workers-json-require

Conversation

@ksawaneh
Copy link
Copy Markdown
Contributor

@ksawaneh ksawaneh commented Jan 14, 2026

Fixes #9822.

@cloudflare/vitest-pool-workers's module fallback service returned .json files as commonJsModule source. When a CommonJS dependency does require("./data.json"), workerd would try to parse the JSON as JavaScript and throw SyntaxError: Unexpected token ':'.

This change:

  • Extends fallback resolution to consider .json
  • Serves .json as an ES module wrapper that exports JSON.parse(...) (avoids object-literal __proto__ prototype mutation)
  • Adds a regression test covering CommonJS JSON require
  • Adds a changeset for @cloudflare/vitest-pool-workers

  • Tests
    • Tests included/updated
    • Automated tests not possible - manual testing has been completed as follows:
    • Additional testing not necessary because:

Commands run:

  • pnpm check

  • pnpm --filter @cloudflare/vitest-pool-workers run check:lint

  • pnpm --filter @cloudflare/vitest-pool-workers run check:type

  • pnpm exec vitest run test/json-require.test.ts (from packages/vitest-pool-workers)

  • Public documentation

    • Cloudflare docs PR(s):
    • Documentation not necessary because: internal module fallback behavior + regression test, no docs change required

A picture of a cute animal (not mandatory, but encouraged)

/_/
( o.o )

^ <


Open with Devin

@ksawaneh ksawaneh requested a review from a team as a code owner January 14, 2026 13:07
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Jan 14, 2026

🦋 Changeset detected

Latest commit: 93cacb2

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Copy Markdown
Contributor

@penalosa penalosa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this PR! It's looking good, just a couple of questions about comments and testing

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for adding this test! However, I think it might be more appropriate as a fixture test importing a real library that suffers from this (e.g. mime-types) in fixtures/vitest-pool-workers-examples/module-resolution/src/index.ts. Happy to add this if you'd like

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Jan 20, 2026

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@11917

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@11917

miniflare

npm i https://pkg.pr.new/miniflare@11917

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@11917

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@11917

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@11917

@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@11917

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@11917

@cloudflare/workers-utils

npm i https://pkg.pr.new/@cloudflare/workers-utils@11917

wrangler

npm i https://pkg.pr.new/wrangler@11917

commit: 93cacb2

ksawaneh and others added 2 commits January 23, 2026 00:17
- Use { json } module type directly instead of converting to ESM
- Replace synthetic test with mime-types fixture test in module-resolution
- Update example package in comment from Ajv to mime-types
@penalosa penalosa force-pushed the fix/vitest-pool-workers-json-require branch from f4364f3 to c43ef1a Compare January 23, 2026 00:18
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 3 additional flags.

Open in Devin Review

@github-project-automation github-project-automation bot moved this from Untriaged to Approved in workers-sdk Jan 23, 2026
@vicb
Copy link
Copy Markdown
Contributor

vicb commented Jan 26, 2026

Thanks for the PR @ksawaneh

@vicb vicb merged commit 7b40ceb into cloudflare:main Jan 26, 2026
32 checks passed
@github-project-automation github-project-automation bot moved this from Approved to Done in workers-sdk Jan 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

require-ing a json file breaks vitest

4 participants