QueryFailedError: SQLITE_MISUSE: Database handle is closed (sqllite memory Jest test)

Viewed 666
import app from "../src/app";
import request from "supertest";
import { createConnection, getConnection, getRepository } from "typeorm";
import { Group } from "../src/entity";

beforeEach(() => {
    return createConnection({
        type: "sqlite",
        database: ":memory:",
        dropSchema: true,
        entities: ["src/entity/**/*.ts"],
        synchronize: true,
        logging: false,
    });
});

afterEach(() => {
    const conn = getConnection();
    return conn.close();
});

const group = new Group();
group.creator_account_id = 1;
group.group_name = "cook101";
group.group_description = "cook101 with best chef";
group.photoName = "cook101";
group.photoUrl = "http://www.youtube.com/sd.jpg";
group.photoKey = "cook101chef";


// subscribe group
describe("/subscribe/:group_id/:account_id route test", () => {
    it("Should return a status of 201 with json data", async () => {
        await group.save();
        await request(app)
            .post("/v1/subscribe/1/1")
            .expect(201)
            .then((res) => {
                expect(res.body.groupAccount.account_id).toBe(1);
            });
    });

    it("Should return a status of 400", async () => {
        await request(app).post("/v1/subscribe/1x/1x").expect(400);
    });
});

// error:

 FAIL  tests/group.test.ts (9.678 s)
  ✕ Should return a status of 400 (15 ms)
  /subscribe/:group_id/:account_id route test
    ✓ Should return a status of 201 with json data (241 ms)

  ● Should return a status of 400

    QueryFailedError: SQLITE_MISUSE: Database handle is closed

      at new QueryFailedError (src/error/QueryFailedError.ts:9:9)
      at handler (src/driver/sqlite/SqliteQueryRunner.ts:79:26)
      at replacement (node_modules/sqlite3/lib/trace.js:25:27)
      at Database.errBack (node_modules/sqlite3/lib/sqlite3.js:14:21)

  ● Should return a status of 400

    CannotExecuteNotConnectedError: Cannot execute operation on "default" connection because connection is not yet established.

      at new CannotExecuteNotConnectedError (src/error/CannotExecuteNotConnectedError.ts:8:9)
      at Connection.<anonymous> (src/connection/Connection.ts:226:19)
      at step (node_modules/tslib/tslib.js:141:27)
      at Object.next (node_modules/tslib/tslib.js:122:57)
      at node_modules/tslib/tslib.js:115:75
      at Object.__awaiter (node_modules/tslib/tslib.js:111:16)
      at Connection.Object.<anonymous>.Connection.close (node_modules/typeorm/connection/Connection.js:169:24)
      at Object.<anonymous> (tests/group.test.ts:19:14)

ReferenceError: You are trying to import a file after the Jest environment has been torn down.

  at src/util/DirectoryExportedClassesLoader.ts:41:22
      at Array.map (<anonymous>)
  at Object.importClassesFromDirectories (src/util/DirectoryExportedClassesLoader.ts:41:10)
  at ConnectionMetadataBuilder.Object.<anonymous>.ConnectionMetadataBuilder.buildEntityMetadatas (src/connection/ConnectionMetadataBuilder.ts:59:56)
  at Connection.Object.<anonymous>.Connection.buildMetadatas (src/connection/Connection.ts:517:59)
2 Answers

You will encounter this error whenever you call asynchronous functions of TypeORM classes such as the QueryBuilder, Repository, or EntityManager, without awaiting them. This causes them to be executed after the connection has already been closed elsewhere. Go over your code carefully and make sure that you async/await all asynchronous TypeORM actions, and if these calls are in functions of their own, await those as well, all the way back up to the top level function.

Try converting all your methods to async. beforeEach(async () => {....} afterEach(async () => {...} ...

Related