Overview
While running tests in CircleCI jobs you may run into different errors that are the result of the high resources consumption.
Many testing frameworks support running tests in parallels across available CPU cores within a single executor.
By default, most testing tools will auto-detect the number of available CPUs and use all of them resulting in CPU contention, OOM kills and other resources consumption problems.
You may find complete specs for Docker resource classes here - https://circleci.com/docs/reference/configuration-reference/
Solution
npm/Jest
Jest determines worker count via the --maxWorkers flag or the maxWorkers config key.
Option 1: CLI flag
- run:
name: Run Jest tests
command: npx jest --maxWorkers=2Option 2: Use percentage
This caps workers at 50% of detected CPUs.
- run:
name: Run Jest tests
command: npx jest --maxWorkers=50%Alternatively, you may configure parallelism in jest.config.js or package.json files
Option 3: jest.config.js
module.exports = {
maxWorkers: 2,
};Option 4: package.json
{
"scripts": {
"test": "jest --maxWorkers=2"
}
}Playwright
Playwright uses workers to run test files concurrently. Configuration looks similar to jest.
Option 1: CLI Flag
- run: name: Run Playwright tests command: npx playwright test --workers=2
Option 2: playwright.config.ts
import { defineConfig } from '@playwright/test';export default defineConfig({
workers: process.env.CI ? 2 : undefined,
});