Enzyme需要配置适配器,但没有找到

kur*_*kan 9 javascript reactjs jestjs enzyme

我在调用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';
Run Code Online (Sandbox Code Playgroud)

我的setupTests.js文件看起来像这样:

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

configure({ adapter: new Adapter() });
Run Code Online (Sandbox Code Playgroud)

并在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"
    }
},
Run Code Online (Sandbox Code Playgroud)

但问题是 - 我正在使用react-16和酶适配器反应-15对我来说无关紧要.即使我添加酶适配器反应-15以防万一 - 错误仍然存​​在.


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

Vik*_*kur 5

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