nuxt / nuxt/test-utils

Overriding msw mock handler does not work when using `nuxt` environment

Open
#1,222 3 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Environment
  • Operating System: Windows_NT
  • Node Version: v22.14.0
  • Nuxt Version: 3.16.1
  • CLI Version: 3.23.1
  • Nitro Version: 2.11.8
  • Package Manager: npm@10.9.2
  • Builder: -
  • User Config: compatibilityDate, devtools, ssr
  • Runtime Modules: -
  • Build Modules: -
Reproduction

https://github.com/AlexanderForm1/nuxt-msw-override

Run npx vitest

I tried using stackblitz but for some reason an error is thrown from the vitest setup file when importing from msw.

Describe the bug

I'm trying to test a vue component that renders some data fetched from a mocked API endpoint using Mock Service Worker. I have a vitest setup file which creates the msw server with a default handler. Inside the tests I'm overriding the default handler following the steps described here.

I have two test files, one using the default test environment (happy-dom, set in vitest.config.ts) and another using the nuxt environment.

When running the test that uses happy-dom environment the component renders the data from the handler that overrides the default one and it passes.
When running the test that uses nuxt environment it fails because the component renders the data from the default handler, even though the handler is overridden at the top of the test just like in the passing test.

Additional context

No response

Logs

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 running npx vitest in the linked reproduction and compare the happy-dom and Nuxt environment tests. Inspect vitest.config.ts and the Vitest setup file to trace server setup and handler overrides. Done means the Nuxt-environment test uses the overridden MSW handler and passes consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
nuxtjs, typescript
Domain
testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.