Open jrutila opened 8 months ago
If you don't actually need the plugin for the test I would assume you could add an early return in your defineNuxtPlugin
based on the environment. Maybe you can useimport.meta.test
or pass a variable yourself and check for that using import.meta.env.YOUR_VARIABLE
and your script in package.json
being YOUR_VARIABLE=true vitest
.
If you don't actually need the plugin for the test I would assume you could add an early return in your
defineNuxtPlugin
based on the environment.
This is a good tip if you don't need the plugin. Still, cluttering the plugin code with that kind of a check is also little smelly. I investigated and I don't think there is a way to disable, say in vitest.config
, certain autoloaded plugins in tests run by test-utils
? That is totally different topic, though.
Environment
Nuxt project info:
Build Modules: -
@nuxt/test-utils version: 3.12.0 vitest version: 1.4.0
Reproduction
Here is a reproduce stackblitz: https://stackblitz.com/edit/github-lxyzpn?file=plugins%2Fonrequest.client.ts
Create a plugin that will override the default
$fetch
. This is handy when trying to inject your own headers into the HTTP requests. If you google this, you will find a helpful SO solution: https://stackoverflow.com/a/75871291/216846This works fine when running the dev environment or prod environment. When you now run
@nuxt/test-utils
based test case, the tests pass, but the run fails to this error (see the whole log in logs):Describe the bug
The correct way to implement this kind of plugin that edits the HTTP requests is described here: https://nuxt.com/docs/examples/advanced/use-custom-fetch-composable. Instead of returning you just inject the
globalThis.$fetch
with the created fetch instance.The bug is that in dev and prod environments you can as well do it like this
globalThis.$fetch = ofetch.create
. Everything works, except when you run unit tests with@nuxt/test-utils
, you get errors described above.So, again, the correct way is to do
globalThis.$fetch = $fetch.create
but I think there should be a warning or something guiding the devs to do it correctly. Or at least they find this bug issue and can fix it in their code.Additional context
No response
Logs