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 mock globals with vitest #321

Open
shellandbull opened this issue Jan 15, 2025 · 1 comment
Open

Unable to mock globals with vitest #321

shellandbull opened this issue Jan 15, 2025 · 1 comment
Labels
help wanted Extra attention is needed

Comments

@shellandbull
Copy link

Hello all!

I'm on

"nuxt-auth-utils": "^0.5.7"
"nuxt": "^3.14.1592"

And I'm attempting to mock the login global useUserSession on my test. But it fails with an useUserSession is not defined error. My code looks like this:

// component code
const { fetch: refreshSession } = useUserSession();

And my test code looks like this

      const wrapper: VueWrapper = mount(LoginForm, {
        global: {
          stubs: ['Button', 'FormInput', 'IconArrowLeft', 'NavLink'],
          mocks: {
            useUserSession: () => ({
              fetch: vi.fn(() => Promise.resolve()),
              loggedIn: false,
              user: null,
            }),
          },
        },
      });

I also tried using vi.mock with no success

How can I mock the globals introduced by this module on my tests?

I'm currently following the official guides -> https://nuxt.com/docs/guide/recipes/sessions-and-authentication

I'm happy to contribute some testing auth examples to the docs if we solve this one 🎊

Copy link
Owner

atinux commented Jan 21, 2025

I am sorry but I do not have enough XP on mocking, I really hope you'll find a solution

@atinux atinux added the help wanted Extra attention is needed label Jan 21, 2025 — with Volta.net
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants