Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Typescript bad coverage #1907

Closed
CarlosSoares opened this issue Oct 10, 2016 · 17 comments
Closed

[BUG] Typescript bad coverage #1907

CarlosSoares opened this issue Oct 10, 2016 · 17 comments

Comments

@CarlosSoares
Copy link

CarlosSoares commented Oct 10, 2016

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

What is the current behavior?
Right now when I execute the jest command and it never give me 100% of coverage.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal repository on GitHub that we can npm install and npm test.
I created an simple example with the bug: Repository
Is just npm install and after that npm test and if you guys see the coverage report it appears 87.5% and if you guys open the coverage report in the browser you guys see the issue that I'm having.
screen shot 2016-10-10 at 5 58 40 pm

What is the expected behavior?
That the coverage it will be at 100%

@beckend
Copy link

beckend commented Oct 10, 2016

I have the same problem.
screen shot 2016-10-10 at 21 16 58

@cpojer
Copy link
Member

cpojer commented Oct 10, 2016

cc @DmitriiAbramov

@jsynowiec
Copy link
Contributor

I'm not getting same results. Coverage seems to be reported correctly, but I'm not using React or tsx files. Don't know if this helps.

screenshot 2016-10-11 01 09 54

@CarlosSoares
Copy link
Author

@jsynowiec when I try with an simple class without using react only with ts extension the coverage works fine.

@jsynowiec
Copy link
Contributor

I have some more complex classes that extend others and still, coverage works. Only difference that I can see right now is the React library and jsx.

Do you have type definitions?

@CarlosSoares
Copy link
Author

Yes, but I don't think that is the issue, but I don't know for sure.
If you download the repository you can see the issue that I'm having.
The funny thing is if I use the var MyClass = React.createClass({}) it works fine, but if I use export class MyClass extends React.Component<{}, {}> {} the report is wrong. I already tried change the tests in jest and it fails also.

@kwonoj
Copy link
Contributor

kwonoj commented Oct 12, 2016

I think this is similar case to #1584, jest supports preprocessor to transpile codes but coverage remapping's on consumer's own in case of TypeScript in opposite to babel which jest supports natively.

I was able to create proof-of-concept code snippet does remapping code coverage to generate correct test coverage, shows below differences.

raw coverage

remapped coverage

I hope jest supports hook for coverage processing as same as preprocessing compilation allows to create / or existing preprocessor integrates coverage remapping as well. (Maybe it's already available but I didn't noticed?)

Anyone interested can look code snippet at https://github.com/kwonoj/jest-typescript-coverage.

@CarlosSoares
Copy link
Author

I got an solution for this, basically what I do is using an custom typescript processor and change the target from ES5 to ES6.
Always run with the option --no-cache and should start working fine.

const tsc = require('typescript');

const compilerOptions = require('./tsconfig.json').compilerOptions;
compilerOptions.target = "ES6";

module.exports = {
  process(src, path) {
    if (path.endsWith('.ts') || path.endsWith('.tsx')) {
      return tsc.transpile(
        src,
        compilerOptions
      );
    }
    return src;
  },
};

@beckend
Copy link

beckend commented Oct 12, 2016

I was using target ES6 to begin with and it's not working.

@CarlosSoares
Copy link
Author

That's weird then, can you try replicate in a clean repository an give the link to see if I can help in some way?

@beckend
Copy link

beckend commented Oct 12, 2016

You can use the active repo react-themeable-ts.
You need:
npm -g i jest-cli
npm i
jest --coverage
And you can find coverage folder in root.

@CarlosSoares
Copy link
Author

@beckend after tried in my own application you're right and didn't solve my solution.

Could this to be done in karma or another tool?

@kwonoj
Copy link
Contributor

kwonoj commented Oct 16, 2016

Anyone interested in this topic can refer kulshekhar/ts-jest#25 as well, I've managed code coverage remapping works with testresultsprocessor. Unfortunately this is not ideal approach as given interface from jest lacks of few things.

  • jest does not allow return post processed results to back to jest runtime
  • testresults delivered by testresultsprocessor does not contain empty coverage from uncovered files
  • jest configuration is not passed into processor, so process need to own responsibility if it need to figure out current configuration of jest

@cpojer
Copy link
Member

cpojer commented Oct 24, 2016

TypeScript support in Jest is not "officially" supported by us. I recommend using ts-jest like @kwonoj mentioned. As somebody pointed out above, if the preprocessor prints code with different line numbers etc., Jest cannot do the proper remapping on its own so this is strictly out of the scope of Jest.

@kwonoj feel free to send PRs to solve any of the problems you outlined above if that will help you have better coverage support.

@alexgorbatchev
Copy link

ts-jest README says to add "mapCoverage": true to jest config. This has fixed the mapping for me.

@zackify
Copy link
Contributor

zackify commented Sep 28, 2017

@alexgorbatchev thanks for this, it's not mentioned well enough in the docs!

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants