3

I am using Jest to test a React app. I keep repeating the same import statements for some common libraries/functionalities I need in every test. I was wondering if there was any way to tell Jest to import those libraries before every test.

raquelhortab
  • 430
  • 4
  • 13

1 Answers1

1

I achieved it by importing the necessary libraries in setupTests.js, which, if existent, is called by default before every test file. In order to make the imported library available to the tests, I had to do the following:

Let's say I need mount from enzyme to be imported in every test.

// setupTests.js
import { mount } from 'enzyme';

global.mount = mount;

This way, I can directly use mount in any test file without needing to import it.

raquelhortab
  • 430
  • 4
  • 13