Call Enzyme.configure in the jasmine helper

I use Enzyme with React 16 through Karma, Jasmine and webpack. I have a test where I configure the Enzyme adapter in the beforeEach function:

import React from 'react';
import Enzyme, { shallow } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

import App from 'app';

describe('App', () => {
  let wrapper;

  beforeEach(() => {
    Enzyme.configure({ adapter: new Adapter() });

    wrapper = shallow(<App />);
  });

  it('should render an h1 header', () => {
    expect(wrapper.type()).toBe('h1');
  });

  it('should render Hello, World!', () => {
    expect(wrapper.text()).toEqual('Hello, World!');
  });
});

It all works great.

Since I don’t want to do this in every Enzyme test, I tried moving this code to the physe_helper.js file:

import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

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

I added the file to the list of files downloaded by Karma and created it through webpack (so I can use the ES6 format). I checked (via console.log and run the debugger) that the helper loads, but when the tests in the test file are executed, I get:

   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 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 http://airbnb.io/enzyme/docs/installation/index.html

Any suggestions what can I do wrong?

+4
source share

Source: https://habr.com/ru/post/1688561/


All Articles