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

Partial code coverage or incorrect mapping #371

Closed
momsse opened this issue Nov 12, 2017 · 36 comments
Closed

Partial code coverage or incorrect mapping #371

momsse opened this issue Nov 12, 2017 · 36 comments

Comments

@momsse
Copy link

momsse commented Nov 12, 2017

  • Issue

Hi and thanks for all the job done for this library 👍

I have probably the same issue as #339 but with a single repo. The solution pointed in the issue is not suitable in my case.

I either have an incorrect code mapping on coverage without "mapCoverage": true or missing files when this option is removed + an almost correct mapping.

Incorrect mapping + all files covered

------------------------------|----------|----------|----------|----------|----------------|
File                          |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
------------------------------|----------|----------|----------|----------|----------------|
All files                     |    83.33 |      100 |    66.67 |       75 |                |
 rnTsWithJest                 |      100 |      100 |      100 |      100 |                |
  App.tsx                     |      100 |      100 |      100 |      100 |                |
  Home.tsx                    |      100 |      100 |      100 |      100 |                |
 rnTsWithJest/core/locales    |        0 |      100 |        0 |        0 |                |
  index.ts                    |        0 |      100 |        0 |        0 |              4 |
 rnTsWithJest/core/locales/en |      100 |      100 |      100 |      100 |                |
  common.ts                   |      100 |      100 |      100 |      100 |                |
  index.ts                    |      100 |      100 |      100 |      100 |                |
 rnTsWithJest/core/locales/fr |      100 |      100 |      100 |      100 |                |
  common.ts                   |      100 |      100 |      100 |      100 |                |
  index.ts                    |      100 |      100 |      100 |      100 |                |
------------------------------|----------|----------|----------|----------|----------------|

capture d ecran 2017-11-13 a 10 09 52

(Almost) Correct mapping + missing covered files

---------------------------|----------|----------|----------|----------|----------------|
File                       |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
---------------------------|----------|----------|----------|----------|----------------|
All files                  |    83.33 |      100 |    66.67 |    83.33 |                |
 rnTsWithJest              |      100 |      100 |      100 |      100 |                |
  App.tsx                  |      100 |      100 |      100 |      100 |                |
  Home.tsx                 |      100 |      100 |      100 |      100 |                |
 rnTsWithJest/core/locales |        0 |      100 |        0 |        0 |                |
  index.ts                 |        0 |      100 |        0 |        0 |              5 |
---------------------------|----------|----------|----------|----------|----------------|

capture d ecran 2017-11-13 a 10 10 18

  • Expected behavior

Having correct code mapping on coverage report without missing covered files.

Any help would be greatly appreciated.

  • Minipal repo

https://github.com/momsse/rnTsWithJest

@kulshekhar
Copy link
Owner

@momsse Can you please create a minimal repo that reproduces this issue? The linked repo isn't really minimal

@momsse
Copy link
Author

momsse commented Nov 12, 2017

@kulshekhar thanks for your fast reply 👍

I just shaved a lot of noise to make your work easier. Screenshots and reports have been updated too. Let me know if you need anything else to solve my issue.

@kulshekhar
Copy link
Owner

I'm getting other errors:

    Invariant Violation: Objects are not valid as a React child (found: object with keys {en, fr}). If you meant to render a collection of children, use an array instead.
        in Text (created by Text)
        in Text (created by Home)
        in View (created by View)
        in View (created by Home)
        in Home (created by App)
        in App
      
      at invariant (node_modules/fbjs/lib/invariant.js:44:15)
      at throwOnInvalidObjectType (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:2411:5)
      at reconcileChildFibers (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:3337:7)
      at reconcileChildrenAtPriority (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:3915:30)
      at reconcileChildren (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:3903:5)
      at updateHostComponent (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:4173:7)
      at beginWork (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:4436:16)
      at performUnitOfWork (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:6030:16)
      at workLoop (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:6159:26)
      at invokeGuardedCallback$1 (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:2003:10)

The cause appears to be at this line in Home. locales is an object which apparently React doesn't allow.

I suspect this error is what's causing this issue.

@momsse
Copy link
Author

momsse commented Nov 13, 2017

@kulshekhar just removed the error in https://github.com/momsse/rnTsWithJest/commit/001c85d4711703c53fcacea54762cd06aee0a171 without any change on coverage.

@kulshekhar
Copy link
Owner

I'm now getting the following error:

  console.error node_modules/fbjs/lib/warning.js:35
    Warning: PropTypes has been moved to a separate package. Accessing React.PropTypes is no longer supported and will be removed completely in React 16. Use the prop-types package on npm instead. (https://fb.me/migrating-from-react-proptypes)

  console.error node_modules/fbjs/lib/warning.js:35
    Warning: checkPropTypes has been moved to a separate package. Accessing React.checkPropTypes is no longer supported and will be removed completely in React 16. Use the prop-types package on npm instead. (https://fb.me/migrating-from-react-proptypes)

  console.error node_modules/fbjs/lib/warning.js:35
    Warning: React.createClass is no longer supported. Use a plain JavaScript class instead. If you're not yet ready to migrate, create-react-class is available on npm as a drop-in replacement. (https://fb.me/migrating-from-react-create-class)

Would it be possible for you to get the linked repo to a state where it shows just the wrong coverage without other errors?

@momsse
Copy link
Author

momsse commented Nov 13, 2017

@kulshekhar i just updated the repo. yarn test should give you a clean output without errors/warnings:

➜  rnTsWithJest git:(master) ✗ yarn test
yarn test v0.24.5
$ jest --coverage 
 PASS  __tests__/App.tsx
  ✓ renders correctly (125ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   1 passed, 1 total
Time:        1.324s, estimated 2s
Ran all test suites.
---------------------------|----------|----------|----------|----------|----------------|
File                       |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
---------------------------|----------|----------|----------|----------|----------------|
All files                  |       80 |      100 |      100 |       80 |                |
 rnTsWithJest              |      100 |      100 |      100 |      100 |                |
  App.tsx                  |      100 |      100 |      100 |      100 |                |
  Home.tsx                 |      100 |      100 |      100 |      100 |                |
 rnTsWithJest/core/locales |        0 |      100 |      100 |        0 |                |
  index.ts                 |        0 |      100 |      100 |        0 |              5 |
---------------------------|----------|----------|----------|----------|----------------|
✨  Done in 2.95s.

@kulshekhar
Copy link
Owner

@momsse thanks.

I've taken a look again and it seems that the output is correct. If I add statements to the language specific files (the ones missing from the report), they start showing up.

I think they aren't showing currently because they only contain stuff that is ignored by coverage (import,export,etc.).

@momsse
Copy link
Author

momsse commented Nov 14, 2017

@kulshekhar thanks for all the time you took to look at the issue 👍

I just pushed a branch es6 with es6 converted files. The output show clearly all the files with perfect mapping on the coverage. (https://github.com/momsse/rnTsWithJest/tree/es6)

➜  rnTsWithJest git:(es6) ✗ yarn test      
yarn test v0.24.5
$ jest --coverage 
 PASS  __tests__/App.jsx
  ✓ renders correctly (97ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   1 passed, 1 total
Time:        0.456s, estimated 1s
Ran all test suites.
------------------------------|----------|----------|----------|----------|----------------|
File                          |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
------------------------------|----------|----------|----------|----------|----------------|
All files                     |    83.33 |      100 |    66.67 |    83.33 |                |
 rnTsWithJest                 |      100 |      100 |      100 |      100 |                |
  App.jsx                     |      100 |      100 |      100 |      100 |                |
  Home.jsx                    |      100 |      100 |      100 |      100 |                |
 rnTsWithJest/core/locales    |        0 |      100 |        0 |        0 |                |
  index.js                    |        0 |      100 |        0 |        0 |              5 |
 rnTsWithJest/core/locales/en |      100 |      100 |      100 |      100 |                |
  common.js                   |      100 |      100 |      100 |      100 |                |
  index.js                    |      100 |      100 |      100 |      100 |                |
 rnTsWithJest/core/locales/fr |      100 |      100 |      100 |      100 |                |
  common.js                   |      100 |      100 |      100 |      100 |                |
------------------------------|----------|----------|----------|----------|----------------|
✨  Done in 1.56s.

capture d ecran 2017-11-14 a 08 07 19

We should not have a different coverage output when we use tsc + babel with ts-jest instead of using just babel. The inaccurate code highlight on the coverage is probably a clue that spot to something like a source map issue. Don't you think ?

Did you spot something about the inaccurate code highlight on coverage reports ?

@tamino-martinius
Copy link

I'm not sure if my problem is related, but i also have problems with the mapping of the coverage results for .ts files. This leads into wrong coverage results and all highlights having an offset.

screenshot 2017-11-14 10 51 31

You can reproduce it with my repo https://github.com/tamino-martinius/node-next-model/tree/typescript-experimental by running ./node_modules/.bin/jest --coverage and see the reports at /coverage_jest_raw

I found a solution to fix it for me by remapping the reports to the compiled js files at build folder of jest using this script https://github.com/tamino-martinius/node-next-model/blob/typescript-experimental/remap_coverage.js

The remapped html report looks like that on my project
screenshot 2017-11-14 10 52 13

@momsse
Copy link
Author

momsse commented Nov 15, 2017

@tamino-martinius thanks for the workaround, gonna take a look on it this weekend

@kulshekhar
Copy link
Owner

I just pushed a branch es6 with es6 converted files. The output show clearly all the files with perfect mapping on the coverage

Thanks. This should help.

We should not have a different coverage output when we use tsc + babel with ts-jest instead of using just babel.

I agree.

The inaccurate code highlight on the coverage is probably a clue that spot to something like a source map issue. Don't you think ?

I don't think this is a sourcemap issue. I'm going to spend some time on this now and hopefully be able to identify the cause

@kulshekhar
Copy link
Owner

@momsse I've sent a PR that has this working. Can you see if that works for you?

@momsse
Copy link
Author

momsse commented Nov 23, 2017

@kulshekhar thanks for you time again. And yeah the coverage is now working well. The problem is that i need the babel step to use the synthetic export magic (https://github.com/momsse/rnTsWithJest/blob/master/tsconfig.json#L9). Since i'm using a transformer that is doing those two build steps (tsc + babel) on my project (https://github.com/ds300/react-native-typescript-transformer). That's why i chose ts-jest in the first place.

@kulshekhar
Copy link
Owner

What's happening with the original setup is that when skipBabel isn't set to true (meaning ts-jest processes the output of typescript with babel), is that the sourcemaps are getting messed up. That's probably what's causing the incorrect coverage results.

@momsse
Copy link
Author

momsse commented Nov 23, 2017

@kulshekhar ok so there is no technical way to have ts + babel with correct coverage ? We have to choose ?

@kulshekhar
Copy link
Owner

I don't really use babel a lot so I'm not quite sure about this. Hopefully someone more familiar with babel might chime in.

/cc @GeeWee

@momsse
Copy link
Author

momsse commented Nov 23, 2017

Perhaps a note on the readme about this coverage issue could help others and save them time.

@GeeWee
Copy link
Collaborator

GeeWee commented Nov 24, 2017

I'm not actually sure how exactly the coverage reporter plays with babel. It should obviously be possible seeing as regular jest uses babel and has correct coverage, but I don't quite have the time to investigate :/

@johnbrett
Copy link

I'm having this issue currently, agree with your comment @GeeWee it should be possible to resolve this. I'm not familiar with the codebase though, any idea on where I should start to investigate this?

@GeeWee
Copy link
Collaborator

GeeWee commented Jan 10, 2018

I'd probably start by checking the babel step, which is in the postprocess file. Reproduce the bug in a repo, and perhaps try comparing it to output manually generated by tsc. To see whether we mess up before sending it to babel somehow or the babel step isn't configured properly.
It's not much of a lead to go on, but it's my best guess.

@andrioli
Copy link

andrioli commented Jan 17, 2018

Not sure if is related. But i'm getting wrong coverage mapping for my TS project only for files that are not executed during tests (option collectCoverageFrom include all my ts and tsx files).

Reproducer and Sample report using Node 9

You can found coverage report artifacts for other node versions in this AppVeyor build

@GeeWee
Copy link
Collaborator

GeeWee commented Feb 26, 2018

Can you confirm this is still an issue with the newest version of ts-jest?

@chasebrewsky
Copy link

I can confirm that it is still an issue. I just attempted yesterday to switch from a webpack/karma/mocha setup to jest and files that aren't imported during tests are given incorrect coverage. Everything that is imported into my spec files is correctly covered when mapCoverage: true is set, but everything specified in collectCoverageFrom that doesn't have a direct test file is incorrectly mapped. Here's my jest configuration:

"jest": {
  "transform": {
    "^.+\\.tsx?$": "ts-jest"
  },
  "testRegex": "\\.spec\\.tsx?$",
  "moduleFileExtensions": [
    "ts",
    "tsx",
    "js",
    "jsx",
    "json",
    "node"
  ],
  "moduleNameMapper": {
    "~/(.*)": "<rootDir>/src/app/$1",
    "\\.scss$": "<rootDir>/test/scssMock.js"
  },
  "mapCoverage": true,
  "collectCoverageFrom": [
    "src/**/*.{ts,tsx}"
  ]
},

I'll setup a test repository showing errors.

@chasebrewsky
Copy link

chasebrewsky commented Feb 27, 2018

I tried recreating it and couldn't with the latest dependencies. Jest deprecated mapCoverage in version 22.4 and now all transforms are automatically mapped to source maps, including those in collectCoverageFrom. I'm guessing source maps were supported for files that matched the test matchers but not the coverage matchers.

Upgrade jest past 22.4 and this issue should be resolved.

@andrioli
Copy link

for me the not executed files during tests are still showing weird coverage mappings.

Tried with jest 22.4.2 and ts-jest 22.4.0

@jscinoz
Copy link

jscinoz commented Jul 30, 2018

Unfortunately this is still an issue with ts-jest 23.0.1 + jest 23.4.1.

When babel is involved, the final coverage report is broken as in @momsse's "(Almost) Correct mapping + missing covered files" scenario, but setting skipBabel to true results in a correct coverage report.

@huafu
Copy link
Collaborator

huafu commented Jul 30, 2018

@jscinoz I cloned @momsse repo and updated node to v10 (else it bails if using the one of .npmrc) as well as upgraded all deps, removing babel-jest which is included anyway with jest. The result is, I've got the same coverage report with or without skipBabel: true. Is this the expected?

------------------------------|----------|----------|----------|----------|-------------------|
File                          |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
------------------------------|----------|----------|----------|----------|-------------------|
All files                     |    95.65 |      100 |    66.67 |    95.65 |                   |
 rnTsWithJest                 |      100 |      100 |      100 |      100 |                   |
  App.tsx                     |      100 |      100 |      100 |      100 |                   |
  Home.tsx                    |      100 |      100 |      100 |      100 |                   |
 rnTsWithJest/core/locales    |       80 |      100 |        0 |       80 |                   |
  index.ts                    |       80 |      100 |        0 |       80 |                 5 |
 rnTsWithJest/core/locales/en |      100 |      100 |      100 |      100 |                   |
  common.ts                   |      100 |      100 |      100 |      100 |                   |
  index.ts                    |      100 |      100 |      100 |      100 |                   |
 rnTsWithJest/core/locales/fr |      100 |      100 |      100 |      100 |                   |
  common.ts                   |      100 |      100 |      100 |      100 |                   |
  index.ts                    |      100 |      100 |      100 |      100 |                   |
------------------------------|----------|----------|----------|----------|-------------------|

FYI here are the new package.json's deps:

  "dependencies": {
    "babel-preset-react-native": "^4.0.0",
    "react": "^16.4.1",
    "react-native": "^0.56.0"
  },
  "devDependencies": {
    "jest": "^23.4.2",
    "react-test-renderer": "^16.4.1",
    "ts-jest": "^23.0.1",
    "typescript": "^2.9.2"
  },

and I've added esModuleInterop: true in ts-jest.config.json.

@huafu
Copy link
Collaborator

huafu commented Jul 30, 2018

@jscinoz I've also changed this in jest config (but that doesn't affect anything anyway):

    "transform": {
      "^.+\\.js$": "babel-jest",
      ".(ts|tsx)": "ts-jest"
    },

@jscinoz
Copy link

jscinoz commented Aug 2, 2018

@huafu In the (very new - only started in the last fortnight) codebase I'm currently adding test coverage to, which uses the following versions:

    "jest": "^23.4.1",                                                                                                                                                                        
    "react": "^16.4.1",                                                                                                                                                                                                                                                                                                                     
    "ts-jest": "^23.0.1",                                                                                                                                                                                                                                                                                                                                   
    "typescript": "^2.9.2"                                                                                                                                                                    

Results with skipBabel: true:

----------------------|----------|----------|----------|----------|-------------------|
File                  |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------------------|----------|----------|----------|----------|-------------------|                                                                                                       
All files             |    40.46 |    36.11 |    38.46 |    40.83 |                   |                                                                                                       
 context.ts           |        0 |      100 |      100 |        0 |             1,5,7 |                                                                                                       
 eccl-provider.tsx    |        0 |        0 |        0 |        0 |... 72,78,79,81,89 |                                                                                                       
 index.ts             |        0 |      100 |      100 |        0 |             1,2,3 |                                                                                                       
 insights-field.tsx   |        0 |        0 |        0 |        0 |... 59,161,164,175 |                                                                                                       
 insights-section.tsx |    93.44 |    78.57 |      100 |    93.44 |       55,57,58,61 |                                                                                                       
 prop-types.ts        |        0 |        0 |        0 |        0 |... 30,31,38,41,51 |                                                                                                       
 utils.ts             |    92.86 |      100 |    66.67 |    92.31 |                 3 |                                                                                                       
----------------------|----------|----------|----------|----------|-------------------|

Results with skipBabel: false:

--------------------|----------|----------|----------|----------|-------------------|
File                |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
--------------------|----------|----------|----------|----------|-------------------|                                                                                                         
All files           |        0 |        0 |        0 |        0 |                   |                                                                                                         
 context.ts         |        0 |      100 |      100 |        0 |             1,5,7 |                                                                                                         
 eccl-provider.tsx  |        0 |        0 |        0 |        0 |... 72,78,79,81,89 |                                                                                                         
 index.ts           |        0 |      100 |      100 |        0 |             1,2,3 |                                                                                                         
 insights-field.tsx |        0 |        0 |        0 |        0 |... 59,161,164,175 |                                                                                                         
 prop-types.ts      |        0 |        0 |        0 |        0 |... 30,31,38,41,51 |                                                                                                         
--------------------|----------|----------|----------|----------|-------------------|

@huafu
Copy link
Collaborator

huafu commented Aug 3, 2018

@jscinoz you should test with latest ts-jest 23.1.2.
If there is still a problem, please create a new issue with a minimal repo.

I am closing this right now (see #371 (comment))

@huafu huafu closed this as completed Aug 3, 2018
@jscinoz
Copy link

jscinoz commented Aug 8, 2018

I can confirm the coverage report is now correct with 23.1.2 and skipBabel: false. Thank you for the great work :)

@andrioli
Copy link

Looks that after I upgrade to Jest 23.4.2 and ts-jest 23.1.3 the coverage is mapped correctly. Thank you guys!

@kirillgroshkov
Copy link
Contributor

I have same issue (but it shows coverage as 0 always) with skipBabel: true. Is it supposed to be working? When I set to false it works.

@huafu
Copy link
Collaborator

huafu commented Aug 31, 2018

badly coverage needs babel, so you cannot have skipBabel set to false if you want coverage. The goal was to avoid 2 pass in babel, but jest checks first if the processor (ts-jest here) can do instrumentation without passing any config...
There is an open issue to remind us to address this in a future release ;-)

@kirillgroshkov
Copy link
Contributor

Ok, clear enough. Just wishing it's more clear in the documentation:)

@huafu
Copy link
Collaborator

huafu commented Sep 1, 2018

@kirillgroshkov you can try the @next dist-tag. It's a beta, so at your own risk :p but we need people testing it anyway!
See #697

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants