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

Coverage report fails with puppeteer evaluate code #7962

Closed
sab24 opened this issue Feb 22, 2019 · 11 comments
Closed

Coverage report fails with puppeteer evaluate code #7962

sab24 opened this issue Feb 22, 2019 · 11 comments

Comments

@sab24
Copy link

sab24 commented Feb 22, 2019

🐛 Bug Report

Code coverage fails to evaluate code that is evaluated in another environment.

To Reproduce

Steps to reproduce the behaviour:

  1. Create a Nodejs program that include page.evaluate from Puppeteer. This code is evaluated in a separate environment and causes test cases to fail.

Expected behaviour

Proper coverage report with the puppeteer evaluate code excluded. Otherwise some kind of configuration option to exclude remotely executed code.

jest
 PASS  ./test.js
  ✓ testing puppeteer page scrape (1144ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        3.653s
Ran all test suites.

Current behaviour

jest --coverage
 FAIL  ./test.js
  ✕ testing puppeteer page scrape (737ms)

  ● testing puppeteer page scrape

    Evaluation failed: ReferenceError: cov_gpckfapr6 is not defined

      at __puppeteer_evaluation_script__:3:7
      at ExecutionContext.evaluateHandle (node_modules/puppeteer/lib/ExecutionContext.js:121:13)
        -- ASYNC --
      at ExecutionContext.<anonymous> (node_modules/puppeteer/lib/helper.js:108:27)
      at ExecutionContext.evaluate (node_modules/puppeteer/lib/ExecutionContext.js:48:31)
      at ExecutionContext.<anonymous> (node_modules/puppeteer/lib/helper.js:109:23)
      at DOMWorld.evaluate (node_modules/puppeteer/lib/DOMWorld.js:105:20)
        -- ASYNC --
      at Frame.<anonymous> (node_modules/puppeteer/lib/helper.js:108:27)
      at Page.evaluate (node_modules/puppeteer/lib/Page.js:815:43)
      at Page.<anonymous> (node_modules/puppeteer/lib/helper.js:109:23)
      at Object.evaluate [as evaluateContent] (scrape.js:4:24)

-----------|----------|----------|----------|----------|-------------------|
File       |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
-----------|----------|----------|----------|----------|-------------------|
All files  |       50 |      100 |       50 |       50 |                   |
 scrape.js |       50 |      100 |       50 |       50 |             5,7,8 |
-----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        2.781s, estimated 4s
Ran all test suites.
Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

Link to repl or repo (highly encouraged)

https://github.com/sab24/jestexample

Run by typing
npm i
jest
jest --coverage

Run npx envinfo --preset jest

Paste the results here:

  System:
    OS: macOS 10.14.2
    CPU: (8) x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
  Binaries:
    Node: 11.9.0 - /usr/local/bin/node
    npm: 6.8.0 - /usr/local/bin/npm
  npmPackages:
    jest: ^24.1.0 => 24.1.0 
@sab24 sab24 added the 🐛 Bug label Feb 22, 2019
@jeysal
Copy link
Contributor

jeysal commented Feb 23, 2019

If I understand correctly, you want return(document.querySelector('#hplogo').alt); to show up as uncovered, but it shows up as covered. Can you explain why you would want that to happen? It seems like very unintuitive behavior to me.
You could extract the pieces of code that you want to exclude from coverage into their own files that are matched by coveragePathIgnorePatterns if you really need to ignore those lines

@sab24
Copy link
Author

sab24 commented Feb 23, 2019

Hi @jeysal,

The test cases fail because the variable tracking in the code coverage software go out of scope I think. Therefore my test cases fail while they do succeed without the code coverage flag.

Is it even possible to save the inner part of page.evaluate in another file?

@jeysal
Copy link
Contributor

jeysal commented Feb 23, 2019

Oh, now I see what you mean. I didn't understand that the actual test result differed depending on whether coverage is enabled (and I couldn't reproduce it on my Linux machine because I got a different error - but on my Mac I can see the behavior you're describing)

@jeysal
Copy link
Contributor

jeysal commented Feb 23, 2019

Could something like https://github.com/istanbuljs/puppeteer-to-istanbul perhaps solve your issue?

@carmanchris31
Copy link

carmanchris31 commented May 23, 2019

I'm having the same issue and unfortunately puppeteer-to-istanbul had no effect. Best option for now might to be to isolate the page.evaluate call into a separate file and selectively disable coverage for that file by using coveragePathIgnorePatterns in your jest config :-/

@StephanBijzitter
Copy link
Contributor

Best option for now might to be to isolate the page.evaluate call into a separate file and selectively disable coverage for that file by using coveragePathIgnorePatterns in your jest config :-/

This worked for me, thanks!
It's not quite perfect... but it'll do.

Took me way too long to figure out the problem was with Jest, rather than Puppeteer or being related to my test being in TypeScript.

Will see if I can edit https://jestjs.io/docs/en/puppeteer to add a disclaimer.

@tom-sherman
Copy link

I have also encountered this same issue when working with PouchDB where indexes are written in JS and stringified into the database. The views are evaluated in a sandbox where the coverage collectors aren't defined. See pouchdb/pouchdb#8129

We've resorted to ignoring the file from coverage which works, but isn't ideal.

@Jimit09
Copy link

Jimit09 commented Dec 1, 2020

Is there any resolution to this issue sooner?

@github-actions
Copy link

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 14 days.

@github-actions github-actions bot added the Stale label Feb 25, 2022
@github-actions
Copy link

This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions
Copy link

github-actions bot commented May 2, 2022

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 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants