Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To integrate TypeScript language with Cypress code coverage plugin, you can follow the below steps:

  1. Install the @cypress/code-coverage plugin and istanbul-lib-report package.
npm install --save-dev @cypress/code-coverage istanbul-lib-report
  1. Create a tsconfig.test.json file in the root directory with the following configuration:
{
  "compilerOptions": {
    "target": "es5",
    "lib": ["es6", "dom"],
    "types": ["cypress"],
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "dist",
    "strict": true,
    "sourceMap": true
  },
  "include": ["**/*.ts"],
  "exclude": ["node_modules", "cypress/**/*.ts"]
}
  1. In the cypress/plugins/index.js file, add the following code:
const cypressTypeScriptPreprocessor = require('./cy-ts-preprocessor')

module.exports = (on) => {
  on('file:preprocessor', cypressTypeScriptPreprocessor)
  require('@cypress/code-coverage/task')(on, { istanbulLibReport: require('istanbul-lib-report') })
}
  1. Create a cy-ts-preprocessor.js file in the cypress/plugins directory with the following code:
const preprocessor = require('cypress/typescript-preprocessor')

module.exports = (on) => {
  on('file:preprocessor', preprocessor)
}
  1. Finally, run your Cypress tests with coverage by adding the coverage flag:
npx cypress run --spec cypress/integration/myTest.spec.ts --headed --coverage

By following these steps, you can integrate TypeScript language with Cypress code coverage plugin for generating code coverage reports.