'Jest: App.use() requires a middleware function when app is not even being used

doing my school project with node, express, typescript and jest. When checking for my test suite, it gives me the following error:

TypeError: app.use() requires a middleware function

  17 | const app = express();
  18 |
> 19 | app.use(validating_db_status);
     |     ^
  20 | // Authenticate user
  21 | app.use(bodyParser.json());
  22 | app.use(authenticate_user)

Even though, in the test where the error is raised (DB_interface.test.ts) there is no reference to the app object or the validating_db_status function.

DB_interface.test.ts (where the error is raised) code:

import { DB_interface, get_db_uri } from "./DB_interface"

describe("DB_interface", () => {
    it("Connection on initialization", async () => {
        const db = new DB_interface({
            connectionString: get_db_uri()
        });
        expect(db.connected()).toBe(true);
        db.close();
        expect(db.connected()).toBe(false);
    });
    
    it("Connection after initialization", async () => {
        const db = new DB_interface({
            connectionString: get_db_uri()
        }, false);
        expect(db.connected()).toBe(false);
        db.connect();
        expect(db.connected()).toBe(true);
        db.close();
        expect(db.connected()).toBe(false);
    });

    it("Re-open connection", async () => {
        const db = new DB_interface({
            connectionString: get_db_uri()
        });
        expect(db.connected()).toBe(true);
        db.close();
        expect(db.connected()).toBe(false);
        db.connect();
        expect(db.connected()).toBe(true);
        db.close();
        expect(db.connected()).toBe(false);
    });
});

Continents.test.ts (here all tests pass):

import { app } from "../../app";
import { DB_interface, get_db_uri } from "../../logic/db_interface/DB_interface";
import supertest from "supertest";

describe("Continents-routes-testing", () => {
    // SETUP
    const request = supertest(app);
    const base_url = "/continents";
    const continents_response = [
        {id: 0, it_name: "Europa"},
        {id: 1, it_name: "Asia"},
        {id: 2, it_name: "Nord America"},
        {id: 3, it_name: "Sud America"},
        {id: 4, it_name: "Africa"},
        {id: 5, it_name: "Oceania"},
        {id: 6, it_name: "Antartica"},
        {id: 7, it_name: "America Centrale"}
    ];

    const countries = {
        "Italy": {id: 5, continent_id: 0},
        "France": {id: 7, continent_id: 0},
        "Mexico": {id: 8, continent_id: 2},
    };

    app.locals.DEFAULT_DB_INTERFACE = new DB_interface({
        connectionString: get_db_uri()
    }, true);

    const error_codes = {
        no_country_id: "continents_1"
    }

    
    it("Gets the /list_all endpoint", async () => {
        const response = await request.get(`${base_url}/list_all`);
        expect(response.status).toBe(200);
        const obj = JSON.parse(response.text);
        for(let i = 0; i < obj.length; i++) {
            expect(obj[i]).toEqual(continents_response[i]);
        }
    });

    it("Gets the /list_single/:continent_id endpoint", async () => {
        const response = await request.get(`${base_url}/list_single/0`);
        expect(response.status).toBe(200);
        const obj = JSON.parse(response.text);
        expect(obj[0]).toEqual(continents_response[0]);
    });

    it("Gets the /continent_of_country/:country_id endpoint", async () => {
        const ids = [
            countries["Italy"],
            countries["France"],
            countries["Mexico"]
        ];
        for (let country of ids) {
            const response = await request.get(`${base_url}/continent_of_country?country_id=${country.id}`);
            expect(response.status).toBe(200);
            const obj = JSON.parse(response.text);
            expect(obj[0]["id"]).toEqual(country.continent_id);
        }
        const response = await request.get(`${base_url}/continent_of_country`);
        const obj = JSON.parse(response.text);
        expect(response.status).toBe(400);
        expect(obj).toEqual({error: error_codes.no_country_id});
    });

    afterAll(async () => {
        app.locals.DEFAULT_DB_INTERFACE.close();
    });

});

App.ts code:

import "dotenv/config";
import express from "express";
import db_shortcut_router from "./routes/dev_shortcuts/DB_shortcuts"; // Remove this in production code
import users_router from "./routes/users/users";
import countries_router from "./routes/countries/countries";
import continents_router from "./routes/continents/continents";
import cities_router from "./routes/cities/cities";
import { validating_db_status, DB_result, QueryResult } from "./logic/db_interface/DB_interface";
import { authenticate_user } from "./logic/users/utils";
import bodyParser from "body-parser";

const app = express();

app.use(validating_db_status);
// Authenticate user
app.use(bodyParser.json());
app.use(authenticate_user)

app.use("/db_shortcuts", db_shortcut_router); // Remove this in production code

app.use("/countries", countries_router);
app.use("/users", users_router);
app.use("/continents", continents_router);
app.use("/cities", cities_router);


app.get("/", (req, res) => {
    res.status(200).send("Hello World!");
});


function send_json(res: express.Response, result: DB_result | string, processing_func?: (arg: Array<QueryResult<any>>) => Object) {
    if(typeof result === "string")
        result = {
            error: result
        } as DB_result;

    if (result.result) {
        if(processing_func === undefined) processing_func = (result) => { return result[0].rows; };
        res.status(200).send(processing_func(result.result));
    }
    else {
        const status = (result.error?.startsWith("i")) ? 500 : 400;
        res.status(status).send({error: result.error});
    }
}

export { send_json, app };


Sources

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

Source: Stack Overflow

Solution Source