I want to use changedSince and onlyChanged flag for jest. I changed lots of tests and also files that the tests depends on. And according to the documentation of jest cli flags:
changedSince: Runs tests related the changes since the provided branch. If the current branch has diverged from the given branch, then only changes made locally will be tested.
onlyChanged: Alias -o. Attempts to identify which tests to run based on which files have changed in the current repository. Only works if you're running tests in a git/hg repository at the moment and requires a static dependency graph (ie. no dynamic requires).
But when I run jest --changedSince=master
or jest --onlyChanged
it runs 0 tests without giving anything in the terminal at all.
jest --listTests
lists all the tests. However when I run jest --listTests --changedSince=master
or jest --listTests --onlyChanged
, it lists no test.
I found --changedSince
and --onlyChanged
to be a little funky too. However, what you can do is use lint-staged and Jest's --findRelatedTests
and add it to a pre-commit hook.
--findRelatedTests {spaceSeparatedListOfSourceFiles}: Find and run the tests that cover a space separated list of source files that were passed in as arguments. Useful for pre-commit hook integration to run the minimal amount of tests necessary. Can be used together with --coverage to include a test coverage for the source files, no duplicate --collectCoverageFrom arguments needed.
Here is the change you need to make in your package.json
file.
{
"scripts": {
"test": "CI=true jest",
"test:staged": "CI=true jest --env=jsdom --findRelatedTests"
}
}
And,
"lint-staged": {
"src/**/*.js": [
"test:staged",
"git add"
]
}
This way, on every commit, Jest will run tests for related source files only. Don't forget to set the CI=true
environment variable or your tests will run in watch mode and never end.
If you still want to run the full suite of tests npm test
will do the trick. Also, it's better to have a CI environment to run all your tests when you push to a branch or send a pull request.
you can use '-o' flag to run changed test file in the watch mode. https://jestjs.io/docs/en/cli
jest -o
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With