You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
first of all I am not sure if this is a bug or if I did something wrong in configuring my tests or if this just normal behaviour.
I run into the problem that global variables ($<something>) provided via plugins are not available in the nuxt test context. I have a simple component, that uses the example function from the docs $hello('world') in the template section and the test fails with the error TypeError: _ctx.$hello is not a function.
I can fix this by providing $hello via a global mock to the options of mountSuspended, but I was under the impression that @nuxt/test-utils should provide the same context to vitest as when I run the nuxt app normally via the dev server.
This also affects modules like dayjs-nuxt for which I need to mock the $dayjs() function. A mock does not need to be provided for all modules though, e.g. the $t() function of @nuxt/i18n works without mocking it. Why is this the case?
MyComponent.spec.ts
// @vitest-environment nuxtimport{describe,it}from'vitest';import{mountSuspended}from'@nuxt/test-utils/runtime';importMyComponentfrom'../components/MyComponent.vue';describe('my test',()=>{it('Should render the component',async()=>{/* ❌ Fails with '_ctx.$hello is not a function' */constwrapper=awaitmountSuspended(MyComponent);/* ✅ Does only work with mocked $hello. */// const wrapper = await mountSuspended(MyComponent, {// global: { mocks: { $hello: (msg: string) => `Hello ${msg}!` }},// });});});
There are similar issues reported here for when the function is provided via a Symbol but I did not find anything regarding this exact problem and error message.
Logs
The text was updated successfully, but these errors were encountered:
Environment
Working directory: /home/projects/github-shtgaydv nuxi 17:57:03
Nuxt project info: nuxi 17:57:03
Reproduction
https://stackblitz.com/edit/github-shtgaydv
Describe the bug
Hello,
first of all I am not sure if this is a bug or if I did something wrong in configuring my tests or if this just normal behaviour.
I run into the problem that global variables (
$<something>
) provided via plugins are not available in the nuxt test context. I have a simple component, that uses the example function from the docs$hello('world')
in the template section and the test fails with the errorTypeError: _ctx.$hello is not a function
.I can fix this by providing
$hello
via a global mock to the options ofmountSuspended
, but I was under the impression that @nuxt/test-utils should provide the same context to vitest as when I run the nuxt app normally via the dev server.This also affects modules like dayjs-nuxt for which I need to mock the
$dayjs()
function. A mock does not need to be provided for all modules though, e.g. the$t()
function of @nuxt/i18n works without mocking it. Why is this the case?MyComponent.spec.ts
plugins/hello
MyComponent.vue
Additional context
There are similar issues reported here for when the function is provided via a Symbol but I did not find anything regarding this exact problem and error message.
Logs
The text was updated successfully, but these errors were encountered: