I have recently made the switch from Jest to Vitest as my unit testing library for my Vue 3 application.
Currently, I am facing an issue while trying to write a unit test for a component that utilizes the vue-i18n library for text translation. When attempting to mount this component in my test file, it fails with the error:
ReferenceError: t is not defined
I would like to know the correct method to stub/mock t
from the
import { useI18n } from 'vue-i18n'
statement when writing tests using the Vitest library.
It's worth noting that the previous approach no longer works after upgrading from Vue2 to Vue3:
const wrapper = shallowMount(MyComponent, {
global: {
mocks: {
$t: () => {}
}
}
})
Below are some relevant package versions:
"vue": "^3.2.31",
"vue-i18n": "^9.2.0-beta.14",
"vite": "^2.9.0",
"vitest": "^0.10.2"
Thank you!