Jest: When mapCoverage is true, covered files have duplicated root paths [Jest 20]

Created on 7 May 2017  路  9Comments  路  Source: facebook/jest

Do you want to request a feature or report a bug?

What is the current behavior?

Running the following project: https://github.com/aurelia/skeleton-navigation/tree/master/skeleton-typescript-webpack.

See the CI build output, the step named: cd skeleton-typescript-webpack && yarn start -- test.all:

[jest] ------------------|----------|----------|----------|----------|----------------|
[jest] File              |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
[jest] ------------------|----------|----------|----------|----------|----------------|
[jest] All files         |    14.49 |     2.44 |    26.83 |    14.14 |                |
[jest]  src              |        0 |        0 |        0 |        0 |                |
[jest]   blur-image.ts   |        0 |        0 |        0 |        0 |... 316,317,318 |
[jest]   main.ts         |        0 |        0 |        0 |        0 |... 68,70,71,76 |
[jest]  src/src          |    90.91 |       50 |    78.57 |    89.13 |                |
[jest]   app.ts          |      100 |      100 |      100 |      100 |                |
[jest]   child-router.ts |      100 |      100 |      100 |      100 |                |
[jest]   users.ts        |    94.74 |       50 |       75 |    92.86 |             30 |
[jest]   welcome.ts      |       80 |       50 |    66.67 |    77.78 |    19,20,24,25 |
[jest] ------------------|----------|----------|----------|----------|----------------|

The 2 uncovered files have correct root directory: src, while the covered and mapped files have src/src as their directory, which is incorrect. They are all contained in a single src directory.

To reproduce:

git clone [email protected]:aurelia/skeleton-navigation.git
cd skeleton-typescript-webpack
yarn
yarn test # or simply run 'jest'

What is the expected behavior?

The coverage file paths are correct.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

  • Running on both OS X and Linux CI.
  • Yarn 0.24
  • Node 7.10
  • Jest 20.0

Configuration (full package.json):

  "jest": {
    "modulePaths": [
      "<rootDir>/src",
      "<rootDir>/node_modules"
    ],
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "transform": {
      "^.+\\.(ts|tsx)$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
    },
    "testRegex": "\\.spec\\.(ts|js)x?$",
    "setupFiles": [
      "<rootDir>/test/jest-pretest.ts"
    ],
    "testEnvironment": "node",
    "moduleNameMapper": {
      "aurelia-(.*)": "<rootDir>/node_modules/$1"
    },
    "collectCoverage": true,
    "collectCoverageFrom": [
      "src/**/*.{js,ts}",
      "!**/*.spec.{js,ts}",
      "!**/node_modules/**",
      "!**/test/**"
    ],
    "coverageDirectory": "<rootDir>/test/coverage-jest",
    "coverageReporters": [
      "json",
      "lcov",
      "text",
      "html"
    ],
    "mapCoverage": true
  },

Most helpful comment

Closing. Also, we're deprecating mapCoverage because it's not necessary now.

All 9 comments

cc @dmitriiabramov

This doesn't fix the problem, move on to next comment

I fixed this by explicitly adding roots to the jest config, ie:

  "roots": [
    "<rootDir>/src",
    "<rootDir>/test/unit" //my test folder
  ],

This is basically just shotgun debugging and shouldn't count as a solution unless someone within jest comes with an amazing explanation on why this is this way :)

Edit: Seems this only was a temporary fix, I'll try digging out some more debug info of what might cause it.

Ooookay, after som debugging I think I got it.

I had

"testEnvironment": "node",

in my jest-config and relied on my tsconfig which had

"module": "es2015",

I changed it to be

"testEnvironment": "jsdom",

in jest-config and added the lines

"globals": {
    "__TS_CONFIG__": {
      "module": "commonjs"
    }
  },

to force the ts transpile to use commonjs modules. This works _for now_, but I feel this is reaaaally brittle.

Can you confirm that this works for you @niieani ?

No, I can't, because I need to use testEnvironment: node for my use case, so that doesn't solve my problem.

I see. Well, they can hopefully narrow it down by my debugging :)

The issue seems to be gone when you properly pass transformOptions as last parameter for process function of preprocessor (this was changed in Jest 20).

For example with ts-jest (they fixed it in v20.0.3):

const {process} = require('ts-jest/preprocessor.js');
module.exports.process = (src, path, config, transformOptions) => {
  // some code here if necessary
  return process(src, path, config, transformOptions);
};

Closing. Also, we're deprecating mapCoverage because it's not necessary now.

Was this page helpful?
0 / 5 - 0 ratings