jest-puppeteer icon indicating copy to clipboard operation
jest-puppeteer copied to clipboard

Server not stopped when jest --watch exits

Open Volcomix opened this issue 5 years ago • 4 comments

🐛 Bug Report

When exiting jest --watch (by pressing q), the server doesn't seem to stop.

To Reproduce

// jest-puppeteer.config.js
module.exports = {
  server: {
    command: 'python -m http.server 8081 --directory src',
    port: 8081,
  },
}
  1. jest --watch (or yarn test --watch in my case)
  2. run some tests
  3. press q to quit
  4. jest --watch again
Determining test suites to run...
✔ Another process is listening on 8081. Should I kill it for you? On linux, this may require you to enter your password. … yes

  ● Test suite failed to run

    TypeError: Cannot read property 'name' of undefined

      at logProcDetection (node_modules/jest-dev-server/lib/global.js:70:69)
      at ask (node_modules/jest-dev-server/lib/global.js:153:9)
      at async setupJestServer (node_modules/jest-dev-server/lib/global.js:174:7)
          at async Promise.all (index 0)
      at async setup (node_modules/jest-dev-server/lib/global.js:123:3)
      at async setup (node_modules/jest-environment-puppeteer/lib/global.js:38:7)

Expected behavior

In step 4. I would expect to have the tests running again without any confirmation to kill a running process.

Link to repl or repo

https://github.com/Volcomix/waane/tree/e4c3631a7424c8fd1cd67f858990de95eb369556

Environment

## System:
 - OS: Linux 5.4 Arch Linux
 - CPU: (8) x64 Intel(R) Core(TM) i7-3770K CPU @ 3.50GHz
 - Memory: 9.14 GB / 15.59 GB
 - Container: Yes
 - Shell: 5.0.11 - /bin/bash
## Binaries:
 - Node: 13.5.0 - /usr/bin/node
 - Yarn: 1.21.1 - /usr/bin/yarn
 - npm: 6.13.4 - /usr/bin/npm
 - Watchman: 4.9.0 - /usr/bin/watchman
## npmPackages:
 - jest-puppeteer: ^4.4.0 => 4.4.0

Volcomix avatar Dec 29 '19 11:12 Volcomix

The error which appears when asking to kill the existing process seems to be related to find-process module because:

$ npx find-process 32431
Found 1 process

[python]
pid: 32431
cmd: python -m http.server 8081 --directory src

but

$ npx find-process -p 8081
No process found

Issue opened: yibn2008/find-process#31

Volcomix avatar Dec 29 '19 11:12 Volcomix

I think I might be having the same issue. When my test fails the node server isn't shut down and I have to manually kill it before running the tests again.

rjdestigter avatar Mar 09 '20 14:03 rjdestigter

Workaround using https://www.npmjs.com/package/start-server-and-test:

In your config file something like:

if (!process.env.NO_SERVER) {
	config.server = {
		command: 'node ./server/start.js',
		port: process.env.PORT || 3456,
	},
};

then run a task like:

start-server-and-test "NODE_ENV=test make run" http://localhost:3456 "NO_SERVER=true jest test --watch"

wheresrhys avatar Jun 09 '20 16:06 wheresrhys

The teardown method linked to below is called after each run of the full test suite, it specifically guards against tearing down the servers when in watch mode:

https://github.com/smooth-code/jest-puppeteer/blob/56715a8938e859d994c9ea1520cf44a3c796c98e/packages/jest-environment-puppeteer/src/global.js#L69-L71

Unfortunately if Jest is shut down whilst the test suites are being run or sat idle between runs then this teardown method does not get called... and I can't yet see any way to hook into Jest to do so.

I had thought that adding a handler for SIGINT might work but there is currently an issue with this: https://github.com/facebook/jest/issues/10052

cc https://github.com/facebook/jest/issues/6029

i-like-robots avatar Jun 10 '20 11:06 i-like-robots

We can't really stop server when jest is closing. To mitigate this, jest-dev-server detects the server running on the port.

gregberge avatar Feb 04 '23 15:02 gregberge