我正在使用Typescript / React运行Jest,并设置了此类:
import i18n from "../i18n";
export default abstract class AnimalTypes {
....
并且当我尝试运行测试时,我得到:
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import i18n from "i18next";
^^^^^^
SyntaxError: Cannot use import statement outside a module
> 1 | import i18n from "../i18n";
我的jestconfig看起来像这样:
module.exports = {
verbose: true,
roots: ["<rootDir>"],
testRegex: "/__tests__/.*\\.(test|spec)\\.tsx?",
transform: {
"^.+\\.ts?$": "ts-jest",
}
};
如何正确包含导入语句而不会出错?
这不是我的回答,而是我在粗略搜索后找到的答案。
我通过以下操作解决了该问题:
yarn add ts-jest -D
然后在jest.config.js文件中,放置以下内容:
module.exports = { transform: { '^.+\\.ts?$': 'ts-jest', }, };