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

v8 coverage crashes with type-only files #10812

Closed
nstepien opened this issue Nov 12, 2020 · 14 comments · Fixed by #10819
Closed

v8 coverage crashes with type-only files #10812

nstepien opened this issue Nov 12, 2020 · 14 comments · Fixed by #10819
Labels

Comments

@nstepien
Copy link

nstepien commented Nov 12, 2020

🐛 Bug Report

With coverageProvider: 'v8' and coverage enabled, Jest crashes with imported but non-elided type-only modules.

To Reproduce

Steps to reproduce the behavior:

  • jest.config.mjs:
    export default {
      collectCoverage: true,
      coverageProvider: 'v8',
      rootDir: 'src'
    };
  • babel.config.json
    {
      "presets": [
        "@babel/typescript"
      ]
    }
  • src/types.ts:
    export interface obj {}
  • src/mytest.test.ts:
    import * as types from './types';
    
    test('1+1=2', () => {
      console.log(types);
      expect(1 + 1).toBe(2);
    });
  • Run npx jest
    PASS  src2/mytest.test.ts
      √ 1+1=2 (16 ms)
    
      console.log
        { default: {} }
    
          at Object.<anonymous> (mytest.test.ts:4:11)
    
    TypeError: Cannot read property 'replace' of undefined
        at V8ToIstanbul._resolveSource (D:\repo\node_modules\v8-to-istanbul\lib\v8-to-istanbul.js:86:29)
        at V8ToIstanbul.load (D:\repo\node_modules\v8-to-istanbul\lib\v8-to-istanbul.js:57:26)
        at D:\repo\node_modules\@jest\reporters\build\CoverageReporter.js:624:27
        at Array.map (<anonymous>)
        at CoverageReporter._getCoverageResult (D:\repo\node_modules\@jest\reporters\build\CoverageReporter.js:587:32)
        at CoverageReporter.onRunComplete (D:\repo\node_modules\@jest\reporters\build\CoverageReporter.js:231:45)
        at ReporterDispatcher.onRunComplete (D:\repo\node_modules\@jest\core\build\ReporterDispatcher.js:88:9)
        at TestScheduler.scheduleTests (D:\repo\node_modules\@jest\core\build\TestScheduler.js:350:5)
        at runJest (D:\repo\node_modules\@jest\core\build\runJest.js:376:19)
        at _run10000 (D:\repo\node_modules\@jest\core\build\cli\index.js:416:7)
    

Expected behavior

Jest doesn't crash, and coverage works.

Link to repl or repo (highly encouraged)

envinfo

  System:
    OS: Windows 10 10.0.19042
    CPU: (64) x64 AMD Ryzen Threadripper 3970X 32-Core Processor
  Binaries:
    Node: 15.2.0 - C:\Program Files\nodejs\node.EXE
    npm: 7.0.8 - C:\Program Files\nodejs\npm.CMD
  npmPackages:
    jest: ^26.6.3 => 26.6.3
@SimenB
Copy link
Member

SimenB commented Nov 12, 2020

Thanks for the report @nstepien!

@bcoe ideas? I haven't dug into it from Jest's side, might be us sending faulty data

@nstepien
Copy link
Author

From what I've seen debugging this a little bit, it seems like the type-only module is empty, so the sourcemap is empty/missing, and that empty/missing sourcemap is sent to v8-to-istanbul, which expects populated sourcemap metadata.

@SimenB
Copy link
Member

SimenB commented Nov 14, 2020

This is what we pass

{
  originalSource: 'export interface obj {}\n\n',
  source: '"use strict";\n' +
    '//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IiIsInNvdXJjZXNDb250ZW50IjpbXX0=',
  sourceMap: {
    sourcemap: {
      version: 3,
      sources: [],
      names: [],
      mappings: '',
      sourcesContent: []
    }
  }
}

Seems like a bug in v8-to-istanbul to me. We pass a valid sourcemap - it's just empty

@nstepien
Copy link
Author

@SimenB Thanks!

@SimenB
Copy link
Member

SimenB commented Nov 14, 2020

v8-to-istanbul should not rely on file being there - it's optional: https://github.com/mozilla/source-map/blob/0.5.7/README.md#new-sourcemapconsumerrawsourcemap

Not sure what ti should do though - probably fallback to scriptPath which is what my change does in practice

@viceice
Copy link

viceice commented Mar 2, 2021

I see this issue again with latest versions, see istanbuljs/v8-to-istanbul#134

@SimenB
Copy link
Member

SimenB commented Mar 2, 2021

Using jest@next?

@viceice
Copy link

viceice commented Mar 2, 2021

@SimenB
Copy link
Member

SimenB commented Mar 2, 2021

#10819 is only available in next, yes.

Good to fix in istanbul of course, then you'd be able to use 26 👍

@viceice
Copy link

viceice commented Mar 2, 2021

Yeah, using patch-package until istanbuljs/v8-to-istanbul#135 is merged 🎉

Any timeframe for next jest version?

@SimenB
Copy link
Member

SimenB commented Mar 2, 2021

Hoping this month, but I've not had much time for OSS these last few months

@bcoe
Copy link
Contributor

bcoe commented Mar 30, 2021

Just merged @viceice's patch 👌 sorry for the nuisance.

@SimenB
Copy link
Member

SimenB commented Mar 30, 2021

Thanks @bcoe!

ianwremmel added a commit to code-like-a-carpenter/components that referenced this issue Apr 11, 2021
@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 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants