Skip to content

feat: add toggable injectCjsGlobals option#10709

Merged
sheremet-va merged 7 commits into
mainfrom
feat/inject-cjs-globals
Jul 8, 2026
Merged

feat: add toggable injectCjsGlobals option#10709
sheremet-va merged 7 commits into
mainfrom
feat/inject-cjs-globals

Conversation

@sheremet-va

@sheremet-va sheremet-va commented Jul 4, 2026

Copy link
Copy Markdown
Member

This PR adds injectCjsGlobals for projects that keep using the module runner to run tests, but need a more fair environment (ESM files do not have access to CJS globals).

Thanks Fable

I am hoping to disable this by default in Vitest 6

@netlify

netlify Bot commented Jul 4, 2026

Copy link
Copy Markdown

Deploy Preview for vitest-dev ready!

Name Link
🔨 Latest commit b196c87
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/6a4d03c9c9fb7f000929551c
😎 Deploy Preview https://deploy-preview-10709--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@hi-ogawa hi-ogawa left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

lgtm. Made a few comments.

I am hoping to disable this by default in Vitest 6

Also agree.

Comment thread packages/vitest/src/node/resolver.ts Outdated
Comment thread packages/vitest/src/runtime/moduleRunner/moduleEvaluator.ts Outdated
Comment thread packages/vitest/src/node/environments/fetchModule.ts
@sheremet-va

Copy link
Copy Markdown
Member Author

Important to sync with #10708 (or the other way around) because they touch the same mechanism

@sheremet-va
sheremet-va merged commit 826714e into main Jul 8, 2026
26 of 27 checks passed
@sheremet-va
sheremet-va deleted the feat/inject-cjs-globals branch July 8, 2026 07:02
sheremet-va added a commit that referenced this pull request Jul 8, 2026
The `fetchWarmModules` fast path serves inline modules to fresh workers
without the `moduleType` tag the direct-fetch path attaches (#10709).
With `injectCjsGlobals: false` the evaluator injects the CommonJS scope
only into modules tagged `moduleType: 'cjs'`, so a CommonJS dependency
read back from the snapshot evaluated without `require`/`module`/
`__dirname` and threw "require is not defined" — the first file worked
(direct fetch) while every later file served from the snapshot failed.

Recompute the tag in `fetchWarmModules` with the same `detectModuleType`
the direct-fetch path uses, gated on `injectCjsGlobals === false` so the
default path pays nothing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Have a strictModuleType option to disable CJS-ESM interop

2 participants