Skip to main content

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%.

jest.config.js
{
"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

caution

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

export KNAPSACK_PRO_COVERAGE_DIRECTORY=coverage

npx knapsack-pro-jest --coverage

Knapsack Pro will generate one coverage report for each batch of tests executed on the CI node. To merge the reports you may consider 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.