Jest is a popular JavaScript testing framework that provides a zero-configuration setup out of the box. However, customizing its behavior often requires tweaking the jest.config.js
file. Here's a guide to help you understand and modify Jest configurations effectively.
Key Configuration Options 📝
testMatch
: Specifies which files Jest should treat as test files.
✅ Default:**/*.(spec|test).js?(on)
testEnvironment
: Sets the environment for tests.
🚀 Common options:jsdom
,node
,react-native
setupFiles
: Runs files at the beginning of each test.
📁 Example:['./setupTests.js']
moduleNameMapper
: Maps module names to file paths.
🔗 Useful for mocking modules like^\.(svg|css|styl|less|sass|scss)$
Configuration File Location 📁
- Create a file named
jest.config.js
in the root of your project. - For TypeScript projects, use
jest.config.ts
instead.
How to Use 🧪
- Basic Setup
module.exports = { testMatch: ['**/*.(spec|test).js?(on)'], testEnvironment: 'node', };
- Advanced Customization
Explore more options → /en/docs/jest/configuration
📚 This link guides you through environment-specific configurations.
Tips 🔍
- Use
jest --config
to specify a custom config file. - Always test your configuration changes with
npm test
.