javascript - Jest/ react - TypeError : Cannot read property 'touchStart' of undefined

标签 javascript reactjs jestjs enzyme webpack-4

我已经使用 react、webpack 和 jest/enzyme 创建了项目来测试组件。 在 webpack 中为文件添加了别名,并在“moduleNameMapper”内的 jest.config.js 中添加了相同的别名。 但是在运行测试时出现以下错误:

测试套件运行失败

TypeError: Cannot read property 'touchStart' of undefined

  2 | // import ReactSixteenAdapter from 'enzyme-adapter-react-16';
  3 | // import Enzyme from 'enzyme';
> 4 | import Adapter from 'enzyme-adapter-react-16';
    | ^
  5 | import Enzyme, { shallow, configure } from 'enzyme';
  6 | import WelcomeBody from '../components/welcome/WelcomeBody';
  7 | 

  at Object.<anonymous> (node_modules/enzyme-adapter-react-16/src/ReactSixteenAdapter.js:64:36)
  at Object.<anonymous> (node_modules/enzyme-adapter-react-16/src/index.js:2:18)
  at Object.<anonymous> (app/src/__tests__/WelcomeBody.test.js:4:1)

有人可以帮忙解决这个问题吗。

最佳答案

如果您使用的是 TypeScript,请确保 tsconfig 文件中的“esModuleInterop”为真。 这是一个示例:

"compilerOptions": {
"target": "es2019",
    "moduleResolution": "node",
    "module": "commonjs",
    "lib": ["es2019"],
    "sourceMap": true,
    "outDir": "dist",
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "noImplicitThis": true,
    "resolveJsonModule": true,
    "alwaysStrict": true,
    "removeComments": true,
    "noImplicitReturns": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "baseUrl": ".",
    "paths": {
      "@src/*": ["./src/*"],
      "@test/*": ["./test/*"]
    },
    "rootDirs": [
      "./src",
      "./test"
    ],
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  },

关于javascript - Jest/ react - TypeError : Cannot read property 'touchStart' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60321647/

相关文章:

javascript - ajax错误仅发送第一个请求

javascript - 交叉口观察者动画和悬停效果不能一起使用吗?

testing - 如何使用带有 react-apollo 的模拟数据进行测试

javascript - JEST 测试不理解导入语句

javascript - 在 React 中调用组件时访问 props 时遇到问题

javascript - 从 ftp 文件夹连续音频广播

reactjs - 当组件重新渲染 React Material UI 时保留 Autocomplete 值

javascript - this.props.onChange 在渲染函数中返回?

javascript - 无法在 Next.js v13 中显示加载组件

javascript - 在 Jest 中模拟导入