【问题标题】:Integration test failure using Puppeteer使用 Puppeteer 的集成测试失败
【发布时间】:2021-10-25 09:10:15
【问题描述】:

我是 Node.JS 的新手,非常想了解更多关于它的知识,因此我决定从书中做一些练习。

我正在努力的一点是集成测试。

我想让爬虫检查我的应用程序以查看链接是否正常工作。为此,我使用以下代码:

package.json

{
  "main": "meadowlark.js",
  "scripts": {
    "test": "jest",
    "lint": "eslint meadowlark.js lib"
  },
  "dependencies": {
    "express": "^4.17.1",
    "express3-handlebars": "^0.5.2"
  },
  "devDependencies": {
    "eslint": "^5.15.3",
    "jest": "^24.9.0",
    "portfinder": "^1.0.20",
    "puppeteer": "^1.13.0"
  }
}

集成测试/basic-navigation.test.js

const portfinder = require('portfinder')
const puppeteer = require('puppeteer')

const app = require('../meadowlark.js')

let server = null
let port = null

beforeEach(async () => {
  port = await portfinder.getPortPromise()
  server = app.listen(port)
})

afterEach(() => {
  server.close()
})

test('home page links to about page', async () => {
  const browser = await puppeteer.launch()
  const page = await browser.newPage()
  await page.goto(`http://localhost:${port}`)
  await Promise.all([
    page.waitForNavigation(),
    page.click('[data-test-id="about"]'),
  ])
  expect(page.url()).toBe(`http://localhost:${port}/about`)
  await browser.close()
})

meadowlark.js

// Starting an express application
var express = require('express');
var app = express();
/* eslint-disable no-undef */
const port = process.env.PORT || 3000
/* eslint-enable no-undef */

// Set up handlebars view engine (Templating)
// Check the views folder for html skeleton and the respective
// handlebars
var handlebars = require('express3-handlebars')
    .create({ defaultLayout:'main' });
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');
/* eslint-disable no-undef */
app.set('port', process.env.PORT || 3000);
/* eslint-enable no-undef */
const handlers = require('./lib/handlers')

// Function to generate the quote of the day
//const fortune = require('./lib/fortune')

// Homepage
app.get('/', handlers.home)
// About
app.get('/about', handlers.about);
// 404
app.use(handlers.notFound);
// 500
app.use(handlers.serverError)

// Binding to the port
if(require.main === module) {
    app.listen(port, () => {
        console.log( `Express started on http://localhost:${port}` +
        '; press Ctrl-C to terminate.' )
    })
} else {
    module.exports = app
}

错误

meadowlark/integration-tests/basic-navigation.test.js:9
beforeEach(async () => {
^

ReferenceError: beforeEach is not defined

我在这里遗漏了什么/做错了什么?

【问题讨论】:

  • 你能提供你用来运行测试的命令吗?它需要通过jest 而不是plainnode 否则 jest 定义的所有全局变量都将不存在。
  • 感谢@SebD。如果您想发布一个,我很高兴接受您的回答

标签: node.js integration-testing puppeteer


【解决方案1】:

您需要通过jest 而不是普通的node 运行测试,否则jest 定义的所有全局变量将不存在。

例如,如果您使用的是yarn

  • yarn jest 根据开玩笑的默认设置运行它可以找到的所有测试(请参阅 documentation 进行自定义)
  • yarn jest meadowlark/integration-tests/basic-navigation.test.js 只运行你的文件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-29
    • 1970-01-01
    • 2019-12-26
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    • 1970-01-01
    相关资源
    最近更新 更多