ts-jest 无法在导入的 ES6 模块上运行测试



我在我的 Typescript 应用程序中使用编译的 ES6 库。测试失败并显示错误

类型错误: 无法读取未定义的属性"属性类型">

它抱怨的模块正在导入react

import React from 'react';

如果我将其更改为

import * as React from 'react';

然后它将编译并运行正常。这是我package.jsonjest配置:

"jest": {
"transform": {
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(/__tests__/.*|\.(test|spec))\.(ts|tsx|js|jsx)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json"
],
"verbose": true,
}

这是我的tsconfig.json

{
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": true,
"module": "commonjs",
"target": "es5",
"jsx": "react",
"allowJs": true,
"preserveConstEnums": true,
"removeComments": true,
"noImplicitAny": false,
"moduleResolution": "node",
"noUnusedParameters": true
},
"include": [
"./src/**/*"
],
"filesGlob": [
"**/*.ts",
"**/*.tsx"
],
"exclude": [
"node_modules",
"typings/main",
"typings/main.d.ts",
"typings/index.d.ts"
]
}

我在这里搞砸了什么配置?

then it will compile and run fine.

这就是TypeScript的必经之路。import * as React from "react".

最新更新