最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using Jest with Puppeteer : Evaluation failed: ReferenceError: cov_4kq3tptqc is not defined - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question asked Mar 21, 2019 at 1:00 ElenaElena 6493 gold badges9 silver badges19 bronze badges 3
  • What is withPage ? – Julien TASSIN Commented Mar 22, 2019 at 20:36
  • this is just a high order function, it works as expected – Elena Commented Mar 25, 2019 at 14:53
  • I had the same issue using Playwright with Jest. @oneralon's answer solved it for me. – Peter Bagnall Commented Jun 8, 2021 at 16:53
Add a comment  | 

3 Answers 3

Reset to default 10

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.

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

Placing /* istanbul ignore next */ within the evaluate function resolved the problem effectively.

Didn't work:

/* istanbul ignore next */
const result = await page.evaluate((el, attr) => el.getAttribute(attr), el, attr)

Worked:

const result = await page.evaluate(/* istanbul ignore next */(el, attr) => el.getAttribute(attr), el, attr)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论