-
Notifications
You must be signed in to change notification settings - Fork 110
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
Testing with vue-18n and script setup #302
Comments
I'm using Vue 3 with script setup (Single File Component) and Jest. my_i18n_lib_path/i18n.ts:
the SFC myComponent.vue file:
then the test:
Hope this helps someone, or if it can be improved, just share. |
Has anyone a solution yet? - Struggling with the same problem |
Vitest setup files to the rescue: // vite.config.ts
export default defineConfig({
test: {
root: './src/',
setupFiles: ['./setup.ts'],
}
}) // src/plugins/i18n.ts
import { createI18n } from 'vue-i18n';
export default createI18n({
// ...
}); // src/setup.ts
import i18n from '@/plugins/i18n';
import { config } from '@vue/test-utils';
// @ts-expect-error type
if (!globalThis.defined) {
config.global.plugins = [i18n];
// @ts-expect-error type
globalThis.defined = true;
} |
I added Then, I used this in my test:
This seems very hackish, but nothing else would work. |
if anyone still having the issue import { vi } from 'vitest';
vi.mock('vue-i18n', () => ({
useI18n: () => ({
t: (key: string) => key,
d: (key: string) => key,
}),
})); fond solution from here |
I don't know why, but in my case this was happening because I use pinia. Once I set pinia as plugin everything worked. So if anyones struggling with this. Try it
|
I use vue-i18n in my component, which is written using script setup syntax. When I try to render component in my test I'm getting the error
TypeError: $setup.t is not a function
The test:
I've solved this problem in another test by this way:
But It doesn't work in this case. I've seen same problem in stack overflow several times, but no answers , so I consider it as bug.
Component I try to test: https://github.com/Explicit12/utube/blob/main/src/components/TheHeader.vue
The text was updated successfully, but these errors were encountered: