Skip to content

Cypress code coverage not instrumenting in angular18 #893

Closed

Description

@MohamedSahir

I am writing a e2e test case with cypress for angular application, i am not able to find proper documentation for cypress code coverage for angular, i can able to do with react, but facing issues in angular18. any additional configuration need to do ,

enable source map in angular json.
image
versions:
cypress@13.15.0
node version:18.20.2
"@cypress/code-coverage": "^3.12.48",
angular 17 ,18 tested
image

could you please update cypress code coverage much more clearer for technology wise in the documentation,

image

here is cypress config.ts

import { defineConfig } from 'cypress';
import { addCucumberPreprocessorPlugin } from '@badeball/cypress-cucumber-preprocessor';
import createBundler from '@bahmutov/cypress-esbuild-preprocessor';
import { createEsbuildPlugin } from '@badeball/cypress-cucumber-preprocessor/esbuild';
import codeCoverageTask from '@cypress/code-coverage/task';
import registerCodeCoverageTasks from '@cypress/code-coverage/task';

export default defineConfig({
  e2e: {
    specPattern: 'cypress/e2e/**/*.feature',
    supportFile: 'cypress/support/e2e.ts',
    
    async setupNodeEvents(
      on: Cypress.PluginEvents,
      config: Cypress.PluginConfigOptions
    ) {
      await addCucumberPreprocessorPlugin(on, config);
      
      on('file:preprocessor', createBundler({
        plugins: [createEsbuildPlugin(config)],
      }));

       // Register code coverage task
       codeCoverageTask(on, config);

       // Ensure coverage is enabled
       config.env.coverage = true;
       config.instrument = true;
       config.experimentalSourceRewriting = true;
 
       return config;
     // require('@cypress/code-coverage/task')(on, config)
    //  registerCodeCoverageTasks(on, config);
    //  return config;
      // codeCoverageTask(on, config);
      
      // Use type assertion for config.env
      // (config.env as any).coverage = true;
      // (config.env as any).codeCoverage = true;
      // (config.env as any).codeCoverageTasksRegistered = true;
    
      
      // return config;
    },
    baseUrl: 'http://localhost:4700',
    
  },
  component: {
    devServer: {
      framework: 'angular',
      bundler: 'webpack',
    },
    specPattern: '**/*.ts',
  },
  env: {
    coverage: true,
    disableGpu: true,
    codeCoverage: true,
    codeCoverageTasksRegistered: true,
  },
  experimentalSourceRewriting: true,
});

tried both

and babel.config.json or .babelrc

{
  "presets": ["@babel/preset-env", "@babel/preset-typescript"],
   "plugins": ["babel-plugin-istanbul"]
  //"plugins": ["istanbul"]
}

i can able to generate cucumber report but cypress not instrumenting

added in plugin index.ts file
const codeCoverageTask = require('@cypress/code-coverage/task');

module.exports = (on:any, config:any) => {
  codeCoverageTask(on, config);
  return config;
};

nycrc

{
  "extends": "@istanbuljs/nyc-config-typescript",
    "all": true,
    "check-coverage": true,
    "excludeAfterRemap": false,
  
    "include": [
      "src/**/*.ts"
  
    ],
    "exclude": [
      "**/*.spec.ts",
      "**/*.d.ts",
      "cypress/**/*",
      "src/test.ts",
      "src/environments/*"
    ],
    "extension": [
      ".ts"
    ],
    "require": [
      "ts-node/register"
    ],
    "reporter": [
      "html",
      "lcov",
      "text-summary"
    ],
    "report-dir": "./coverage-e2e"
  }

nothing working could you please help me on this ,struggling here for two weeks.

code coverage report generated with no coverage.

i want both reports cucumber and cypress .cucumber can able to generate , but cypress not instrumenting

Activity

  1. ducthang-vu commented on Feb 5, 2025

    @ducthang-vu

    MohamedSahir: did you find a solution in the meantime? We are having same issue.

  2. iherbak commented on Jul 17, 2025

    @iherbak

    Any solutions?

  3. jennifer-shehane commented on Nov 2, 2026

    @jennifer-shehane
    Member

    The plugin doesn't instrument your code itself. It only collects coverage from window.__coverage__, and in this setup that object never exists because the app is never instrumented:

    • Angular CLI ignores .babelrc and babel.config.json, so babel-plugin-istanbul never runs.
    • Angular 17+ builds with the esbuild-based application builder, which has no hook for Istanbul.
    • config.instrument, env.coverage and experimentalSourceRewriting don't instrument anything.

    To instrument the app, switch the build and serve targets to @angular-builders/custom-webpack and add babel-loader with babel-plugin-istanbul as a post loader, in a separate coverage configuration. The steps are in the README under Instrument an Angular app, added in #1011. They were tested on Angular 18.

    Closing as a setup question. If window.__coverage__ is still missing after following those steps, open a new issue with a minimal reproduction repo.

  4. github-actions commented on Nov 2, 2026

    @github-actions

    🎉 This issue has been resolved in version 4.1.3 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions