vendredi 27 novembre 2020

Jest with node.JS Express causing open handle error

I have a basic node.JS Express app server, and when I try running Jest jest --detectOpenHandles, I get the following error. I'm not sure what could be causing it though, since as far as I'm aware, I make sure to close the server once all the tests complete.

Error message

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

  ●  TCPSERVERWRAP

      18 |  * Listen on provided port, on all network interfaces.
      19 |  */
    > 20 | server.listen(process.env.PORT);
         |        ^
      21 | server.on('listening', onListening);
      22 | 
      23 | /**

      at Object.<anonymous> (index.ts:20:8)

index.ts

#!/usr/bin/env node

import { app } from './app'
import http from 'http'
import {AddressInfo} from "net";

export const server = http.createServer(app);

server.listen(process.env.PORT);
server.on('listening', onListening);

function onListening() {
    const addr = server.address() as AddressInfo;
    const bind = 'port ' + addr.port;

    console.log(`Listening on ${bind}`)
}

jest.config.ts

export default {
  bail: true,
  clearMocks: true,
  globalTeardown: "./tests/helpers/server_close.ts",
  roots: [
    "./tests"
  ],
  testEnvironment: "node",
  watchman: true,
};

tests/helpers/server_close.ts

import {server} from "../../index";

export default async () => {
    await server.close();
}

tests/webhook.test.ts

// imports

const request = supertest(app);

beforeAll(async (done) => {
    const url = `mongodb://127.0.0.1/test`
    await mongoose.connect(url, {
        useNewUrlParser: true,
        useUnifiedTopology: true
    })
    done();
});

afterAll(async (done) => {
    await mongoose.disconnect();
    done();
})

afterEach(async (done) => {
    await User.deleteMany({});
    await Installation.deleteMany({});
    done();
})

test('webhook receive event valid', async (done) => {
    const response = await request.post('/receive-webhook')
        .set({
            'X-GitHub-Event': 'pull_request',
            'X-GitHub-Delivery': '1'
        })
        .send(FakeData.makePullRequestPayload('opened', false))

    expect(response.status).toBe(HTTPStatusCode.OK);

    done()
})

Versions:

  • node v14.10.1
  • express v4.16.1
  • jest v26.6.3

(Adding some more text since SO says my post is mostly code)

Aucun commentaire:

Enregistrer un commentaire