'Failing Jest unit test - open handles

I have an apiSocket class that implements socket.io-client socket:

class ApiSocket {
  private static readonly instance: ApiSocket;
  private readonly socket: Socket;

  private constructor() {
    this.socket = io(envConfig.apiHost, {
      transports: ['websocket']
    });
    ...
  }

  ...

  static getInstance(): ApiSocket {
    return this.instance ?? new ApiSocket();
  }
}

const apiSocket = ApiSocket.getInstance();
export default apiSocket;

Then I use this as an import in test, e.g. store.test.ts, like this:

import apiSocket from 'services/apiSocket';

jest.mock('services/apiSocket');

describe('store', () => {
    describe('getSomething', () => {
        test('should do something', () => {
          ...
          expect(apiSocket.getSomething as jest.Mock).toHaveBeenCalledTimes(1);
        });
    });
});

Whenever I was running this test, it passed but I was getting this errors in console:

PASS  src/stores/store/__tests__/store.test.ts (7.335 s)
node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
          ^

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "ReferenceError: document is not defined".] {
  code: 'ERR_UNHANDLED_REJECTION'
} 

So I added --detectOpenHandles flag to jest command and this is what I got:

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

  ●  Timeout

      20 |
      21 |   private constructor() {
    > 22 |     this.socket = io(envConfig.apiHost, {
         |                   ^
      23 |       transports: ['websocket']
      24 |     });
      25 |     this.socket.on(InboundEventName.Exception, (error) => {

      at initAsClient (node_modules/ws/lib/websocket.js:715:31)
      at new WebSocket (node_modules/ws/lib/websocket.js:83:7)
      at WS.doOpen (node_modules/engine.io-client/build/cjs/transports/websocket.js:63:23)
      at WS.open (node_modules/engine.io-client/build/cjs/transport.js:52:18)
      at Socket.open (node_modules/engine.io-client/build/cjs/socket.js:170:19)
      at new Socket (node_modules/engine.io-client/build/cjs/socket.js:108:14)
      at Manager.open (node_modules/socket.io-client/build/cjs/manager.js:137:23)
      at new Manager (node_modules/socket.io-client/build/cjs/manager.js:66:18)
      at Function.socket_io_client_1 (node_modules/socket.io-client/build/cjs/index.js:41:25)
      at new ApiSocket (src/services/apiSocket.ts:22:19)
      at Function.getInstance (src/services/apiSocket.ts:40:29)
      at Object.<anonymous> (src/services/apiSocket.ts:124:29)
      at Object.<anonymous> (src/stores/matches/matches.test.ts:2:1)

Is there any possible solution that can help me to resolve this issue? I tried --forceExit flag but these error keep on showing up in the console.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source