10

I'm using lint-staged with Jest testing framework to test only changed files from last commit as described in this blog.

My configuration is as below:

"src/**/*.{ts}": [
  "prettier --write",
  "tslint --fix --project .",
  "jest --bail --findRelatedTests",
  "git add"
]

I also want to generate coverage-report for only changed files. To do this, I have to put list of changed files in multiple places.

jest --bail --findRelatedTests <spaceSeparatedListOfSourceFiles> --collectCoverageFrom=<glob>

Using lint-staged, how can I limit both test and coverage-report only for changed files?

skyboyer
  • 22,209
  • 7
  • 57
  • 64
özüm
  • 1,166
  • 11
  • 23

2 Answers2

7

I didn't know about lint-staged. I'll give it a try. I looked for this a while ago.

Have you tried?

"jest --bail --coverage --findRelatedTests",

Jest docs say that findRelatedTests can be used together with --coverage to include a test coverage for the source files, no duplicate --collectCoverageFrom arguments needed.

Should work but I haven't tried.

bamse
  • 4,243
  • 18
  • 25
  • v22 did not have this. It seems to be added as of jest v23. I couldn't verify, since I didn't switch to v23, but according to jest docs, your answer should solve my problem. Thank you. – özüm Jul 23 '18 at 08:44
  • Should be `--coverage` not `---coverage` – Chidi Williams Feb 23 '19 at 01:06
  • 4
    Did it work for anyone? I am using `Jest 25.5.2` but it's still calculating coverage for all the files despite `--findRelatedTests` gives only 5 files. – Debajit Majumder Aug 04 '21 at 15:00
  • @DebajitMajumder did you solve it? – Tal Rofe Feb 12 '22 at 19:45
  • I am using `--collectCoverageOnlyFrom $MODIFIED_FILES` where `$MODIFIED_FILES` is the list of files returned by `git diff --name-only --diff-filter=AM $TARGETREMOTE/$TARGETBRANCH $SOURCEREMOTE/$SOURCEBRANCH -- 'src/*.js'`. – Debajit Majumder Feb 14 '22 at 05:09
0

it works correctly when you define the collectCoverageFrom parameter as follows

--collectCoverageFrom="<rootDir>/src/components/Home/index.js"

for multiple files you can define each as above

--collectCoverageFrom="<rootDir>/src/components/Home/index.js" --collectCoverageFrom="<rootDir>/src/components/Home/utils.js"

or you can define it as a glob

--collectCoverageFrom="<rootDir>/src/components/Home/*"
Ahmet Şimşek
  • 1,391
  • 1
  • 14
  • 24