-
Notifications
You must be signed in to change notification settings - Fork 87
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
mountSuspended with i18n #585
Comments
same problem here. I can't seem to set up an environment where I can have the vitest and i18n modules running at the same time 😅 |
I've had a similar issue with a slightly different setup where a different I've solved it by setting an alias in the //vitest.config.ts
resolve: {
alias: [
{
find: '@vue/test-utils',
replacement: '/node_modules/@vue/test-utils/dist/vue-test-utils.cjs.js'
}
]
} Does that work for you? |
So I added your suggested resolve alias and changed the package of the mountSuspended import. And now mountSuspended has not thrown the SyntaxError: Need to install with Although my tests pass now I still have some unhandled errors thrown by Vitest which seem to originate in the Nuxt layout component.
I remove the alias again to determine if it has anything to do with this error being thrown. But it points out that in my config changing the import package did the trick, not adding the alias you suggested.
If anyone has an idea what is causing this error is thrown while running any vitest test. Please enlighten me. |
I am also using SyntaxError: Need to install with `app.use` function
❯ Module.createCompileError ../node_modules/.pnpm/@intlify+message-compiler@9.3.0-beta.27/node_modules/@intlify/message-compiler/dist/message-compiler.node.mjs:78:19
❯ createI18nError ../node_modules/.pnpm/vue-i18n@9.3.0-beta.27/node_modules/vue-i18n/dist/vue-i18n.mjs:107:34
105| [I18nErrorCodes.UNEXPECTED_RETURN_TYPE]: 'Unexpected return type in composer',
106| [I18nErrorCodes.INVALID_ARGUMENT]: 'Invalid argument',
107| [I18nErrorCodes.MUST_BE_CALL_SETUP_TOP]: 'Must be called at the top of a `setup` function',
| ^
108| [I18nErrorCodes.NOT_INSTALLED]: 'Need to install with `app.use` function',
109| [I18nErrorCodes.UNEXPECTED_ERROR]: 'Unexpected error',
❯ Module.useI18n ../node_modules/.pnpm/vue-i18n@9.3.0-beta.27/node_modules/vue-i18n/dist/vue-i18n.mjs:2291:15
❯ setup components/global/ToolbarAccountMenu.vue:8:7
❯ callWithErrorHandling ../node_modules/.pnpm/@vue+runtime-core@3.3.4/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:156:18
❯ setupStatefulComponent ../node_modules/.pnpm/@vue+runtime-core@3.3.4/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:7190:25
❯ setupComponent ../node_modules/.pnpm/@vue+runtime-core@3.3.4/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:7151:36
❯ mountComponent ../node_modules/.pnpm/@vue+runtime-core@3.3.4/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5555:7
❯ processComponent ../node_modules/.pnpm/@vue+runtime-core@3.3.4/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5521:9
❯ patch ../node_modules/.pnpm/@vue+runtime-core@3.3.4/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5007:11
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Serialized Error: {
"code": 24,
"domain": undefined,
} |
Same problem here. I have tried many alternatives to be able to test components but none of them work correctly. I believe that component testing is essential for development of any type, not having a way to perform this type of testing is a huge blocker. @danielroe Do you have information about official support for this feature or what you recommend to solve this? |
Just stumbled across. I've managed to get some successful i18n tests. I stumbled for a while on only being able to access the i18n key when I had a requirement to test the actual value. The repo is public if it helps anyone. There's still lots I'm trying to figure out myself. Here's my setup.
|
If someone is still experiencing this, would you provide a reproduction? I've opened a sample PR here with an runtime test with i18n which seems to be working fine: #633 |
Would you be able to provide a reproduction? 🙏 More infoWhy do I need to provide a reproduction?Reproductions make it possible for us to triage and fix issues quickly with a relatively small team. It helps us discover the source of the problem, and also can reveal assumptions you or we might be making. What will happen?If you've provided a reproduction, we'll remove the label and try to reproduce the issue. If we can, we'll mark it as a bug and prioritise it based on its severity and how many people we think it might affect. If How can I create a reproduction?We have a couple of templates for starting with a minimal reproduction: 👉 https://stackblitz.com/github/nuxt/test-utils/tree/main/examples/app-vitest A public GitHub repository is also perfect. 👌 Please ensure that the reproduction is as minimal as possible. See more details in our guide. You might also find these other articles interesting and/or helpful: |
This issue was closed because it was open for 7 days without a reproduction. |
@danielroe I have a reproduction in a public repository, WordPress/openverse, where we're attemping to migrate to Nuxt 3 and are running into this very problem. Unfortunately things are a bit messy there still, because we're also still working through other issues: WordPress/openverse#3571 You can reproduce the issue by running What I'm seeing when I run vitest in the If you'd like me to open a separate issue for this, please let me know. The exact same presentation for us as in this issue:
cc @obulat who is the Openverse maintainer actually leading our Nuxt 3 migration effort. I'm just lending a hand today. |
Actually, I was able to reproduce this inside the application: #673 The issue is with the |
Can confirm @sarayourfriend
after changing all my t()-function-calls to
and removing the above useI18n-composable call, the error is gone. |
@renky It is indeed gone but const { t } = useI18n({useScope: 'local', messages: { en: { test: 'test' } } } })
|
Spent two days on this. It's worth fixing this issue. I solved the problem by mocking the "vue-i18n" module and assigning it to global plugins
import { beforeAll, afterAll, vi } from 'vitest'
import { ref } from 'vue'
import { config } from '@vue/test-utils'
import { createI18n } from 'vue-i18n'
beforeAll(() => {
vi.mock('vue-i18n', async (importOriginal) => {
const module = await importOriginal<typeof import('vue-i18n')>()
return {
...module,
useI18n: () => ({
locale: ref('en'),
locales: ref([
{
name: 'English',
code: 'en',
iso: 'en-GB',
file: 'en.ts',
},
]),
}),
}
})
})
afterAll(() => {
vi.restoreAllMocks()
})
// setup i18n
const i18n = createI18n({
legacy: false,
locale: 'en',
locales: [
{
name: 'English',
code: 'en',
iso: 'en-GB',
file: 'en.ts',
},
],
messages: {},
missing: (locale: string, key: string) => key,
})
config.global.plugins.push(i18n) |
@martio solution work for me, thank you |
@martio Solution is perfect :) |
This does not work for me, is there something missing before this is doable (or am I just missing an obvious step)? |
I'm not quite sure why but this didn't work for me. Instead, I was able to make it work with:
|
I'm setting up the nuxt-vitest module in my project. Though I'm running into some issues getting a simple test, I use mountSuspended(Logo) to test the mounting of a Logo component. This component uses i18n. Whereas when I'm using mount from Vitest the test passes.
The error I'm getting is as follows:
And while we are at it. Is there a way to test with the translated i18n messages iso the keys?
Here are some snippets from my configuration.
vitest.config.js
./test/nuxt/components/Logo.nuxt.spec.ts (with mountSuspended)
./tests/unit.setup.ts
./tests/nuxt/components/Logo.nuxt.spec.ts (without mountSuspended)
The text was updated successfully, but these errors were encountered: