Search code examples
node.jsintegration-testingpuppeteer

Integration test failure using Puppeteer


I am new to Node.JS and very curious to learn more about it, therefore I decided to do some exercises from a book.

The point which I am struggling is with the integration test.

I would like to have a crawler checking my application to see if the links are working fine. For that I am using the following code:

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"
  }
}

integration-tests/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
}

Error

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

ReferenceError: beforeEach is not defined

What am I missing/ doing wrong here?


Solution

  • You need to run your test through jest and not plain node otherwise all the globals defined by jest won't exist.

    Example if you're using yarn:

    • yarn jest to run all tests it can find based on jest default settings (see documentation to customize)
    • yarn jest meadowlark/integration-tests/basic-navigation.test.js to only run your file