【发布时间】:2020-09-15 14:38:07
【问题描述】:
我有一个使用 TypeScript 的 React 项目,在主 tsx 文件中,import React from 'react' 行工作正常。但是在我的测试文件中,它仍然会触发 TS1259 错误。我猜我的 TS/Jest/Babel 配置有些古怪,但似乎无法确定。
有人知道是什么问题吗?
tsconfig.json
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"target": "ES2019",
"moduleResolution": "node",
"module": "ESNext",
"allowJs": true,
"noEmit": true,
"strict": true,
"isolatedModules": true,
"esModuleInterop": true,
"strictNullChecks": true,
"jsx": "react",
"baseUrl": "./",
"types": []
},
"include": [
"index.d.ts",
"src/*.ts",
"src/*.tsx",
"src/**/*.ts",
"src/**/*.tsx"
]
}
jest.config.js
module.exports = {
collectCoverageFrom: ['src/**/*.{ts,tsx}', '!src/index.ts'],
moduleNameMapper: {'^.+\\.s?css$': 'identity-obj-proxy'},
transform: {'^.+\\.tsx?$': 'babel-jest'},
testRegex: '/__tests__/.*\\.test\\.tsx?$',
testURL: 'http://localhost',
setupFilesAfterEnv: ['<rootDir>/__tests__/setup.ts'],
setupFiles: [],
testPathIgnorePatterns: [
'__snapshots__',
'__mocks__',
'<rootDir>/scripts',
],
};
Jest 测试文件中的错误消息
主组件文件没有错误
【问题讨论】:
-
如果您的测试被
tsconfig.json排除在外,但似乎并非如此,就会发生这种情况。这可能是你的IDE误解了一些东西。从命令行运行./node_modules/.bin/tsc --noEmit进行类型检查。 -
实际上,我们的测试不在
src文件夹中,因此您的建议被证明是100% 正确的。您介意将其发布为答案,以便我可以将其标记为解决方案吗?我知道这将是一些平凡的细节(我不总是这样吗?)。谢谢! -
很高兴我能帮上忙。
标签: reactjs typescript jestjs