Ts-jest: It generates remapped coverage only for main.ts

Created on 30 Jan 2017  路  27Comments  路  Source: kulshekhar/ts-jest

I have a Typescript project and I'm facing issues generating the remapped coverage.
It creates only the coverage for main.ts instead that for all the files - the original coverage has all the files (but with wrong lines, of course).

This is the relevant part of my package.json:

{
  "watch": {
    "pretest": {
      "patterns": "src",
      "extensions": "ts"
    },
    "start": {
      "patterns": "src",
      "extensions": "ts"
    }
  },
  "scripts": {
    "prestart": "tsc && ./scripts/localModuleUpdate.sh",
    "start": "node dist/main",
    "pretest": "tslint 'src/**/*.ts'; echo",
    "test": "jest",
    "watch": "npm-watch",
    "postinstall": "typings install && ./scripts/localModuleUpdate.sh",
    "coverage": "jest --no-cache --coverage"
  },
  "dependencies": {
    "body-parser": "^1.15.2",
    "cors": "^2.8.1",
    "csv": "^1.1.0",
    "express": "^4.14.0",
    "lodash": "^4.17.2",
    "mongoose": "^4.7.4",
    "typescript": "^2.1.4",
    "validator": "^6.2.1"
  },
  "devDependencies": {
    "jest": "^18.0.0",
    "npm-watch": "^0.1.6",
    "sinon": "^1.17.7",
    "supertest": "^2.0.1",
    "ts-jest": "^18.0.2",
    "tslint": "^4.1.1",
    "typings": "^2.1.0"
  },
  "jest": {
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js"
    ],
    "transform": {
      "^.+\\.(ts|tsx)$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
    },
    "testRegex": "/__tests__/.*\\.(ts|tsx)$",
    "testResultsProcessor": "<rootDir>/node_modules/ts-jest/coverageprocessor.js",
    "collectCoverageFrom": [
      "src/**/*.tsx",
      "src/**/*.ts"
    ],
  }
}

and this is tsconfig.json:

{
  "compilerOptions": {
    "module": "CommonJS",
    "target": "ES6",
    "noResolve": false,
    "noImplicitAny": false,
    "strictNullChecks": true,
    "noUnusedParameters": true,
    "noUnusedLocals": true,
    "outDir": "dist"
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.d.ts",
    "typings/**/*.d.ts"
  ],
  "exclude": [
    "src/f-*/*"
  ]
}

I run the tests with:

npm coverage

Any suggestion?

Most helpful comment

@danieldiekmeier You're welcome!

@japhar81 is the one who deserves all the credit for this fix though :)

All 27 comments

Same was happening to me. My workaround:

  • First delete coverage folder
  • Run jest --watch or jest --coverage --no-cache, but not watch and coverage at the same time. In my case it was messing up things.

That doesn't generate a remapped folder, just the normal one but with the right lines (in my case also not right lines, but better ones, check this comment for more info)

@alexjoverm thanks for the feedback.

Anyway, I already run jest without the --watch, and it builds the remapped folder but just with the main.ts, and the standard folder has the usual wrong lines.

Ops, sorry I thought you had the same problem than me, but I see is a different thing :S

@rpadovani what's the behavior if you use "target": "ES5" in tsconfig.json?

@kulshekhar I am using "target": "ES6" in tsconfig.json

I'm getting the exact same issue and I have "target": "ES5" in tsconfig.json.

@rfgamaral can you create a minimal repo that reproduces this issue?

@kulshekhar Sorry it took me a while but it was not easy to get a minimal repo producing this issue. I'm not entirely sure the following repo reproduces it but I believe it does.

Please try this: ts-jest-issue-104-demo.zip.

If you look into tests/components/Application.spec.tsx there's only one test and one assertion:

expect(shallow(<Application />)).toMatchSnapshot();

Having this line commented out, build/coverage/remapped/html/components will be generated. But if the line is not commented, the components folder won't be generated.

I hope this helps you pinpoint the issue... :)

@rfgamaral I downloaded the zipped file, extracted it and executed npm install, followed by npm test.

It didn't exhibit the behavior you've specified. I suspect there might be minor settings issues in the attached project because the build folder isn't created (with and without that line commented out)

@kulshekhar You have to execute npm test -- --coverage.

@rfgamaral thanks. I don't use coverage so I wasn't aware!

I downloaded it again and executed npm install followed by npm test -- --coverage. The components folder was generated.

However, when I comment out that line, run test, uncomment that line and run test again, the components folder isn't generated. This is probably the issue you're facing.

Running the same command with --no-cache, however, seems to fix the issue. This is mentioned at the bottom of this section in the readme.

Does this fix the issue for you?

@kulshekhar It did work on the demo project but not on the real one.

I don't know the details of --no-cache but it feels odd to me that I have to run the coverage command with that flag every time for it to produce full coverage reports. Doesn't fell right. Still, like I said, this didn't fix it on my real project. And unfortunately I cannot share that one, not yet at least.

@rfgamaral from what I gather, the requirement of using --no-cache arises due to the way jest is structured. As far as I know, there's no way around this yet.

However, since I'm not very familiar with the part that handles coverage (I haven't ever used that), I'll leave this issue open. Hopefully someone will pick this one up.

@kulshekhar But it will be hard to debug this without a proper project actually displaying this issue even with the --no-cache flag. Hopefully I'll be able to open-source my own soon and post here unless someone does it first :)

Thanks for your time.

So, after reading #113 I tried to downgrade the module and now it works, so my problem is the version of jest

Tried downgrading and it worked for me to:

位 npm update
+-- @types/[email protected]
+-- [email protected]
`-- [email protected]

This issue and #113 should probably be merged into one. Not sure which should prevail :D

Here's my minimal repro.

Rename src/client/redux/reducer.test.ts to src/client/redux/reducer.test.ts.tmp and the remapped folder is generated.

It also ignores the CLI config option and uses the configuration in package.json, if any.

I'm hoping that once facebook/jest#2290 is done, ts-jest will be able to leverage that for more reliable coverage reports. That will probably address a lot of coverage related issues that are currently open!

I've just upgraded ts-jest to use the latest version of jest (19.0.0). In the process, I came across a similar issue and fixed it. Can you please check if the latest version resolves this issue?

I've just published a new version on npm thanks to #132

@rpadovani can you check if this has fixed this issue for you

@kulshekhar The latest version did indeed solve the issue for me. Thank you for the fix and for this project in general! :)

@danieldiekmeier You're welcome!

@japhar81 is the one who deserves all the credit for this fix though :)

Thanks :) I am out of office until Wed, I'll try it asap and if it works I'll close the bug :)

I don't need to use the --runInBand flag to make the code coverage work! :tada:

I no longer have this issue with the latest published version. Thanks everyone involved :)

Indeed it works like a charm right now :)

Thanks for your work @kulshekhar and @japhar81 !

Was this page helpful?
0 / 5 - 0 ratings