添加带有Typescript和Jest的i18n,得到“当'--isolatedModules'错误时,所有文件都必须是模块”

问题描述 投票:2回答:1

我正在尝试使用打字稿和玩笑来正确地模拟我的i18n文件,但在运行测试时却不断收到此错误:

  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', () => ({

react-i18next.ts:

jest.mock('i18next', () => ({
  use: () => {
    return {
      init: () => { }
    };
  },
  t: k => k
}));

我的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

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;
typescript jestjs i18next
1个回答
0
投票

[将文件放入__mocks__时,您不需要使用它进行映射

© www.soinside.com 2019 - 2024. All rights reserved.