Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use --changedSince and --onlyChanged in jest?

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.

like image 746
Pravin Avatar asked Aug 10 '18 13:08

Pravin


2 Answers

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.

like image 113
Saugat Avatar answered Sep 23 '22 02:09

Saugat


you can use '-o' flag to run changed test file in the watch mode. https://jestjs.io/docs/en/cli

jest -o
like image 44
Suben Saha Avatar answered Sep 23 '22 02:09

Suben Saha