Skip to content

Nuxt 2 Jest context #910

Closed
Closed
@Julien-Martin

Description

@Julien-Martin

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 });
    });
  });

Metadata

Metadata

Assignees

No one assigned

    Labels

    📚 docsRelated to documentation changes

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions