Reduce pre-approval pipeline duration and cost by running only related jest tests
Objective
Reduce pre-approval pipeline duration and cost by running only related jest tests.
Approach
jest
has --findRelatedTests
option to run related tests to a given list of files.
--findRelatedTests Find related tests for a list of source files
that were passed in as arguments. Useful for
pre-commit hook integration to run the minimal
amount of tests necessary. [boolean]
We may be able to use this to run minimal versions of jest
similar to #336063 (closed).
There may be other alternative approaches, but jest --findRelatedTests
seems to provide what we need out of the box.
Related to #222369 (closed)
References:
- https://jestjs.io/docs/cli#--findrelatedtests-spaceseparatedlistofsourcefiles
- https://stackoverflow.com/questions/44066996/how-does-jest-findrelatedtests-work-under-the-hood
Implementation detail
Prior to a Merge Request approval, the CI would run jobs named jest minimal
which runs jest --findRelatedTests
on the files changed in the Merge Request. This would run a smaller set of tests and is expected to shorten the pipeline duration.
After the Merge Request is approved, the CI would run jest
as before, which would run the full suite of jest
test files.
Some files and directories are recognised to have potential for test failures, so when these are changed, the CI would still run all jest
tests:
- "{package.json,yarn.lock}"
- "babel.config.js"
- "jest.config.{base,integration,unit}.js"
- "config/helpers/**/*.js"
- "vendor/assets/javascripts/**/*"
- "{,ee}/app/assets/**/*.graphql"
FAQ
What if I need to run everything in jest?
You could add pipeline:run-all-jest label to the merge request and start a new pipeline. With the label, the CI would run jest
instead of jest minimal
jobs, thus covering all the test files.