使用酶和开胃液进行React 16测试的进口失败

时间:2017-10-19 14:18:11

标签: reactjs jestjs enzyme

我有很多测试在升级到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中仍然有效。我所有涉及渲染的测试都失败了。我错过了什么?

1 个答案:

答案 0 :(得分:0)

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

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