开玩笑/反应-TypeError:无法读取未定义的属性'touchStart'

时间:2020-02-20 13:58:52

标签: 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)

有人可以帮助解决此问题。

1 个答案:

答案 0 :(得分:0)

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

"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
  },