-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
language is not set during tests with vitest #1724
Comments
make sure the file that contains the i18next.init call is included/required during your tests |
@adrai thanks for the suggestion. I've tried that with no avail sadly :/ Tried to include it in the test, in the test setup file, the |
hard to help, sorry |
@phun-ky this might be due to something called "dual package hazard" Had a similar issue (main code was using different build of the package than the library
|
Hi @phun-ky , I had the same problem and I solved it by using import {render as rtlRender} from '@testing-library/react'
import {I18nextProvider, initReactI18next} from "react-i18next";
import {createInstance} from "i18next";
import Backend from "i18next-fs-backend";
import translation from "../public/locales/en/translation.json"
const instance = createInstance()
await instance
.use(Backend)
.use(initReactI18next)
.init({
fallbackLng: "en",
debug: false,
interpolation: {
escapeValue: false
},
defaultNS: "translation",
initImmediate: false,
resources: {
en: {
translation
}
}
});
export const render = (ui: JSX.Element) => {
function Wrapper({children}: any): any {
return (
<I18nextProvider i18n={instance}>
{children}
</I18nextProvider>
)
}
rtlRender(ui, {wrapper: Wrapper})
} Hope that can help |
Hi there, I just found an alternative for me, which is pretty well working. In my case I need to use the var env
Actually by just defining the namespaces, with this line I have been trying your asynchronous stuff @RomRom1 but not working. |
🐛 Bug Report
Note
I do not think this is a bug, more of a configuration issue/documentation issue, because clearly, I am doing something wrong
Language is not set during tests with
vitest
. In a dependency I import, this code exists:This code throws
throw Error('Language must be either nb or en.');
when testing usingvitest
. It does not throw for building withvite
nor with the previous test runnerjest
(with practically same config).That code is wrapping a component that is consumed by several components up until the file I am testing, which is using this:
With an import
i18n
that looks like this:Which is consumed like this:
I've narrowed it down to
react-i18next
is not picking up language, i.e., thatuse(initReactI18next).init({…})
is not called, or something..To Reproduce
I cannot produce a reproduction case due to the complexity of the internal (non public) dependencies, which I also think has something to do with this. As stated, I think this is a misconfiguration on my part, not a bug itself.
Expected behavior
That language is set.
Your Environment
The text was updated successfully, but these errors were encountered: