Nest: Code coverage in e2e tests

Created on 11 Oct 2019  路  3Comments  路  Source: nestjs/nest

Bug Report

Current behavior

Collecting coverage on e2e test results in no files covered.

Input Code

By using

nest new project-name

Then in test/jest-e2e.json add key "collectCoverage": true

{
  "moduleFileExtensions": ["js", "json", "ts"],
  "rootDir": ".",
  "testEnvironment": "node",
  "testRegex": ".e2e-spec.ts$",
  "transform": {
    "^.+\\.(t|j)s$": "ts-jest"
  },
  "collectCoverage": true
}

Then run

npm run test:e2e

Expected behavior

Code coverage should be shown from app.controller.ts and app.service.ts.

Possible Solution

It seems as the module fixture is not being imported for tracking by jest.

Environment


Nest version: 6.10.1

For Tooling issues:

  • Node version: v12.10.0
  • Platform: Mac
needs triage

Most helpful comment

I solved this as follows:

  1. Move jest-e2e.json from the test directory to the root directory of the project (where package.json is located)

  2. Change the contents of jest-e2e.json to the following:

{
    "moduleFileExtensions": ["js", "json", "ts"],
    "rootDir": ".",
    "testEnvironment": "node",
    "testMatch": ["<rootDir>/test/**/*.e2e-spec.{ts,js}"],
    "transform": {
        "^.+\\.(t|j)s$": "ts-jest"
    },
    "coverageDirectory": "./coverage-e2e"
}
  1. Change the test:e2e script inside package.json to:
"test:e2e": "jest --config ./jest-e2e.json"

Now running the test:e2e script with the --coverage flag should generate the required coverage inside the coverage-e2e directory.

All 3 comments

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.

This was solved by switching over jest to mocha and using yarn instead of npm.

I solved this as follows:

  1. Move jest-e2e.json from the test directory to the root directory of the project (where package.json is located)

  2. Change the contents of jest-e2e.json to the following:

{
    "moduleFileExtensions": ["js", "json", "ts"],
    "rootDir": ".",
    "testEnvironment": "node",
    "testMatch": ["<rootDir>/test/**/*.e2e-spec.{ts,js}"],
    "transform": {
        "^.+\\.(t|j)s$": "ts-jest"
    },
    "coverageDirectory": "./coverage-e2e"
}
  1. Change the test:e2e script inside package.json to:
"test:e2e": "jest --config ./jest-e2e.json"

Now running the test:e2e script with the --coverage flag should generate the required coverage inside the coverage-e2e directory.

Was this page helpful?
0 / 5 - 0 ratings