1
votes

Trying to get a login unit test working but it keeps giving me the error

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

This is my unit test

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

1

1 Answers

1
votes

As the error is telling you to do, you need to configure an adapter for enzyme like so:

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

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

in let's say /src/tests/setupTests.js, and then tell Jest about it in the jest.config.json like this:

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

and then run jest with specifying the config file:

jest --config=jest.config.json

It's all in the installation docs: https://airbnb.io/enzyme/docs/installation/