【问题标题】:Imports failing in React 16 tests with enzyme and jest导入在 React 16 测试中失败的酶和玩笑
【发布时间】:2018-03-31 14:55:28
【问题描述】:

在升级到 React 16 之前,我进行了很多正常工作的测试,以及对 enzime 和 jest 的相应升级。我关注the instructions 并将这两个文件添加到我的 package.json 中的 jest 配置中

"jest": {
  "globals": {
    "rootLevel": "/portal"
  },
  "moduleNameMapper": {
    "components": "<rootDir>/../components",
    "\\.(jpg|jpeg|png|gif|svg|woff|woff2)$": "<rootDir>/_tests/__mocks/fileMock.js"
  },
  "setupFiles": [
    "<rootDir>/_tests/__config/shim.js",
    "<rootDir>/_tests/__config/enzyme-setup.js"
  ]
}

但我仍然没有看到我的测试运行正常。我的大多数测试都抛出了与此相同的错误:

Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in.

Check the render method of `WrapperComponent`.
at invariant (node_modules/fbjs/lib/invariant.js:42:15)
...
at mount (node_modules/enzyme/build/mount.js:19:10)
at Object.<anonymous> (_tests/MyTest.test.js:39:35)

所有触发的都是这一行:

const wrapper = mount(<MyComponent {...props} />)

据我所知,mount 在酶 3 中的作用仍然相同。我所有涉及渲染的测试都失败了。我错过了什么?

【问题讨论】:

    标签: reactjs jestjs enzyme


    【解决方案1】:

    问题是模块名称映射器中缺少$

    "components$": "<rootDir>/../components",
    

    【讨论】:

    • 我在您发布的 URL 中没有看到任何对“组件”的引用。否则,此文件应使用 create-react-app 自动生成。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 2022-07-10
    • 2021-05-02
    相关资源
    最近更新 更多