W3cubDocs

/Enzyme

Using enzyme with Jest

Configure with Jest

To run the setup file to configure Enzyme and the Adapter (as shown in the Installation docs) with Jest, set setupFilesAfterEnv (previously setupTestFrameworkScriptFile) in your config file (check Jest's documentation for the possible locations of that config file) to literally the string <rootDir> and the path to your setup file.

{
  "jest": {
    "setupFilesAfterEnv": ["<rootDir>src/setupTests.js"]
  }
}

Jest version 15 and up

Starting with version 15, Jest no longer mocks modules by default. Because of this, you no longer have to add any special configuration for Jest to use it with enzyme.

Install Jest, and its Babel integrations, as recommended in the Jest docs. Install enzyme. Then, simply require/import React, enzyme functions, and your module at the top of a test file.

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

import Foo from '../Foo';

You do not need to include Jest's own renderer, unless you want to use it only for Jest snapshot testing.

Example Project for Jest version 15+

Jest prior to version 15

If you are using Jest 0.9 – 14.0 with enzyme and using Jest's automocking feature, you will need to mark react and enzyme to be unmocked in your package.json:

package.json:

{
  "jest": {
    "unmockedModulePathPatterns": [
      "node_modules/react/",
      "node_modules/enzyme/"
    ]
  }
}

If you are using a previous version of Jest together with npm3, you may need to unmock more modules.

© 2015 Airbnb, Inc.
Licensed under the MIT License.
https://airbnb.io/enzyme/docs/guides/jest.html