【问题标题】:Import a file after the Jest environment has been torn down在 Jest 环境被拆除后导入文件
【发布时间】:2020-12-27 08:48:53
【问题描述】:

我正在使用 Express 制作一个简单的 API,我正在尝试使用 Jest 添加测试,但是当我尝试运行测试时,它会显示下一个错误:

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

如果在开玩笑之后(或类似的事情),我不确定是否相信结果:

我的测试是:

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);
  });
});

我在package.json上的笑话配置:

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

注意事项:

我阅读了有关 jest.useFakeTimers() 的信息,但它不起作用,我不确定我是否以错误的方式使用。我也尝试将它添加到 beforeEach 方法中,但没有。

【问题讨论】:

  • 您好,您找到解决方案了吗?我遇到了和你一样的问题。
  • @DoneDeal0 no :c 这是一个学习项目,所以我暂停了测试部分,抱歉

标签: javascript api express jestjs


【解决方案1】:

尝试在调用 jest 时添加--testTimeout=10000 标志,它对我有用。

基于Testing NodeJs/Express API with Jest and Supertest的信息

--testTimeout 标志 - 这会增加 Jest 的默认超时时间,即 5000 毫秒。这很重要,因为测试运行器需要在运行测试之前刷新数据库

【讨论】:

  • 对我们的案例没有影响:(
猜你喜欢
  • 2022-01-06
  • 2021-11-03
  • 2020-07-02
  • 2018-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-29
相关资源
最近更新 更多