When trying to properly mock my i18n file with TypeScript and Jest, I encountered an error while running the test:
TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):
__tests__/__mock__/react-i18next.ts:1:1 - error TS1208: All files must be modules when the '--isolatedModules' flag is provided.
1 jest.mock('i18next', () => ({
In my file react-i18next.ts:
jest.mock('i18next', () => ({
use: () => {
return {
init: () => { }
};
},
t: k => k
}));
Here's a snippet from my jest.config:
module.exports = {
verbose: true,
roots: ["<rootDir>"],
testRegex: "/__tests__/.*\\.(test|spec)\\.tsx?",
transform: {
"^.+\\.tsx?$": "ts-jest",
},
moduleNameMapper: {
"react-i18next": "<rootDir>/__tests__/__mock__/react-i18next.ts"
},
};
i18n.ts content:
import i18n from "i18next";
import {initReactI18next} from "react-i18next";
import english from "./locales/en/common.json";
import french from "./locales/fr/common.json";
i18n.use(initReactI18next).init({
lng: 'en',
resources: {
fr: {
translation: french,
},
en: {
translation: english,
},
},
interpolation: {
escapeValue: false,
},
fallbackLng: ["en"]
});
export default i18n;