【发布时间】:2018-08-15 19:37:03
【问题描述】:
所以我正在使用 Jest(使用 Enzyme)来测试 Typescript-React 项目,并且遇到了别名模块的问题。
我知道问题本身是找不到模块,因为它正确地找到了模块。我认为问题可能出在我的一个文件的结构上。
jest.config.js
module.exports = {
"collectCoverage": false,
"collectCoverageFrom": [
"!**/*.d.ts",
"src/**/*.{js,jsx,ts,tsx}"
],
"coverageDirectory": "test",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
],
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"testEnvironment": "node",
"testURL": "http://localhost",
"transform": {
"^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
"^.+\\.tsx?$": "ts-jest"
},
"transformIgnorePatterns": [
"[/\\\\]node_modules[/\\\\].+\\.(js|jsx|ts|tsx)$"
],
"resolver": "jest-webpack-resolver",
"moduleNameMapper": {
"^react-native$": "react-native-web",
"^components$": "<rootDir>/src/components",
"^reducers$": "<rootDir>/src/reducers",
"^actions$": "<rootDir>/src/actions",
"^selectors$": "<rootDir>/src/selectors",
"^services$": "<rootDir>/src/services",
"^views$": "<rootDir>/src/views",
"^domains$": "<rootDir>/src/domains"
},
};
在我正在测试的组件中使用的导入:
import { Label } from 'components';
我的项目结构在组件方面采用以下方式:
src
|_ components
|_ // ..various folders with React components
|_ index.ts
而我的 index.tsx 基本上是这样的:
export { default as Label } from './Label/Label'
// export other components
发生的事情如下:
当我运行 Jest 时,它可以很好地解析我的“组件”模块,这在我的 webpack.alias 中是一样的,但是破坏测试运行的是它开始“构建”(我不确定它是否真的构建?)其他组件,并开始为一些我什至没有测试的组件上升依赖关系树,并最终中断,因为它一直到运行整个应用程序的主 index.tsx 文件。
问题是我构建 index.ts 以导出所有组件的方式吗?
【问题讨论】:
标签: javascript reactjs typescript jestjs