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

Nuxt 2 Jest context #910

Closed
Julien-Martin opened this issue Dec 19, 2021 · 2 comments
Closed

Nuxt 2 Jest context #910

Julien-Martin opened this issue Dec 19, 2021 · 2 comments
Labels
📚 docs Related to documentation changes

Comments

@Julien-Martin
Copy link

Reproduction

Thank you so much for such a great lib.
I found a little bug when testing store that use this.$nuxt

TypeError: Cannot destructure property '$config' of 'this.$nuxt' as it is undefined.

Sandbox: https://codesandbox.io/s/competent-field-w3j63?file=/test/use-main-store-error.spec.js

Steps to reproduce the behavior

  1. Inside actions use this.$nuxt to access the nuxt context
  2. Test the action => yarn test
  3. See error

Expected behavior

The test should pass because $nuxt should exist in this context

Actual behavior

The test throw an error

TypeError: Cannot destructure property '$config' of 'this.$nuxt' as it is undefined.

Additional information

I found a little workaround in my test to avoid this error

File: https://codesandbox.io/s/competent-field-w3j63?file=/test/use-main-store-working.spec.js

beforeEach(() => {
    const pinia = setActivePinia(createPinia());
    const contextMock = {
      $config: {
        CONFIG_1: 1
      }
    };
    pinia._p.push(({ store }) => {
      Object.defineProperty(store, "$nuxt", { value: contextMock });
    });
  });
@posva
Copy link
Member

posva commented Dec 19, 2021

This is not a bug, you need to mock the nuxt context. I don’t know if nuxt provides something to easily mock their context though

@posva posva added the 📚 docs Related to documentation changes label Dec 19, 2021
@posva posva mentioned this issue Dec 28, 2021
20 tasks
@posva
Copy link
Member

posva commented Dec 28, 2021

Added to #829

@posva posva closed this as completed Dec 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📚 docs Related to documentation changes
Projects
None yet
Development

No branches or pull requests

2 participants