1

I'm using jest in a monorepo. I would like to generate a Cobertura report when testing a some of my projects.

Jest.config.base.js

module.exports = {
  preset: '@vue/cli-plugin-unit-jest/presets/typescript-and-babel',
  transform: {
    'vee-validate/dist/rules': 'babel-jest',
    '.*\\.(vue)$': 'vue-jest',
    '^.+\\.(ts|tsx)$': 'ts-jest',
  },
  testMatch: [
    '**/*.(spec|test).(js|jsx|ts|tsx)',
  ],
  testEnvironmentOptions: {
    // Allow test environment to fire onload event
    // See https://github.com/jsdom/jsdom/issues/1816#issuecomment-355188615
    resources: 'usable',
  },
  reporters: [
    'default',
    [
      'jest-trx-results-processor',
      {
        outputFile: './coverage/test-results.trx',
        defaultUserName: 'user name to use if automatic detection fails',
      },
    ],
  ],
  moduleFileExtensions: [
    'js',
    'ts',
    'json',
    'vue',
  ],
  testURL: 'http://localhost/',
  snapshotSerializers: [
    'jest-serializer-vue',
  ],
  runner: 'groups',
};

Jest.config (root)

const baseConfig = require('./jest.config.base')

module.exports = {
  ...baseConfig,
  projects: [
    '<rootDir>/apps/my-app',
    '<rootDir>/apps/my-app-copy',
    '<rootDir>/libs/my-lib',
  ],
  coverageDirectory: '<rootDir>/coverage/',
};

Jest.config (my-app)

const baseConfig = require('../../jest.config.base');
const packageJson = require('./package.json');

module.exports = {
  ...baseConfig,
  transformIgnorePatterns: [],
  roots: [
    '<rootDir>/src',
  ],
  name: packageJson.name,
  displayName: packageJson.name,
};

I did not paste other jest.config to save space, but they are similar.

What works

If I run jest --coverage --coverageReporters=cobertura, it will generate a Cobertura report, but all my projects will be tested.

What does not work

If I run jest --projects apps/my-app apps/my-app-copy --coverage --coverageReporters=cobertura, only test-results.trx is generated.

Question

How could I test only 2 projects out of 3, and generate a single Cobertura report for those?

1 Answer 1

0

I'm working on something similar in our monorepo and I was able to generate the cobertura report installing jest-junit and defining the reporters in the root jest.config like this

coverageReporters: ['html', 'text', 'text-summary', 'cobertura'],

Then I run jest --coverage

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.