Skip to content

Jest freeze on Error when autoMock and collectCoverage are both true #402

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

Closed
typehorror opened this issue Dec 28, 2017 · 7 comments
Closed
Labels

Comments

@typehorror
Copy link

typehorror commented Dec 28, 2017

  • Issue

Test freeze on error when jest autoMock and collectCoverage are set to true.

  • Expected behavior

Test should display the error and move on to the next test. The same jest settings work on my babel projects.

@typehorror typehorror changed the title Jest freeze Jest freeze on Error when autoMock and collectCoverage are both true Dec 28, 2017
@typehorror
Copy link
Author

A little more info as I'm investigating the bug here. The issue seems to be coming from the jest unmock() feature introduced in jest-cli 0.9.0 which leads me to think it's a hoisting issue (related to the mock() reported in the readme).

@kulshekhar
Copy link
Owner

@Debrice this does seem like a hoisting issue.

A quick workaround is to configure ts-jest to skip babel

/cc @GeeWee

@typehorror
Copy link
Author

You're right @kulshekhar , skiping babel prevents the test from hanging but creates a new error. If I update the test to read (so it breaks on purpose):

jest.unmock("../index");

import foo from "../index";

it("will hang", () => {
  expect(()=>foo("test")).not.toThrow();
})

Running the test with skipBabel set to true gives me that sourceMap.map.originalPositionFor error in its output

$ yarn test
yarn run v1.3.2
$ jest
 FAIL  __tests__/index.ts
  ✕ will hang (24ms)

  ● will hang

    TypeError: sourceMap.map.originalPositionFor is not a function

      4 | 
      5 | it("will hang", () => {
    > 6 |   expect(()=>foo("test")).not.toThrow();
      7 | })
      8 | 
      
      at mapSourcePosition (node_modules/source-map-support/source-map-support.js:199:42)
          at Array.map (<anonymous>)
      at Object.<anonymous>.it (__tests__/index.ts:6:47)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        2.294s
Ran all test suites.
----------|----------|----------|----------|----------|----------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
----------|----------|----------|----------|----------|----------------|
All files |      100 |      100 |      100 |      100 |                |
 index.ts |      100 |      100 |      100 |      100 |                |
----------|----------|----------|----------|----------|----------------|
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Note: I had to move the jest.unmock() before the import. When the unmocking operation happens after the import, it's silently ignored and the imported function stays a spy (because automock is true).

@kulshekhar
Copy link
Owner

@Debrice that error goes away if mapCoverage is removed from jest config. I'm not yet sure what needs to be done to fix this

I'll need to take a closer look at this. This will probably address #340 as well

@typehorror
Copy link
Author

typehorror commented Jan 2, 2018

@kulshekhar, you are right. I decided not to mention the removal of mapCoverage since it would render the use of collectCoverage option moot. For now I just don't collect coverage during the coding process.

Once all my tests pass, I turn on collectCoverage and run another pass... it has become more or less a "first world problem" at this point.

@GeeWee
Copy link
Collaborator

GeeWee commented Jan 2, 2018

Seems strange with the hoisting issue. We're running things through the same transform jest is, so the issue should manifest in plain jest as well if it's related to that.

@huafu
Copy link
Collaborator

huafu commented Sep 2, 2018

@Debrice if this has yet not been fixed in latest version, you can test beta (see #697). Anyway, I am closing this since it's quite old, but feel free to re-open if I shouldn't have closed it of course.

@huafu huafu closed this as completed Sep 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants