最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Import a file after the Jest environment has been torn down - Stack Overflow

programmeradmin5浏览0评论

I'm making a simple API with Express and I'm trying to add tests with Jest but when I try to run the tests it displays the next error:

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

      at BufferList.Readable (node_modules/readable-stream/lib/_stream_readable.js:179:22)
      at BufferList.Duplex (node_modules/readable-stream/lib/_stream_duplex.js:67:12)
      at new BufferList (node_modules/bl/bl.js:33:16)
      at new MessageStream (node_modules/mongodb/lib/cmap/message_stream.js:35:21)
      at new Connection (node_modules/mongodb/lib/cmap/connection.js:52:28)
/home/jonathangomz/Documents/Node/Express/Devotionals/node_modules/readable-stream/lib/_stream_readable.js:111
  var isDuplex = stream instanceof Duplex;
                        ^

TypeError: Right-hand side of 'instanceof' is not callable

I'm not sure to trust the result if right after jest break (or something like that):

My test is:

const app = require("../app");
const request = require("supertest");

describe("Testing root router", () => {
  test("Should test that true === true", async () => {
    jest.useFakeTimers();
    const response = await request(app).get("/");
    expect(response.status).toBe(200);
  });
});

My jest configuration on package.json:

"jest": {
    "testEnvironment": "node",
    "coveragePathIgnorePatterns": [
      "/node_modules/"
    ]
  }

Notes:

I read about jest.useFakeTimers() but It's not working and I'm not sure if I'm using in the wrong way. I also tried adding it to the beforeEach method but nothing.

I'm making a simple API with Express and I'm trying to add tests with Jest but when I try to run the tests it displays the next error:

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

      at BufferList.Readable (node_modules/readable-stream/lib/_stream_readable.js:179:22)
      at BufferList.Duplex (node_modules/readable-stream/lib/_stream_duplex.js:67:12)
      at new BufferList (node_modules/bl/bl.js:33:16)
      at new MessageStream (node_modules/mongodb/lib/cmap/message_stream.js:35:21)
      at new Connection (node_modules/mongodb/lib/cmap/connection.js:52:28)
/home/jonathangomz/Documents/Node/Express/Devotionals/node_modules/readable-stream/lib/_stream_readable.js:111
  var isDuplex = stream instanceof Duplex;
                        ^

TypeError: Right-hand side of 'instanceof' is not callable

I'm not sure to trust the result if right after jest break (or something like that):

My test is:

const app = require("../app");
const request = require("supertest");

describe("Testing root router", () => {
  test("Should test that true === true", async () => {
    jest.useFakeTimers();
    const response = await request(app).get("/");
    expect(response.status).toBe(200);
  });
});

My jest configuration on package.json:

"jest": {
    "testEnvironment": "node",
    "coveragePathIgnorePatterns": [
      "/node_modules/"
    ]
  }

Notes:

I read about jest.useFakeTimers() but It's not working and I'm not sure if I'm using in the wrong way. I also tried adding it to the beforeEach method but nothing.

Share Improve this question edited Sep 13, 2023 at 20:55 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Sep 8, 2020 at 23:33 Jonathan Gómez PérezJonathan Gómez Pérez 2972 gold badges4 silver badges16 bronze badges 2
  • Hi, have you found a solution? I've got the exact same issue than you. – DoneDeal0 Commented Apr 4, 2021 at 17:37
  • @DoneDeal0 no :c it's a side project for learning so I pause the Testing part, sorry – Jonathan Gómez Pérez Commented Apr 6, 2021 at 15:10
Add a ment  | 

4 Answers 4

Reset to default 0

In my case, I had to add the package to transformIgnorePatterns in the jest config.

By adding jest.useFakeTimers() just after all your import.

What about making your test async ?

const app = require("../app");
const request = require("supertest");

describe("Testing root router",async () => {
  test("Should test that true === true", async () => {
    jest.useFakeTimers();
    const response = await request(app).get("/");
    expect(response.status).toBe(200);
  });
});

Add jest.useFakeTimers('modern') before the asynchronous call. Add jest.runAllTimers() after the asynchronous call. This will fast-forward timers for you.

const app = require("../app")
const request = require("supertest")

describe("Testing root router", () => {
  test("Should test that true === true", async () => {

    //Before asynchronous call
    jest.useFakeTimers("modern")

    const response = await request(app).get("/")

    //After asynchronous call
    jest.runAllTimers()

    expect(response.status).toBe(200)
  })
})

Try adding --testTimeout=10000 flag when calling jest, it works for me.

Information based on Testing NodeJs/Express API with Jest and Supertest

--testTimeout flag - This increases the default timeout of Jest which is 5000ms. This is important since the test runner needs to refresh the database before running the test

发布评论

评论列表(0)

  1. 暂无评论