-
-
Notifications
You must be signed in to change notification settings - Fork 355
setting locale in jest test environment is not working correctly #1160
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
Comments
Thank you for your reporting! This is weird behavior... |
@r-kober You can try this. import Form from './Form.vue';
export const i18n = createI18n({
// set legacy to false
legacy: false,
});
config.global.plugins = [i18n];
describe('Mounted Form', () => {
test.each(['en', 'ru', 'uk'])('renders correctly %s', locale => {
// set locale
i18n.global.locale.value = locale;
const wrapper = mount(Form);
expect(wrapper.element).toMatchSnapshot();
});
}); |
@kazupon I can confirm that this is also an issue when using It seems that doing this only works the first time: i18n.global.locale.value = locale; |
I am having the same issue with vitest + testing library. Let me know if I need to submit more info! |
Hi @kazupon and first thanks for this fantastic package so many people use ! I encounter the exact same problem here with an app using I was wondering, as some time has passed since this issue was open, whether you had some time to investigate and/or have found what could be the cause of this problem... Many thanks in advance for keeping us updated on this one. |
I believe the issue is due to Vue Testing Library(VTL). I've submitted an issue on their repo with a repo showing Vue utils working in similar tests - testing-library/vue-testing-library#318 Global translations seem to work in VTL, but local scope definitely has an issue. |
Hi @ben-hamel, Unfortunaly, I also encounter the problem with VTU as I don't use VTL personally. The way I implemented it on my side was to create an i18n instance with The only working way I've found is to recreate a new i18n instance before each test and associate it to |
Yeah, I'm unsure if there's difference due to mine being a Vitest setup. But you can check my repo and see translations are testing correctly with VTU. But with VTL I get the first test translating and then stops afterwards, but only for the local scoped translations. |
I experienced the same with Vue Testing Library. i18n.global.locale.value = language does not change the value correctly with a .each type test. Vitest. If the each is the only test run and en in the first language tested, it works if there are 2 languages being tested. If I run a test before the each test that also accesses locale, then the each will fail. Was able to make it work with Vue Test Utils. vue-i18n v9.1.10 works fine by the way. Not sure if that is the latest version that works though. |
Hi @kazupon, Just some words to let you know I investigated a bit deeper in it and found what the problem is. Thanks to @r-kober information, it was quite easy to spot. The problem happens since #977 was merged and since the effect scope is released when an I18n instance is disposed. Personally, I encountered it as I have a lot of components relying on import { config } from '@vue/test-utils'
const i18nInstance = createI18n({ ... })
config.global.plugins = [i18nInstance] But doing this, the plugin is re-installed and re-disposed in each unit test. That's why it works only once as the effect scope is destroyed after the first one ! I tried to add a boolean inside the I'd be happy to open a PR with this if you want, however, I'd like to know what you think about it before writing it:
Just tell me how you feel about this ! Thanks in advance ! |
Reporting a bug?
in Jest with testing-library the changing of the language does not really work. Only the first call to change this.$i18n.locale changes the locale actually and it stays that way for the following tests in which the locale should be changed.
If you try the same in the browser everything works fine.
Expected behavior
If you change $i18n.locale or i18n.global.locale it should change the locale in a jest environment everytime it is called.
Reproduction
https://github.com./r-kober/vue-i18n-next-jest-bug
System Info
Screenshot
No response
Additional context
The error came somewhere between Version 9.2.0-beta.34 and 9.2.0-beta.35, because in beta 34 it worked and also in 9.1.0.
Validations
The text was updated successfully, but these errors were encountered: