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

50 Views Asked by At

I starting an app using express + typescript + typeORM + mysql 2 + Jest

I create my first test using ts-jest and supertest like this:

import request from "supertest";
import { app } from "../src/app";

describe("Test app.ts", () => {
  test("Catch-all route", async () => {
    const res = await request(app).get("/");
    expect(res.body).toEqual({ message: "Allo! Catch-all route." });
  });
});

But when i run npm run test got this

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        2.038 s
Ran all test suites.

ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From __tests__/TestController.spec.ts.

      at ClientHandshake.handshakeResult (node_modules/mysql2/lib/commands/client_handshake.js:177:26)
      at ClientHandshake.execute (node_modules/mysql2/lib/commands/command.js:45:22)
      at PoolConnection.handlePacket (node_modules/mysql2/lib/connection.js:478:34)
      at PacketParser.onPacket (node_modules/mysql2/lib/connection.js:97:12)
      at PacketParser.executeStart (node_modules/mysql2/lib/packet_parser.js:75:16)
      at Socket.<anonymous> (node_modules/mysql2/lib/connection.js:104:25)

The test created run without problem but I got this error after the test pass.

I try using this:

jest.useFakeTimers({timerLimit: 10000});

But has no effect

0

There are 0 best solutions below