运行笑话操纵者测试时出现未处理的错误

时间:2020-07-13 16:48:33

标签: jestjs puppeteer jest-puppeteer

我正在尝试为我的操纵up项目设置测试。我遵循了基本指南,测试通过了,但是终端中有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
}

package.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问题,但是我尝试过的任何事情都会产生相同的错误,如果没有,则会产生更多的错误。我已经用这些文件创建了一个新项目,但出现了同样的错误

2 个答案:

答案 0 :(得分:0)

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

enter image description here

答案 1 :(得分:0)

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

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

  • HEADLESS = false npm测试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;