将 Jest 与 Puppeteer 一起使用:评估失败:ReferenceError: cov_4kq3tptqc is not defined

我正在尝试使用 Puppeteer 获取页面的描述,我有一个高阶函数,为该函数提供页面对象:

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;
};

该功能按预期工作.然后,我正在使用 Jest 运行测试.

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");

我有以下错误:

  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 --

如果我只是将函数粘贴到 jest 文件中,那么它会按预期工作

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

推荐答案

如果需要收集覆盖率,可以通过在浏览器上下文函数前添加 /* istanbul ignore next */ 来修复(带有 .eval) 的行以防止伊斯坦布尔覆盖注入.

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.

相关文章