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.
Asked
Active
Viewed 1,035 times
1 Answers
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
-
Would that reimport enzyme every time a test is executed? – kevin parra May 24 '23 at 15:50