---
url: /howto/setting-up-profiler-tga/nodejs.md
---

# How to Record Test Coverage for JavaScript and TypeScript NodeJS Applications

This article describes how test coverage information can be recorded for a NodeJS application using [*IstanbulJS*](https://istanbul.js.org/).
It applies both to JavaScript and TypeScript applications.
It can be used for all types of tests, including unit tests, automated E2E tests and manual tests.

For frontend JavaScript/TypeScript code, please use our tutorial for [coverage in the browser](../javascript/).

## Install Dependencies

::: tabs
\== NPM

```bash
npm install --save-dev nyc
```

For a TypeScript application also run:

```bash
npm install --save-dev @istanbuljs/nyc-config-typescript
```

\== Yarn

```bash
yarn add -D nyc
```

For a TypeScript application also run:

```bash
yarn add -D @istanbuljs/nyc-config-typescript
```

:::

:::tip Tests outside the CI/CD pipeline
Please note that if you are bundling your application and executing your tests on another system, e.g. a dedicated test environment, you will also need to install `nyc` in that environment.
E.g. by running the following on the test environment:

:::tabs
\== NPM

```bash
npm install -g nyc
```

\== Yarn

```bash
yarn global add nyc
```

:::

## TypeScript: Configure IstanbulJS

Create `.nycrc` in your project's root folder (next to `package.json`):

```json
{
    "extends": "@istanbuljs/nyc-config-typescript"
}
```

:::tip Tests outside the CI/CD pipeline
Please note that if you are bundling your application and executing your tests on another system, e.g. a dedicated test environment, you will also need to copy `.nycrc` to that environment.
:::

## Enable Source Maps

For TypeScript or if you use a bundler/minifier to transform/package your JS code:
enable source maps for all build steps.
Make sure that each step contributes to the same source map, otherwise the recorded coverage will reference incorrect line numbers.

::: tabs
\== TypeScript

```javascript
// tsconfig.json
{ compilerOptions: { sourceMap: true, ... }, ... }
```

See the [Typescript documentation](https://www.typescriptlang.org/tsconfig#inlineSourceMap) for more details and options.

\== Webpack

```javascript
// webpack.config.js
module.exports = { devtool: 'source-map', ... }
```

See the [Webpack documentation](https://webpack.js.org/configuration/devtool/) for more details on options to control the generation of source maps.

Please use the [`source-map-loader` package](https://webpack.js.org/loaders/source-map-loader/) to ensure that source maps from a previous transformation step are not lost.

\== Rollup

```javascript
// rollup.config.js
output: { sourceMap: true, ... }
```

See the [Rollup documentation](https://rollupjs.org/guide/en/#outputsourcemap) for more details and options.

\== esbuild

```javascript
// esbuild.js
require('esbuild').buildSync({
  entryPoints: ['app.ts'],
  sourcemap: 'inline',
  outfile: 'out.js',
})
```

The [esbuild documentation](https://esbuild.github.io/api/#sourcemap) provides details and options on producing source map files.
:::

:::tip Tests outside the CI/CD pipeline
Please note that if you are bundling your application and executing your tests on another system, e.g. a dedicated test environment, you will also need to copy the generated source maps (`.map` files) to that system.
:::

## Run Your Application with Coverage Recording

Run your application with `nyc` prepended to enable coverage recording at runtime:

:::tabs
\== Directly

```bash
nyc --reporter=lcovonly node src/index.js
```

\== With NPM

```bash
npx nyc --reporter=lcovonly node src/index.js
```

\== With Yarn

```bash
yarn run nyc --reporter=lcovonly node src/index.js
```

:::

Once your application shuts down, `nyc` generates the file `coverage/lcov.info` which contains the coverage information.

## Upload Coverage to Teamscale

The final step is to upload this coverage file to Teamscale.
This can be done by using the [`teamscale-upload` tool](/howto/uploading-external-data/#upload-via-command-line) with `--format LCOV`.

## Troubleshooting

### Teamscale shows the wrong or no lines as covered

Please check your source maps.
These are used by `nyc` to map coverage data from your compiled JavaScript code back to the original source code.
If the source maps are not correct, the coverage report will contain incorrect file names or line numbers.
