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.