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

How to to jest test inside nuxt-bridge #610

Closed
MatthD opened this issue Nov 3, 2022 · 4 comments
Closed

How to to jest test inside nuxt-bridge #610

MatthD opened this issue Nov 3, 2022 · 4 comments
Labels
question Further information is requested

Comments

@MatthD
Copy link

MatthD commented Nov 3, 2022

Environment



Reproduction

composables/config.ts

type Config = {
  categories: string[];
  featureFlags: string[];
  languages: string[];
};

const config = reactive<Config>({
  categories: [],
  featureFlags: [],
  languages: [],
});
export const useConfig = () => {
  $fetch<Config>('/url')
    .then(({ categories }) => {
      config.categories = categories;
    })
    .catch(console.error);
  return config;
};

config.spec.ts

import { mount } from '@vue/test-utils';
import flushPromises from 'flush-promises';
import { useConfig } from './config'; //importing the composable

const mockedGetConfig = {
  categories: ['first'],
  featureFlags: ['header', 'footer'],
  languages: ['fr'],
};

describe('Config', () => {
  afterEach(() => {
    jest.resetAllMocks();
  });
  it('Should return config from hook when called', async () => {
    const Comp = {
      template: `<div>Fake component {{config}}</div>`,
      setup() {
        const config = useConfig();
        return { config };
      },
    };
    const wrapper = mount(Comp);
    await flushPromises();
    expect(wrapper.vm.config).toStrictEqual({
      categories: ['first'],
      featureFlags: ['header', 'footer'],
      languages: ['fr'],
    });
  });
});

The error

Capture d’écran 2022-11-03 à 14 27 14

Describe the bug

Jest failed to execute test when auto import composables of nuxt are used

Trying to test a composables that use reactive auto-import lead to ReferenceError: reactive is not defined
I tried to install @nuxt/test-utils but it's not compatible with nuxt-bridge.

What is the way to do unit testing on nuxt-bridge with auto import inside ?

Additional context

No response

Logs

No response

@antlionguard
Copy link

can you share small reproduction?

@MatthD
Copy link
Author

MatthD commented Nov 3, 2022

Yes sure (will add it to the reproduction inside the desc)

composables/config.ts

type Config = {
  categories: string[];
  featureFlags: string[];
  languages: string[];
};

const config = reactive<Config>({
  categories: [],
  featureFlags: [],
  languages: [],
});
export const useConfig = () => {
  $fetch<Config>('/url')
    .then(({ categories }) => {
      config.categories = categories;
    })
    .catch(console.error);
  return config;
};

config.spec.ts

import { mount } from '@vue/test-utils';
import flushPromises from 'flush-promises';
import { useConfig } from './config'; //importing the composable

const mockedGetConfig = {
  categories: ['first'],
  featureFlags: ['header', 'footer'],
  languages: ['fr'],
};

describe('Config', () => {
  afterEach(() => {
    jest.resetAllMocks();
  });
  it('Should return config from hook when called', async () => {
    const Comp = {
      template: `<div>Fake component {{config}}</div>`,
      setup() {
        const config = useConfig();
        return { config };
      },
    };
    const wrapper = mount(Comp);
    await flushPromises();
    expect(wrapper.vm.config).toStrictEqual({
      categories: ['first'],
      featureFlags: ['header', 'footer'],
      languages: ['fr'],
    });
  });
});

The error

Capture d’écran 2022-11-03 à 14 27 14

@wattanx
Copy link
Collaborator

wattanx commented May 18, 2023

I believe there's no way to solve Auto Import with Jest.
It's not impossible if you do something like this:

// jest.setup.ts
import * as vue from 'vue';
for (const key of Object.keys(vue)) {
  global[key] = vue[key];
}

However, personally, I think it would be better to use https://github.com/antfu/unplugin-auto-import with Vitest.

@wattanx wattanx added question Further information is requested and removed pending triage labels Aug 22, 2023
@wattanx wattanx closed this as completed Aug 22, 2023
@wattanx
Copy link
Collaborator

wattanx commented Aug 22, 2023

It will be closed as there is no reaction for some time.
You can open it again if you have bugs or questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants