|
| 1 | +## Create NX Workspace |
| 2 | + |
| 3 | +``` |
| 4 | +npx create-nx-workspace@latest |
| 5 | +``` |
| 6 | + |
| 7 | +Reply "NO" standalone components and "YES" to routing |
| 8 | + |
| 9 | +## Install Dependencies |
| 10 | + |
| 11 | +``` |
| 12 | +npm i ngx-build-plus @cypress/code-coverage @istanbuljs/nyc-config-typescript @jsdevtools/coverage-istanbul-loader istanbul-lib-coverage nyc --save-dev |
| 13 | +``` |
| 14 | + |
| 15 | +## Create Webpack config |
| 16 | + |
| 17 | +Create a `webpack.coverage.js` file in the root of your nx workspace. And add this config: |
| 18 | + |
| 19 | +``` |
| 20 | +const path = require('path'); |
| 21 | +
|
| 22 | +module.exports = { |
| 23 | + module: { |
| 24 | + rules: [ |
| 25 | + { |
| 26 | + test: /\.(js|ts)$/, |
| 27 | + loader: '@jsdevtools/coverage-istanbul-loader', |
| 28 | + options: { esModules: true }, |
| 29 | + enforce: 'post', |
| 30 | + include: [path.join(__dirname, '{your-app-directory}')], |
| 31 | + exclude: [/\.(cy|spec)\.ts$/, /node_modules/], |
| 32 | + }, |
| 33 | + ], |
| 34 | + }, |
| 35 | +}; |
| 36 | +``` |
| 37 | + |
| 38 | +## Modify your app project.json |
| 39 | + |
| 40 | +Open your app `project.json` and add this to `targets`: |
| 41 | + |
| 42 | +``` |
| 43 | +"serve-nx-demo-app-e2e": { |
| 44 | + "executor": "ngx-build-plus:dev-server", |
| 45 | + "defaultConfiguration": "", |
| 46 | + "options": { |
| 47 | + "browserTarget": "{your-app-directory}:build:development", |
| 48 | + "extraWebpackConfig": "./webpack.coverage.js", |
| 49 | + "port": 3000 |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +## Modify your e2e project.json |
| 55 | + |
| 56 | +Add a new configuration to add coverage inside `targets.e2e.configurations`: |
| 57 | + |
| 58 | +``` |
| 59 | +"coverage": { |
| 60 | + "devServerTarget": "{your-app-directory}:serve-nx-demo-app-e2e" |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +## Import cypress code coverage library |
| 65 | + |
| 66 | +Open `apps/your-app-directory-e2e/src/support/e2e.ts` and add this line: |
| 67 | + |
| 68 | +``` |
| 69 | +import '@cypress/code-coverage/support'; |
| 70 | +``` |
| 71 | + |
| 72 | +## Update cypress.config.ts |
| 73 | + |
| 74 | +Open `apps/your-app-directory-e2e/cypress.config.ts` and replace it with this: |
| 75 | + |
| 76 | +``` |
| 77 | +import { defineConfig } from 'cypress'; |
| 78 | +import { nxE2EPreset } from '@nx/cypress/plugins/cypress-preset'; |
| 79 | +
|
| 80 | +export default defineConfig({ |
| 81 | + e2e: { |
| 82 | + ...nxE2EPreset(__dirname), |
| 83 | + setupNodeEvents(on, config) { |
| 84 | + // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 85 | + require('@cypress/code-coverage/task')(on, config); |
| 86 | + return config; |
| 87 | + } |
| 88 | + } |
| 89 | +}); |
| 90 | +``` |
| 91 | + |
| 92 | +Run `nx e2e nx-demo-app-e2e --configuration=coverage` to generate your coverage then open in your browser `apps/your-app-directory-e2e/coverage/lcov-report/index.html` to view your coverage results. |
0 commit comments