Search code examples
jestjsmonorepo

jest coverageDirectory configuration for project inside monorepo


I have a typescript monorepo with jest. Their jest.config.js is

module.exports = {
  clearMocks: true,
  projects: ['<rootDir>/packages/**/jest.config.js'],
  collectCoverage: true,
  coverageReporters: [
    "text-summary",
    "lcov",
  ],
  preset: 'ts-jest',
  testEnvironment: 'node',
  testMatch: ['*.spec.ts', '*.spec.tsx']
}

At the server package I create the jest.config.js with

const { name } = require('./package.json');

module.exports = {
  displayName: name,
  name,
  'transform': {
    '^.+\\.ts$': 'ts-jest'
  },
  collectCoverageFrom: [
    '<rootDir>/src/**/*.ts'
  ],
  coverageDirectory: '<rootDir>/packages/server/src/tests/coverage',
}

But when I run jest it creates the coverage directory at the root of my monorepo.

I just try to use coverageDirectory: '<rootDir>/src/tests/coverage',, coverageDirectory: 'src/tests/coverage', and coverageDirectory: [__dirname, 'src', 'tests', 'coverage'].join('/'), without success.

The only way I could change the path of coverage directory was to specify it at the jest.config.js of the monorepo, but with this I am unable to specify a coverage directory to each project.

Somebody knows how can I specify a different coverage directory for each project at a monorepo?


Solution

  • You can only specify a different coverage directory for each project if you are running jest for each product individually. If you are using a global config (the jest.config.js at the root of your monorepo) then the same coverage settings are applied across them all, including the coverage output directory.