jest - coverage report now showing all files
Asked Answered
P

2

8

I am manually setting-up for jest.

My repo struture:

my-proj
 - src
  - components
   ...
   - accordion
    - index.jsx
  - tests
   - components
    - accordion.test.js
  - setupTests.js
 - package.json

When I run npm run test-coverage to get the test coverage, it only returns the files I've tested but not all files inside /src.

enter image description here

package.json

  "scripts": {
    "test": "jest --watchAll",
    "test-coverage": "jest --coverage", 
  },
  "jest": {
    "moduleDirectories": [
      "node_modules",
      "src"
    ],
    "moduleNameMapper": {
      "\\.(css|scss)$": "identity-obj-proxy"
    },
    "setupFilesAfterEnv": [
      "<rootDir>/src/setupTests.js"
    ],
    "snapshotSerializers": [
      "enzyme-to-json/serializer"
    ]
  },
Portamento answered 4/2, 2022 at 10:49 Comment(1)
I only see just one test file accordion.test.js. What did you expect?Kenrick
F
7

I've just stumbled upon your question having the same problem as you do. Try running jest --coverage --watchAll. That should cover all files coverage and show which aren't, even without test files.

Feverwort answered 21/3, 2022 at 19:21 Comment(2)
Amazing, it was what was missing in my case. I didn't know that the coverage report only shows tested components by default. Seems counterintuitive for a coverage test...Censor
Doesn't this also put jest in watch mode though?Clue
S
4

to get all files report, you have to pass --collectCoverageFrom='app/**/*.js'

where app/ is your main folder, also, you can try to put all of these settings in your jest.config.js file

collectCoverageFrom: ['app/**/*.js'],

jest config file - collect coverage

Soraya answered 26/4, 2023 at 14:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.