3

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.

juliomalves
  • 42,130
  • 20
  • 150
  • 146
skon182
  • 71
  • 1
  • 7

1 Answers1

2

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.

nugenjs
  • 155
  • 2
  • 10
  • Good catch @Nugen.exe I updated my config to this `collectCoverageFrom: ['/../src/**/*.{ts,js,tsx,jsx}']` but it still fails to generate the coverage report :/ Do you think this could be an issue with `@swc/jest`? – skon182 Jan 11 '22 at 16:06
  • @skon182 Prehaps is resolving to where your package.json exists (https://gaurav5430.medium.com/rootdir-is-automatically-filled-in-by-jest-to-be-the-directory-where-your-package-json-4e7ffce33232), unless you have one in your tests dir, which would explain how your tests are running. I can't tell it is @swc/jest issue. Seems like there is unexpected behavior in having rootDir as jest/ instead of node root. I found a few rigid structure logic in Jest, such as mocks have to be next to its source to auto mock. – nugenjs Jan 11 '22 at 17:10