Node.js: Jest + redis.quit() but open handle warning persists
Asked Answered
G

4

15

I'm trying to run an integration test with redis and jest.

It always throws the "Jest has detected the following 1 open handle potentially keeping Jest from exiting:" error when running with --detectOpenHandles.

It doesn't hang so the socket is closing, but how do I write it so it doesn't throw that warning?

My code

import redis from 'redis';
let red: redis.RedisClient;

beforeAll(() => {
    red = redis.createClient();
});

afterAll((done) => {
    red.quit(() => {
        done();
    });
});

test('redis', (done) => {
    red.set('a', 'b', (err, data) => {
        console.log(err);
        red.get('a', (err, data) => {
            console.log(data);
            done();
        });
    });

});

Warning

Jest has detected the following 1 open handle potentially keeping Jest from exiting:

●  TCPWRAP

    3 |
    4 | beforeAll(() => {
    > 5 |     red = redis.createClient();
        |                 ^
    6 | });
    7 |
    8 | afterAll((done) => {

    at RedisClient.Object.<anonymous>.RedisClient.create_stream (node_modules/redis/index.js:251:31)
    at new RedisClient (node_modules/redis/index.js:159:10)
    at Object.<anonymous>.exports.createClient (node_modules/redis/index.js:1089:12)
    at Object.<anonymous>.beforeAll (src/sockets/redis.integration.test.ts:5:17)

Running the compiled code with regular jest throws the same warning. The compiled code looks nearly identical.

Gokey answered 23/10, 2018 at 0:46 Comment(0)
G
22

Figured it out after posting. I forgot to post the answer here.

It has to do with how redis.quit() handles its callback.

It creates a new thread so we need to wait for the entire eventloop stack to cycle again. See below for the workaround.

async function shutdown() {
    await new Promise((resolve) => {
        redis.quit(() => {
            resolve();
        });
    });
    // redis.quit() creates a thread to close the connection.
    // We wait until all threads have been run once to ensure the connection closes.
    await new Promise(resolve => setImmediate(resolve));
}
Gokey answered 6/2, 2019 at 18:50 Comment(0)
D
0

I have the same issue. I haven't figured it out to fix this but I have a workaround. You can add jest --forceExit. For info about forceExit: https://jestjs.io/docs/en/cli.html#forceexit

Dragging answered 5/2, 2019 at 10:24 Comment(0)
R
0

Fixed for me doing

const redis = require("redis");
const { promisify } = require("util");

const createRedisHelper = () => {
  const client = redis.createClient(`redis://${process.env.REDIS_URI}`);

  client.on("error", function (error) {
    console.error(error);
  });

  const setAsync = promisify(client.set).bind(client);
  const setCallValidateData = async (key, data) => {
    return setAsync(key, JSON.stringify(data));
  };

  return {
    setCallValidateData,
    cleanCallValidateData: promisify(client.flushdb).bind(client),
    end: promisify(client.end).bind(client),
  };
};

module.exports = createRedisHelper;

Versions: redis 3.0.2 node 12.6.1

Ropy answered 14/9, 2020 at 15:27 Comment(0)
I
0

The problem is that you are creating the redis client but not connecting, so calling later redis.quit will not work, since you have no connection.

try:

beforeAll(async () => {
    red = redis.createClient();
    await red.connect();
});
Insurer answered 10/9, 2022 at 14:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.