Test Setup Files
Question
What is a Jest setup file?
Hint
Setup files configure globals, mocks, and polyfills.
Explanation
A Jest setup file (configured in jest.config.js setupFiles or setupFilesAfterEach) runs before tests to prepare the environment. Uses: 1) Polyfill APIs (fetch, IntersectionObserver). 2) Configure global mocks (jest.mock). 3) Set up test utilities (custom render for RTL). 4) Configure environment variables. 5) Reset database state. 6) Import test CSS. setupFiles runs before the test framework is installed (no expect/describe). setupFilesAfterEach runs after (has Jest globals). Setup files keep test files clean by centralizing common configuration. Separate setup for unit vs integration tests.
Related Resources
Related Lessons
- Setup/Teardown
Learn setup.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.