【问题标题】:Automated browser testing with puppeter and jest使用 puppeteer 和 jest 进行自动浏览器测试
【发布时间】:2021-03-11 03:40:50
【问题描述】:

我遇到了一个将 puppeteer 与 jest 结合起来的大问题。每当我点击“npm run test”时,此测试失败显示:“超时 - 在 jest.setTimeout 指定的 5000 毫秒超时内未调用异步回调。”。即使我将 timeout 作为第三个参数传递给测试函数或从 beforeEach 方法回调内部调用 jest.setTimeout(timeout) 也会出现此警告。有什么问题,各位大神能帮帮我。附言我分别使用 jest 和 puppeteer 包

const pup = require('puppeteer')

let browser, page

beforeEach(async _ => {
  browser = await pup.launch({ 
    headless: false  
  })
  page = await browser.newPage()
  await page.goto('localhost:3000')
})

afterEach(async _ => await browser.close())

test('Login function', async _ => {
  await page.click('link')

  const url = await page.url()

  expect(url).toMatch(/accounts\.google\.com/)
})

【问题讨论】:

标签: jestjs automated-tests puppeteer jest-puppeteer


【解决方案1】:

在 Mocha 和 Jest 中,它看起来大多相同。当您运行的异步脚本执行时间超过默认超时时,您必须手动覆盖默认超时。

test('Login function', async _ => {
 //..
}, 60000);

替代你 override the global timeout 通过 CLI 使用以下方式进行测试:

--testTimeout=<number>

【讨论】:

    猜你喜欢
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    • 2021-10-15
    相关资源
    最近更新 更多