Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use vitest spy within nuxt 3 component methods. #763

Open
LorenzoRottigni opened this issue Feb 18, 2024 · 0 comments
Open

Unable to use vitest spy within nuxt 3 component methods. #763

LorenzoRottigni opened this issue Feb 18, 2024 · 0 comments

Comments

@LorenzoRottigni
Copy link

Environment

This is one of the environments i came across the problem:
[11:15:23 AM] Working directory: /Users/lorenzorottigni/Documents/gitlab_private/portfolio.rottigni.tech
[11:15:23 AM] Nuxt project info: (copied to clipboard)



[11:15:23 AM] 👉 Report an issue: https://github.com/nuxt/nuxt/issues/new

👉 Suggest an improvement: https://github.com/nuxt/nuxt/discussions/new

👉 Read documentation: https://nuxt.com

Reproduction

The crucial file is MyComponent.spec.ts:
https://stackblitz.com/~/github.com/LorenzoRottigni/nuxt-test-utils-vitest-spy

Describe the bug

I'm currently facing challenges in testing a component method within a Nuxt environment. Specifically, I'm attempting to spy on a component method to test its interactions within template events. Despite researching similar cases online, I've yet to find a clear solution to this issue.

One suggestion I encountered was to define the spy before the component mounts. However, I'm unable to access wrapper.vm before mount time, complicating this approach. Additionally, I experimented with spying on the global injected $fetch method from the 'ofetch' library. Unfortunately, this led to another error regarding the inability to redefine the variable $fetch when attempting variations like:

import OFetch from 'ofetch'
const spy = vi.spyOn(OFetch, '$fetch')
// or
const spy = vi.spyOn(global, '$fetch')
// or
const spy = vi.spyOn({ $fetch }, '$fetch')

It appears that this issue may not be directly tied to the nuxt-test-utils package. Nevertheless, I wanted to bring it to your attention in the hopes that someone might offer guidance or assistance. I'm eager to find a resolution and would appreciate any insights or suggestions you might have.

I'm also considering reporting a similar issue to the vue-test-utils package, as I suspect that this problem might be reproducible outside of the Nuxt environment.

Thank you for your time and assistance.

Additional context

No response

Logs

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant