Jest Cookbook
Type-check in a separate CI step with ts-jest
Since Knapsack Pro runs Jest multiple times, you can speed up tests execution by moving type-checking (isolatedModules
) and diagnostics
to a separate CI step. On larger codebases, it increases execution time by more than 50%.
{
"globals": {
"ts-jest": {
"diagnostics": false,
"isolatedModules": true
}
}
}
Speed up tests with --runInBand
Since Knapsack Pro runs Jest multiple times, you can speed up tests execution with --runInBand
. This way tests run serially in the current process, rather than creating a worker pool of child processes that run tests:
npx knapsack-pro-jest --runInBand
Use a Jest config file
To filter tests use KNAPSACK_PRO_TEST_FILE_PATTERN
.
You can pass it with a command-line argument:
npx knapsack-pro-jest --config=jest.config.e2e.js
Generate code coverage reports
Each CI node executes multiple batches of tests in Knapsack Pro Queue Mode, and each batch starts a new Jest process. By default, a coverage report generated by each batch overwrites the previous one, which can lead to incomplete coverage data.
To avoid this, you can configure Knapsack Pro to generate a separate coverage report for each batch by setting the following environment variable:
export KNAPSACK_PRO_COVERAGE_DIRECTORY=coverage
npx knapsack-pro-jest --coverage
This ensures each batch creates a unique subfolder for its coverage report, preventing overwrites:
coverage
├── 04c9ea40-4477-11ea-8397-c539e4406df8
│ ├── clover.xml
│ ├── coverage-final.json
│ ├── lcov-report
│ │ ├── a.js.html
│ │ ├── base.css
│ │ ├── block-navigation.js
│ │ ├── index.html
│ │ ├── prettify.css
│ │ ├── prettify.js
│ │ ├── sort-arrow-sprite.png
│ │ └── sorter.js
│ └── lcov.info
└── 0c584590-4477-11ea-8397-c539e4406df8
├── clover.xml
├── coverage-final.json
├── lcov-report
│ ├── b.js.html
│ ├── base.css
│ ├── block-navigation.js
│ ├── index.html
│ ├── prettify.css
│ ├── prettify.js
│ ├── sort-arrow-sprite.png
│ └── sorter.js
└── lcov.info
You can merge the reports with this script.
Generate XML reports
You can generate jest-junit reports with:
export JEST_JUNIT_UNIQUE_OUTPUT_NAME=true
npx knapsack-pro-jest --ci --reporters=jest-junit
Knapsack Pro will generate one XML reports for each batch of tests executed on the CI node. Some CI providers (e.g., GitLab CI) can merge multiple XML files from parallel CI nodes.
Run a subset of tests
To run a subset of your test suite you can use the KNAPSACK_PRO_TEST_FILE_*
environment variables: