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 mock i18n with vitest #3291

Open
fchancel opened this issue Jan 2, 2025 · 1 comment
Open

How mock i18n with vitest #3291

fchancel opened this issue Jan 2, 2025 · 1 comment

Comments

@fchancel
Copy link

fchancel commented Jan 2, 2025

Hello,

I've been fighting for a day about the i18n mock with vitest.
Before updating to v9, I make a global mock as follows in vitest.setup.ts

vi.mock("vue-i18n", () => ({
  useI18n: () => ({
    t: (key: string) => key,
    locale: ref("en"),
  }),

  createI18n: () => ({
    install: () => {},
  }),
}));

But since the update, I've tried a number of solutions to make an i18n mock, but I've never succeeded.
I keep getting the following error:

SyntaxError: Need to install with `app.use` function
 ❯ Module.createCompileError ../node_modules/@intlify/message-compiler/dist/message-compiler.mjs:78:19
     76|         : code;
     77|     const error = new SyntaxError(String(msg));
     78|     error.code = code;
       |                   ^
     79|     if (loc) {
     80|         error.location = loc;
 ❯ createI18nError ../node_modules/vue-i18n/dist/vue-i18n.mjs:81:34
 ❯ Module.useI18n ../node_modules/vue-i18n/dist/vue-i18n.mjs:2187:15
 ❯ setup modules/test/pages/test/test.vue:28:19
 ❯ wrappedSetup ../node_modules/@nuxt/test-utils/dist/runtime-utils/index.mjs:244:28
 ❯ clonedComponent.setup ../node_modules/@nuxt/test-utils/dist/runtime-utils/index.mjs:328:50
 ❯ callWithErrorHandling ../node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:200:19
 ❯ setupStatefulComponent ../node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:7848:25
 ❯ setupComponent ../node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:7809:36
 ❯ mountComponent ../node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5159:7

knowing that in my component view. I use the following format:

const { t } = useI18n()

and that my nuxt.config.ts is

is as follows:

  i18n: {
    strategy: "no_prefix",
    locales: [
      {
        code: "en",
        name: "English",
        files: ["en.json"],
      },
      {
        code: "fr",
        name: "Français",
        files: ["fr.json"],
      },
    ],
    defaultLocale: "en",
    detectBrowserLanguage: {
      useCookie: true,
      cookieKey: "i18n_test",
      fallbackLocale: "en",
    },
  },

Do you have any solutions ?

@th1nkgr33n
Copy link

th1nkgr33n commented Jan 22, 2025

Although I got another issues with the new Update :D

In the past we have configured a global i18n mock/instance in the setup.ts file of vitest.
Currently this throws a duplicated init warning for the t function, but if I remove this, I got your error, so this might help?

// https://github.com/nuxt/test-utils/issues/566#issuecomment-2200984889
import { config } from '@vue/test-utils'
import { createI18n } from 'vue-i18n'

const i18n = createI18n({
  legacy: false,
  globalInjection: true,
  locale: 'de',
  missing: (_, key) => key,
})

config.global.plugins.push(i18n)

vue-i18n made the use of the useI18n composable much stricter, not to use it outside of a
For other purposes you need to use useNuxtApp().$i18n

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

No branches or pull requests

2 participants