3

我正在尝试为我的 puppeteer 项目设置测试。我正在遵循基本指南并且测试通过但终端中有 2 个控制台错误。

使用https://google.comhttps://youtube.com时不会出现该错误。所以看起来它可能与特定站点有关?

 console.error
    Unhandled error

      at process.uncaught (node_modules/jest-jasmine2/build/jasmine/Env.js:248:21)
      at handler (node_modules/jest-environment-puppeteer/lib/PuppeteerEnvironment.js:17:11)
      at map (node_modules/mitt/src/index.ts:74:75)
          at Array.map (<anonymous>)
      at Object.emit (node_modules/mitt/src/index.ts:74:56)
      at Page.emit (node_modules/puppeteer/lib/EventEmitter.js:72:22)

  console.error

      at process.uncaught (node_modules/jest-jasmine2/build/jasmine/Env.js:249:21)
      at handler (node_modules/jest-environment-puppeteer/lib/PuppeteerEnvironment.js:17:11)
      at map (node_modules/mitt/src/index.ts:74:75)
          at Array.map (<anonymous>)
      at Object.emit (node_modules/mitt/src/index.ts:74:56)
      at Page.emit (node_modules/puppeteer/lib/EventEmitter.js:72:22)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        5.613 s
Ran all test suites.

这是我的代码

describe('NCAA Home', () => {
    beforeAll(async () => {
      await page.goto('http://stats.ncaa.org/rankings/change_sport_year_div');
    });
  
    it('should be titled "NCAA Statistics"', async () => {
      await expect(page.title()).resolves.toMatch('NCAA Statistics');
    });
});

这是我的 jest.config.js

module.exports = {
    preset: "jest-puppeteer",
    testMatch: [
      "**/test/**/*.test.js"
    ],
    verbose: true
}

包.json

{
  "name": "stackoverflow",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "jest": {
    "preset": "jest-puppeteer"
  },
  "scripts": {
    "test": "jest"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "jest": "^26.1.0",
    "jest-puppeteer": "^4.4.0"
  },
  "dependencies": {
    "puppeteer": "^5.1.0"
  }
}

我遇到的所有事情都提到了 async/await 的问题,但是我尝试过的任何事情都会产生相同的错误,如果不是的话,还会产生更多错误。我用这些文件做了一个新项目,我得到了同样的错误

4

2 回答 2

0

错误来自网站本身。检查网站的控制台。因此,对于像 google.com 或 youtube.com 这样的网站,它可以正常运行而不会出现任何错误。

在此处输入图像描述

于 2020-07-14T09:05:23.313 回答
0

我创建了重现问题的干净回购。 https://github.com/sergtimosh/jest-puppeteer-issue-reproduction.git

  • 克隆存储库
  • npm 我
  • npm 测试 test.spec.js

或者

  • HEADLESS=false npm test test.spec.js

一种解决方法是在jest-environment.js. 只需取消注释此文件中的两行,测试就会顺利通过。但是如果您需要在测试套件(文件)之间共享浏览器上下文,问题仍然存在。

const PuppeteerEnvironment = require('jest-environment-puppeteer');


class JestEnvironment extends PuppeteerEnvironment {

  async setup() {
    await super.setup()
    //to fix issue uncomment next two lines
    // const incognitoContext = await this.global.browser.createIncognitoBrowserContext()
    // this.global.page = await incognitoContext.newPage()

  }

  async teardown() {
    await super.teardown()
  }

}

module.exports = JestEnvironment;

于 2020-09-17T09:18:44.740 回答