Skip to content

Unable to mock globals with vitest  #321

Open
@shellandbull

Description

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 🎊

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions