I have a simple Express
application that exposes a RESTful API. It uses Knex
and Objection
to access the database, and Jest
/ Supertest
for testing the API.
I have a test that starts the server, fetches the available data from a given route and asserts the received values. Everything works fine, except that Jest
never exits after executing this test.
This is my test:
import { app } from "../../src/application.js";
import { describe, expect, test } from "@jest/globals";
import request from "supertest";
describe("Customer Handler", () => {
test("should retrieve all existing customer(s)", async () => {
const expected = [
// ...real values here; omitted for brevity
];
const response = await request(app).get(`/api/customers`);
expect(response.statusCode).toStrictEqual(200);
expect(response.headers["content-type"]).toContain("application/json");
expect(response.body).toStrictEqual(expected);
});
});
The application.js
file looks very much like a usual Express
setup/configuration file:
import { CustomerHandler } from "./handler/customer.handler.js";
import connection from "../knexfile.js";
import express from "express";
import Knex from "knex";
import { Model } from "objection";
const app = express();
Model.knex(Knex(connection[process.env.NODE_ENV]));
// ...removed helmet and some other config for brevity
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use("/api/customers", CustomerHandler);
app.use((err, req, res, next) => {
res.status(err.status || 500);
res.json({
errors: {
error: err,
message: err.message,
},
});
next();
});
export { app };
I've tried --detectOpenHandles
, but nothing else is printed in the console, so I can't see any hints about what the issue might be — which I suspect it's Knex
/ Objection
because I'm using SQLite
, so probably the connection to the database is not getting closed.
In the meantime I'm using
--forceExit
, but I would like to figure it out why isJest
not able to exit.