Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Jest with Puppeteer : Evaluation failed: ReferenceError: cov_4kq3tptqc is not defined

I'm trying to get description of a page with Puppeteer, I have a high order function that provides the page object to this function :

export const checkDescription = async page => {
  const metaDescription = await page.$eval(
    'meta[name="description"]',
    description => description.getAttribute("content")
  );
  return metaDescription;
};

the function works as expected. Then, I'm using Jest to run a test.

const testDescription = await withPage(checkDescription)(URL);
expect(typeof testDescription).toBe("string");

I have the following err:

  Error: Evaluation failed: ReferenceError: cov_4kq3tptqc is not defined
      at __puppeteer_evaluation_script__:2:41
      at ExecutionContext.evaluateHandle 
      at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)
    -- ASYNC --
      at ExecutionContext.<anonymous> 
      at ExecutionContext.evaluate
      at ExecutionContext.<anonymous> 
      at ElementHandle.$eval
      at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)
    -- ASYNC --

If I just paste the function in the jest file, then it works as expected

like image 260
Elena Avatar asked Mar 21 '19 01:03

Elena


2 Answers

If you need to collect the coverage, it can be fixed by adding /* istanbul ignore next */ before browser contexted functions (lines with .eval) to prevent istanbul coverage injects.

like image 67
oneralon Avatar answered Nov 04 '22 00:11

oneralon


In puppeteer, while running tests, istanbul was inserting the following :

 /* istanbul ignore next */cov_4kq3tptqc.f[7]++;
                    cov_4kq3tptqc.s[19]++;

Was fixed by adding config.collectCoverage = false; to the jest.config

like image 6
Elena Avatar answered Nov 03 '22 23:11

Elena