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

Using Jest with Puppeteer : Evaluation failed: ReferenceError: cov_4kq3tptqc is not defined(将 Jest 与 Puppeteer 一起使用:评估失败:ReferenceError: cov_4kq3tptqc is not defined)
本文介绍了将 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.

这篇关于将 Jest 与 Puppeteer 一起使用:评估失败:ReferenceError: cov_4kq3tptqc is not defined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)