nuxt / nuxt/test-utils

Isolation conflict

Open
#342 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

v0
Dominant language
TypeScript
Stars
437
Forks
120
Avg merge
22h 52m
Merged PRs (30d)
23

Description

There's a complex problem with a not-rare case.

When you start your Nuxt app, Nuxt fills process.env with .env file content during Nuxt.config loading stage.
Process.env is available on the server, including code in the server bundle. So, many apps rely on this behavior for their private env vars.

Unfortunately, when you run Nuxt from inside your Jest test it behaves unexpectedly differently:
Nuxt still fills process.env, and it's even available inside the Jest test, but when it comes to the bundled server code there's a different copy of process.env that doesn't contain previously filled .env values.

The reason is doubled VM isolation usage - by Jest, then by vue-server-renderer (as a part of Nuxt).
Jest sets up a new VM context in the Node environment, so the global object (including global.process.env) inside our tests is not the initial one.
Nuxt runs fine in the same context, sharing the same global till a server render step, where runInThisContext is being called (by default). Unobviously, this method is not about sharing the current context, but the root one.

So, all the globals inside the client-side bundle are being shared on the top-level context ignoring the Jest isolation level.
It also can lead to conflicts between different tests.

It's hard to say, is it NTU's fault, Jest's, or VSR's? It looks like a bug in VSR, but only appears on the second isolation level, which hardly will be used outside nuxt+jest combo (which is NTU). So the issue is here

Workaround:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the Nuxt app run inside Jest, then read the referenced Nuxt packages/config/src/load.js and vue-server-renderer create-bundle-runner.ts entries alongside Jest's runtime and node-environment files. Done means the server bundle sees the intended .env values within Jest isolation without introducing conflicts between tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, nuxt
Domain
backend, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.