This is a port of mafintosh' why-is-node-running module to TypeScript, with modernized syntax and no dependencies.
const { whyIsNodeStillRunning } = require('why-is-node-still-running');
const { createServer } = require('net');
const server = createServer();
server.listen(0);
whyIsNodeStillRunning();
// There are 2 handle(s) keeping the process running
npm i -D why-is-node-still-running
Always import this module first so that the asynchronous hook can be setup.
The hook will log to the console by default, but you can provide it with a
custom logger that implements error()
.
Sometimes Jest complains that there are asynchronous operations still hanging
after the tests have been completed. When the --detectOpenHandles
flag gives
no output, you can try using this module to help pinpoint the cause:
import { whyIsNodeStillRunning } from 'why-is-node-still-running';
afterAll(async () => {
// Do your actual cleanup here
// [...]
// Print the handles still opened
await new Promise(resolve => setTimeout(() => {
whyIsNodeStillRunning();
resolve();
}, 4000));
});
Don't forget to run Jest with --useStderr
to show console output.
Alternatively, you can use this module to print some information about the stack regularly while the tests are running (e.g. see this comment).
Copyright (c) 2020-present, cheap glitch
Permission to use, copy, modify, and/or distribute this software for any purpose
with or without fee is hereby granted, provided that the above copyright notice
and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.