Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot generate Jest coverage report

I am working on a Next.js project in Typescript and am currently using the SWC compiler. As a result, I'm using @swc/jest for my tests. All my tests are passing, but the coverage report always comes back empty. Here's what my jest.config.js looks like:

module.exports = {
  'roots': ['<rootDir>/../src'],
  'moduleDirectories': ['node_modules', 'src'],
  'setupFilesAfterEnv': ['<rootDir>/setup-tests.js'],
  'coverageDirectory': '<rootDir>/../coverage',
  'verbose': true,
  'collectCoverage': true,
  'transform': {
    '^.+\\.(t|j)sx?$': [
      '@swc/jest',
      {
        'jsc': {
          target: 'es2021',
        },
        'sourceMaps': true,
      },
    ],
  },
  'collectCoverageFrom': [
    '<rootDir>/../src/**.{ts,js,tsx,jsx}',
    '!**/node_modules/**',
  ],
}

My file structure looks like:

.
├── coverage
├── jest
│   ├── jest.config.js
│   ├── setup-tests.js
├── src/
├── tsconfig.json

The output looks like this:

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |       0 |        0 |       0 |       0 |                   
----------|---------|----------|---------|---------|-------------------

Test Suites: 11 passed, 11 total
Tests:       25 passed, 25 total
Snapshots:   0 total
Time:        1.307 s
Ran all test suites.

I created an issue on @swc/jest, but I'm not sure if this is an issue with them or something else so I figured I'd ask here too.

like image 603
skon182 Avatar asked Nov 17 '25 13:11

skon182


1 Answers

Your 'collectCoverageFrom' value seems to me malformed in your jest config. You would use double asterisks ** to recursively all directories and sub-directories, and a single * to act as a wildcard (whether a directory name, or a file name).

To capture all files in all directories (no matter how many sub-directories) is:

src/**/*

If you want only js and ts files in all directories (no matter how many sub-directories):

src/**/*.{js,ts}

If you all files in only the src directory:

src/*

What you have in your config, matches a directory that ends with js, ts, etc.

like image 180
nugen.exe Avatar answered Nov 19 '25 08:11

nugen.exe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!