【问题标题】:Jest puppeteer typescript reference error Page not foundJest puppeteer typescript reference error 找不到页面
【发布时间】:2021-01-16 05:53:44
【问题描述】:

试图用 puppeteer 设置 typescript jest

我按照下面提到的分步说明进行操作

Jest-puppeteer with typescript configuration

有一个简单的测试

describe('Google', () => {
 
    beforeAll(async () => {
        await page.goto('https://google.com')
    })
 
    it('should display "google" text on page', async () => {
        await expect(page).toMatch('google')
    })
 
})

当我运行我的测试时,我得到了奇怪的错误

ReferenceError: page is not defined

它指向 beforeAll 中的“等待页面”对象

我还注意到 chrome 尝试启动但未启动可能是此错误是 b/c chrome 无法启动。

jest-puppeteer 库负责启动浏览器并提供浏览器和页面对象

这是从上面链接中提到的页面获取的代码

//jest-puppeteer.config.js

let jest_puppeteer_conf = {
    launch: {
        timeout: 30000,
        dumpio: true // Whether to pipe the browser process stdout and stderr 
    }
}
 
const isDebugMode = typeof v8debug === 'object' || /--debug|--inspect/.test(process.execArgv.join(' '));
 
if (isDebugMode) {
    jest_puppeteer_conf.launch.headless = false; // for debug:  to see what the browser is displaying
    jest_puppeteer_conf.launch.slowMo = 250;  // slow down by 250ms for each step
    jest_puppeteer_conf.launch.devtools = true; // This lets you debug code in the application code browser
    jest_puppeteer_conf.launch.args = [ '--start-maximized' ]; // maximise the screen
}
 
module.exports = jest_puppeteer_conf;

底部有一个小的调试部分,提醒添加以下类型,我已经拥有它们但仍然没有运气,感谢任何帮助。

"compilerOptions": {
       .....
        "types": [
         .......
          "puppeteer",
          "jest-environment-puppeteer",
          "expect-puppeteer"
        ]
      }

【问题讨论】:

    标签: jestjs puppeteer ts-jest jest-puppeteer


    【解决方案1】:

    注释掉以下行可以解决问题。

    // testEnvironment: "node"
    

    【讨论】:

      猜你喜欢
      • 2020-01-09
      • 2019-10-28
      • 1970-01-01
      • 2020-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-11
      相关资源
      最近更新 更多