我有一个使用TypeScript,Jest,Webpack和Babel构建的React应用程序(不使用Create React App)。尝试运行“ yarn jest”时,出现以下错误:
我尝试删除所有软件包并重新添加它们。它不能解决此问题。我看过类似的问题和文档,但仍然有些误解。我什至遵循了另一本从头开始设置此环境的指南,但我的代码仍然收到此问题。
依赖项包括...
"dependencies": {
"@babel/plugin-transform-runtime": "^7.6.2",
"@babel/polyfill": "^7.6.0",
"babel-jest": "^24.9.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-test-renderer": "^16.11.0",
"source-map-loader": "^0.2.4"
},
"devDependencies": {
"@babel/core": "^7.6.0",
"@babel/preset-env": "^7.6.0",
"@babel/preset-react": "^7.0.0",
"@types/enzyme": "^3.9.2",
"@types/enzyme-adapter-react-16": "^1.0.5",
"@types/jest": "^24.0.13",
组件的导入行...
import * as React from "react";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import HomePage from "./components/pages";
import {
Footer,
Header,
Navigation,
} from "./components/shared";
测试文件...
import * as React from "react";
import * as renderer from "react-test-renderer";
import App from "../App";
it("Renders the Footer correctly", () => {
const tree = renderer
.create(<App />)
.toJSON();
expect(tree).toMatchSnapshot();
});
我希望能够在我的组件中使用命名的导入,而不会引起测试的失败。如果我仅通过解决方案使用默认导入,则似乎可以解决该问题,但我宁愿不走那条路。
require
代替import
,仅用于此文件。