【问题标题】:Enzyme expects an adapter to be configured, but found noneEnzyme 期望配置一个适配器,但没有找到
【发布时间】:2018-06-25 01:19:32
【问题描述】:

我在调用 enzym's mount 函数时遇到问题。它说:

Enzyme Internal Error: Enzyme expects an adapter to be configured, but found none. To
      configure an adapter, you should call `Enzyme.configure({ adapter: new Adapter() })`
      before using any of Enzyme's top level APIs, where `Adapter` is the adapter
      corresponding to the library currently being tested. For example:

      import Adapter from 'enzyme-adapter-react-15';

我的setupTests.js 文件如下所示:

import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

configure({ adapter: new Adapter() });

在 package.json 中:

"jest": {
    "testEnvironment": "jest-environment-jsdom-global",
    "setupFiles": [
        "<rootDir>/jestConfigs/setupTests.js"
    ],
    "moduleNameMapper": {
        "^.+\\.(css|scss)$": "<rootDir>/jestConfigs/CSSStub.js",
        "^.+\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/jestConfigs/fileStub.js",
        "/common-mms/(.+)": "<rootDir>/App/common-mms/$1"
    }
},

但问题是 - 我使用的是 react-16,而酶适配器反应 15 与我无关。即使我添加酶适配器反应 15 以防万一 - 错误仍然存​​在。


更新: 如果我将 setupTests.js 的内容复制到每个测试文件的开头 - 一切正常! 如果我将console.log(1) 放入 setupTests - 它实际上会打印出来!这意味着该文件实际上是在开玩笑初始化时引入的。

【问题讨论】:

  • 至少请帮助我了解如何调试它
  • 你有没有安装包enzyme-adapter-react-16
  • 是的,我已经安装了。现在我将更新问题 - 问题是如果我将我的配置从配置文件复制并粘贴到每个测试文件的开头 - 一切正常。

标签: javascript reactjs jestjs enzyme


【解决方案1】:

您必须在编写测试用例的文件中导入 setupTests.js。 这个link已经回答了这个问题

【讨论】:

    【解决方案2】:

    您不应该在所有测试中复制适配器设置。事实上,这个问题之前已经回答过:Enzyme expects an adapter to be configured

    简而言之,解决方案取决于您是否使用create-react-app 设置项目并弹出应用程序。

    对于非弹出的 create-react-app 项目,您只需添加您在src/ 下提到的setupTests.js 文件。 对于 Typescript 用户,这应该是 src/setupTests.ts

    如果你已经从 create-react-app 中退出或者在没有它的情况下设置了项目,你需要在你的 jest config 中添加这一行(无论是哪一行):"setupFilesAfterEnv": ["&lt;rootDir&gt;/src/setupTests.js"]

    【讨论】:

      猜你喜欢
      • 2018-10-17
      • 2020-02-20
      • 1970-01-01
      • 2021-09-08
      • 2013-06-07
      • 2019-11-13
      • 2021-01-20
      相关资源
      最近更新 更多