Question: What is the recommended best practice for unit testing? #696
Replies: 3 comments 1 reply
-
You can fix It adding the config json on |
Beta Was this translation helpful? Give feedback.
-
Not sure if it can help anyone? I choose to test all my components with the English translation. Here is my solution:
In the actual tests I will then import render (and any other methods) from testUtils.ts:
This will render components with the actual translations inside the test environment. |
Beta Was this translation helpful? Give feedback.
-
In case to test hooks using the jest.setup.js: import transCore from 'next-translate/transCore';
const lang = 'en-EN';
const allNamespaces = require('fs').readdirSync(`./locales/${lang}`).reduce((all, file) => {
const namespace = file.split('.')[0];
all[namespace] = require(`./locales/${lang}/${namespace}.json`);
return all;
}, {});
jest.mock('next-translate/useTranslation', (namespace = 'common') => () => ({
t: (id, params = {}) => {
const prefix = id.includes(':') ? '' : `${namespace}:`;
const t = transCore({
config: { logger: () => null },
allNamespaces,
pluralRules: new Intl.PluralRules(lang),
lang,
});
return t(`${prefix}${id}`, params);
},
lang,
})); This is needed because the normal |
Beta Was this translation helpful? Give feedback.
-
Hello! What is the recommended way to set up unit tests with components that use
next-translate
?I currently don't see a straightforward way of doing this, so I concocted my own solution (albiet imperfect). Right now I'm creating a custom
render
function when usingtesting-library/react
. In that, I useI18nProvider
then pass in any translations I think it might need, but this can get unwieldy since this would require me to know all the translations my tests will need upfront.In the test, I'm trying to use
getT
to get the translation being used, but I get the following error:Is there a recomended way of going about this?
Thanks
Edit: turns out this is also an open question on stack overflow
Beta Was this translation helpful? Give feedback.
All reactions