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

Global helpers from plugins not available in test context #1102

Open
yschroe opened this issue Jan 28, 2025 · 0 comments
Open

Global helpers from plugins not available in test context #1102

yschroe opened this issue Jan 28, 2025 · 0 comments

Comments

@yschroe
Copy link

yschroe commented Jan 28, 2025

Environment

Working directory: /home/projects/github-shtgaydv nuxi 17:57:03
Nuxt project info: nuxi 17:57:03


  • Operating System: Linux
  • Node Version: v18.20.3
  • Nuxt Version: 3.15.3
  • CLI Version: 3.20.0
  • Nitro Version: 2.10.4
  • Package Manager: [email protected]
  • Builder: -
  • User Config: devtools, compatibilityDate
  • Runtime Modules: -
  • Build Modules: -

Reproduction

https://stackblitz.com/edit/github-shtgaydv

Describe the bug

Hello,

first of all I am not sure if this is a bug or if I did something wrong in configuring my tests or if this just normal behaviour.

I run into the problem that global variables ($<something>) provided via plugins are not available in the nuxt test context. I have a simple component, that uses the example function from the docs $hello('world') in the template section and the test fails with the error TypeError: _ctx.$hello is not a function.

I can fix this by providing $hello via a global mock to the options of mountSuspended, but I was under the impression that @nuxt/test-utils should provide the same context to vitest as when I run the nuxt app normally via the dev server.

This also affects modules like dayjs-nuxt for which I need to mock the $dayjs() function. A mock does not need to be provided for all modules though, e.g. the $t() function of @nuxt/i18n works without mocking it. Why is this the case?

MyComponent.spec.ts

// @vitest-environment nuxt
import { describe, it } from 'vitest';
import { mountSuspended } from '@nuxt/test-utils/runtime';
import MyComponent from '../components/MyComponent.vue';

describe('my test', () => {
  it('Should render the component', async () => {
    /* ❌ Fails with '_ctx.$hello is not a function' */
    const wrapper = await mountSuspended(MyComponent);

    /* ✅ Does only work with mocked $hello. */
    // const wrapper = await mountSuspended(MyComponent, {
    //   global: { mocks: { $hello: (msg: string) => `Hello ${msg}!` } },
    // });
  });
});

plugins/hello

export default defineNuxtPlugin(() => {
  return {
    provide: {
      hello: (msg: string) => `Hello ${msg}!`,
    },
  };
});

MyComponent.vue

<template>
  <div>
    {{ $hello('world') }}
  </div>
</template>

Additional context

There are similar issues reported here for when the function is provided via a Symbol but I did not find anything regarding this exact problem and error message.

Logs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant