79418530

Date: 2025-02-06 15:52:04
Score: 2
Natty:
Report link

Turns out that there were a few possible solutions to my problem. The easiest was to let puppeteer start the server by adding the server command to the jest-puppeteer.config.js file. I also added a command flag (--puppeteer) as described in the stackoverflow question:

server: {
  command: "node server.js --puppeteer",
  port: 5000,
  launchTimeout: 10000,
  debug: true,
},

I then added a check in the server.js file for the flag so that the server is not started for any unit tests other than those using puppeteer:

if (process.env['NODE_ENV'] === 'test'
    && !process.argv.slice(2).includes('--puppeteer')) {
  module.exports = {
    'app': app,
  };
} else {
  http.listen(port);
}

I also put my react (puppeteer) tests in a separate file just for good measure.

jest.config.js:

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

Feel free to comment if you have a better solution.

Reasons:
  • Blacklisted phrase (1): to comment
  • Blacklisted phrase (1): stackoverflow
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Rob Gravelle