If you run Jest and get EMFILE: too many open files, your system or test watcher is hitting file descriptor limits.
This error is common in bigger monorepos and watch mode.
Why this happens
Jest can open many files at once when it scans tests, watches changes, and transforms modules. If your OS limit is low, you get EMFILE.
Typical triggers:
- too many workers in parallel
- running watch mode with huge file trees
- very broad test include patterns
Quick fix in Jest config
Start with lower concurrency and less aggressive watching.
/** @type {import('jest').Config} */
module.exports = {
maxWorkers: '50%',
watchPathIgnorePatterns: ['/node_modules/', '/.next/', '/dist/'],
testPathIgnorePatterns: ['/node_modules/', '/.next/', '/dist/'],
};
In many projects, this already removes the error.
Add CI-friendly command
For CI, run Jest in non-watch mode and serial if needed:
jest --runInBand --ci
--runInBand runs tests in one process, slower but stable when file limit is strict.
Check your open file limit
On macOS/Linux:
ulimit -n
If value is very low (for example 256), you may hit limits quickly.
Temporary increase in current shell:
ulimit -n 4096
Then run tests again.
Reduce watch scope
If you only need a specific suite while debugging, run focused tests:
jest src/__tests__/auth.test.ts
This avoids scanning all test files and lowers file handle usage.
Good baseline config
If you want a safer default:
maxWorkers: '50%'- no watch mode in CI
- ignore build output folders
- run targeted tests during local debug