首页 > 解决方案 > 错误:酶内部错误:酶需要配置适配器,但没有找到

问题描述

我遇到了一个问题,我无法弄清楚它与什么有关。事实上,在 Stockoverflow 中也有类似的问题,但没有帮助。当我起诉 react 16.2 时,我无法理解如何修复以及为什么它需要酶适配器反应 15 我收到以下错误:

     FAIL  src\components\Card.test.js (7.032s)    
Expect to render Card component

          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';

          To find out more about this, see https://airbnb.io/enzyme/docs/installation/index.html


      at validateAdapter (node_modules/enzyme/build/validateAdapter.js:16:11)
      at getAdapter (node_modules/enzyme/build/getAdapter.js:27:36)
      at makeShallowOptions (node_modules/enzyme/build/ShallowWrapper.js:357:45)
      at new ShallowWrapper (node_modules/enzyme/build/ShallowWrapper.js:408:19)
      at shallow (node_modules/enzyme/build/shallow.js:21:10)
      at Object.<anonymous> (src/components/Card.test.js:8:30)
          at new Promise (<anonymous>)
      at node_modules/p-map/index.js:46:16
      at processTicksAndRejections (internal/process/task_queues.js:97:5)

Card.js 如下

    import React from "react";
import { shallow, mount, render } from "enzyme";

const Card = ({ name, email, id }) => {
  return (
    <div className="bg-light-green">
      // some working code
    </div>
  );
};

export default Card;

Card.test.js 如下

 import { shallow } from "enzyme";
    import React from "react";
    import Card from "./Card";
    
    //console.log(shallow(<Card />));
    
    it("Expect to render Card component", () => {
      expect(shallow(<Card />).length).toEqual(1);
    });

测试文件如下

import React from "react";
import Adapter from "enzyme-adapter-react-16";
import { shallow, configure } from "enzyme";

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

所有文件都在同一个 src/component 文件夹中

另外,这是 package.json

{
  "name": "robofriends",
  "version": "0.1.0",
  "private": true,
  "homepage": "https://some working link",
  "dependencies": {
    "gh-pages": "^1.1.0",
    "react": "^16.2.0",
    "react-dom": "^16.13.1",
    "react-redux": "^5.0.7",
    "react-scripts": "1.0.17",
    "redux": "^3.7.2",
    "redux-logger": "^3.0.6",
    "redux-thunk": "^2.2.0",
    "tachyons": "^4.9.0"
  },
  "scripts": {
    "predeploy": "npm run build",
    "deploy": "gh-pages -d build",
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "devDependencies": {
    "enzyme": "^3.11.0",
    "enzyme-adapter-react-16": "^1.15.2"
  }
}

标签: javascriptenzyme

解决方案


您应该在某处声明您的酶配置文件。

开个玩笑,您可以将其添加到您的 package.json

"jest": {
  "setupFilesAfterEnv": ["<rootDir>pathToYourEnzymeConfig.js"]
}

或者使用 jest 配置文件做同样的事情 https://jestjs.io/docs/en/configuration


推荐阅读