【问题标题】:Enzyme Internal Error: Enzyme expects an adapter to be configured, but found noneEnzyme 内部错误:Enzyme 需要配置适配器,但没有找到
【发布时间】:2020-02-20 02:53:44
【问题描述】:

试图让登录单元测试正常工作,但它一直给我错误

Enzyme 内部错误:Enzyme 需要配置适配器,但没有找到。 要配置适配器,您应该调用Enzyme.configure({ adapter: new Adapter() }) 在使用任何 Enzyme 的顶级 API 之前,其中 Adapter 是适配器 对应于当前正在测试的库。例如:

从 'enzyme-adapter-react-15' 导入适配器;

这是我的单元测试

import React from 'react';
import { shallow, mount, render } from 'enzyme';
import Login from './components/login.js';


describe('Login Test Suite', () => {

  it('should render the form', () => {
    const wrapper = shallow(<Login />);

    expect(wrapper.find('form.login').exists()).toBe(true);
    expect(wrapper.find('#Username').length).toEqual(1);
    expect(wrapper.find('#password').length).toEqual(1);
  })
})

describe('Username Test Suite', () => {

  it('should change the state of the Login component', () => {

    const wrapper = shallow(<Login />);
    wrapper.find('#Username').simulate('blur',
      {
        target: { name: 'Username', value: 'adastest' }
      });

    expect(wrapper.state('Username')).toEqual('adastest');
  })
})

describe('Password Test Suite', () => {

  it('should change the state of the Login component', () => {

    const wrapper = mount(<Login />);
    wrapper.find('#password').simulate('blur',
      {
        target: { name: 'password', value: 'adastest' }
      });

    expect(wrapper.state('password')).toEqual('adastest');
  })
})

【问题讨论】:

    标签: reactjs jestjs enzyme


    【解决方案1】:

    正如错误告诉你的那样,你需要像这样为酶配置一个适配器:

    // setup file
    import { configure } from 'enzyme';
    import Adapter from 'enzyme-adapter-react-16';
    
    configure({ adapter: new Adapter() });
    

    假设/src/tests/setupTests.js,然后在jest.config.json 中告诉 Jest,如下所示:

    {
      "setupFiles": [
        "<rootDir>/src/tests/setupTests.js"
      ]
    }
    

    然后运行 ​​jest 并指定配置文件:

    jest --config=jest.config.json

    这一切都在安装文档中: https://airbnb.io/enzyme/docs/installation/

    【讨论】:

      猜你喜欢
      • 2018-06-25
      • 2018-10-17
      • 1970-01-01
      • 1970-01-01
      • 2016-05-16
      • 1970-01-01
      • 2020-09-08
      • 2014-01-03
      • 2019-02-06
      相关资源
      最近更新 更多