|
| 1 | +# Code Coverage |
| 2 | + |
| 3 | +This document explains how code coverage instrumentation works in Rocket.Chat's build and CI pipeline. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Coverage is collected during E2E test runs (API, UI, Livechat) to measure how much of the server-side code is exercised by tests. The instrumentation uses [Istanbul](https://istanbul.js.org/)-compatible tooling, which injects counters (`__coverage__`) into the compiled code at build time. |
| 8 | + |
| 9 | +## Architecture |
| 10 | + |
| 11 | +The coverage pipeline has three components: |
| 12 | + |
| 13 | +1. **Build-time instrumentation** - injects coverage counters into the code during Meteor build |
| 14 | +2. **Runtime collection** - the `rocketchat:coverage` Meteor package collects `__coverage__` data on process exit |
| 15 | +3. **CI reporting** - test workflows merge coverage data and upload reports |
| 16 | + |
| 17 | +``` |
| 18 | +Build (SWC + plugin) --> Run tests --> Process exit triggers report --> Merge & upload |
| 19 | +``` |
| 20 | + |
| 21 | +## Build-time instrumentation |
| 22 | + |
| 23 | +### Modern build stack (SWC) |
| 24 | + |
| 25 | +Rocket.Chat uses Meteor's modern build stack with [SWC](https://swc.rs/) as the transpiler. For coverage builds, the [`swc-plugin-coverage-instrument`](https://github.com/kwonoj/swc-plugin-coverage-instrument) plugin is injected into `.swcrc` at build time. |
| 26 | + |
| 27 | +This is configured in `.github/actions/meteor-build/action.yml`: |
| 28 | + |
| 29 | +```yaml |
| 30 | +env: |
| 31 | + BABEL_ENV: ${{ inputs.type }} # "production" or "coverage" |
| 32 | +``` |
| 33 | +
|
| 34 | +When `BABEL_ENV=coverage`, the build script: |
| 35 | + |
| 36 | +1. Adds `rocketchat:coverage` to `.meteor/packages` |
| 37 | +2. Injects `swc-plugin-coverage-instrument` into `.swcrc` via a node script: |
| 38 | + |
| 39 | +```js |
| 40 | +const swcrc = JSON.parse(fs.readFileSync('./apps/meteor/.swcrc', 'utf8')); |
| 41 | +swcrc.jsc.experimental = swcrc.jsc.experimental || {}; |
| 42 | +swcrc.jsc.experimental.plugins = swcrc.jsc.experimental.plugins || []; |
| 43 | +swcrc.jsc.experimental.plugins.push(['swc-plugin-coverage-instrument', {}]); |
| 44 | +fs.writeFileSync('./apps/meteor/.swcrc', JSON.stringify(swcrc, null, 2) + '\n'); |
| 45 | +``` |
| 46 | + |
| 47 | +This approach ensures the same build pipeline (SWC) is used for both regular and coverage builds, avoiding behavioral differences between build modes. |
| 48 | + |
| 49 | +### Legacy build stack (Babel) |
| 50 | + |
| 51 | +Before the modern build stack, coverage was handled via `babel-plugin-istanbul` configured in `.babelrc`: |
| 52 | + |
| 53 | +```json |
| 54 | +{ |
| 55 | + "env": { |
| 56 | + "coverage": { |
| 57 | + "plugins": [ |
| 58 | + ["istanbul", { "exclude": ["**/*.spec.js", "**/*.test.js"] }] |
| 59 | + ] |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +This section is still present in `.babelrc` as a fallback for files that fall back to Babel compilation (e.g., SWC-incompatible code). |
| 66 | + |
| 67 | +## Runtime collection: `rocketchat:coverage` |
| 68 | + |
| 69 | +The `rocketchat:coverage` Meteor package (`apps/meteor/packages/rocketchat-coverage/`) is only added to the build during coverage runs. It: |
| 70 | + |
| 71 | +1. Registers a `process.on('exit')` handler |
| 72 | +2. Reads `globalThis['__coverage__']` (populated by the instrumentation) |
| 73 | +3. Generates a coverage report using `istanbul-lib-coverage` and `istanbul-reports` |
| 74 | + |
| 75 | +Configuration via environment variables: |
| 76 | + |
| 77 | +| Variable | Description | Example | |
| 78 | +|---|---|---| |
| 79 | +| `COVERAGE_DIR` | Output directory for reports | `/tmp/coverage/api` | |
| 80 | +| `COVERAGE_FILE_NAME` | Report filename | `api-1.json` | |
| 81 | +| `COVERAGE_REPORTER` | Istanbul reporter format | `json`, `lcov` | |
| 82 | + |
| 83 | +## CI workflow |
| 84 | + |
| 85 | +Coverage is collected in the `ci-test-e2e.yml` workflow: |
| 86 | + |
| 87 | +1. **Build**: `meteor-build` action runs with `type: coverage`, producing a Docker image with instrumented code |
| 88 | +2. **Test**: E2E tests run against the instrumented server. On each test shard: |
| 89 | + - `COVERAGE_DIR`, `COVERAGE_FILE_NAME`, and `COVERAGE_REPORTER` are set |
| 90 | + - When the Rocket.Chat process exits after tests, the coverage plugin writes a JSON report |
| 91 | +3. **Merge**: `nyc merge` combines per-shard JSON reports into a single coverage file |
| 92 | +4. **Upload**: Coverage data is uploaded to Codecov |
| 93 | + |
| 94 | +## Local development |
| 95 | + |
| 96 | +To build with coverage locally: |
| 97 | + |
| 98 | +```bash |
| 99 | +cd apps/meteor |
| 100 | +
|
| 101 | +# Inject the SWC coverage plugin into .swcrc |
| 102 | +node -e " |
| 103 | + const fs = require('fs'); |
| 104 | + const swcrc = JSON.parse(fs.readFileSync('.swcrc', 'utf8')); |
| 105 | + swcrc.jsc.experimental = { plugins: [['swc-plugin-coverage-instrument', {}]] }; |
| 106 | + fs.writeFileSync('.swcrc', JSON.stringify(swcrc, null, 2)); |
| 107 | +" |
| 108 | +
|
| 109 | +# Add the coverage package |
| 110 | +echo -e "rocketchat:coverage\n" >> .meteor/packages |
| 111 | +
|
| 112 | +# Set env vars and run |
| 113 | +COVERAGE_DIR=/tmp/coverage COVERAGE_FILE_NAME=local.json COVERAGE_REPORTER=lcov yarn dev |
| 114 | +``` |
| 115 | + |
| 116 | +Remember to restore `.swcrc` and `.meteor/packages` after testing. |
| 117 | + |
| 118 | +## Dependencies |
| 119 | + |
| 120 | +| Package | Purpose | |
| 121 | +|---|---| |
| 122 | +| `swc-plugin-coverage-instrument` | SWC plugin for Istanbul-compatible instrumentation | |
| 123 | +| `istanbul-lib-coverage` | Coverage map creation (used by `rocketchat:coverage`) | |
| 124 | +| `istanbul-lib-report` | Report context creation | |
| 125 | +| `istanbul-reports` | Report formatters (json, lcov, etc.) | |
0 commit comments