【问题标题】:Cannot run test API call using Jest / mongodb-memory-server (problem setimeout)无法使用 Jest / mongodb-memory-server 运行测试 API 调用(问题 setimeout)
【发布时间】:2021-11-17 18:07:19
【问题描述】:

我确实使用 jest / mongodb-memory-server 在我的应用程序中测试了许多功能,它们都运行良好。但是由于某些原因我无法运行 API 调用测试。

每次我尝试运行 api 调用时,我都会遇到 setimout 问题,即使我增加它仍然无法正常工作。我的函数只是返回一个用户列表......没有更多。

 FAIL  Tests/Controller/auth.test.js (11.991 s)
  ● Console

    console.warn
      Using NodeJS below 12.22.0

      at Object.<anonymous> (node_modules/mongodb-memory-server-core/src/util/MongoInstance.ts:21:11)
      at Object.<anonymous> (node_modules/mongodb-memory-server-core/src/MongoMemoryServer.ts:13:1)

  ● test updateSchemaField() › get users list

    MongooseError: Can't call `openUri()` on an active connection with different connection strings. Make sure you aren't calling `mongoose.connect()` multiple times. See: https://mongoosejs.com/docs/connections.html#multiple_connections

      10 |     useUnifiedTopology: true,
      11 |   };
    > 12 |   await mongoose.connect(uri, mongooseOpts);
         |                  ^
      13 | };
      14 |
      15 | module.exports.closeDatabase = async () => {

      at NativeConnection.Object.<anonymous>.Connection.openUri (node_modules/mongoose/lib/connection.js:697:13)
      at node_modules/mongoose/lib/index.js:330:10
      at node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
      at promiseOrCallback (node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
      at Mongoose.Object.<anonymous>.Mongoose._promiseOrCallback (node_modules/mongoose/lib/index.js:1151:10)
      at Mongoose.connect (node_modules/mongoose/lib/index.js:329:20)
      at Object.<anonymous>.module.exports.connect (Tests/db.js:12:18)
      at Tests/Controller/auth.test.js:5:23

  ● test updateSchemaField() › get users list

    thrown: "Exceeded timeout of 5000 ms for a hook.
    Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."

       5 | beforeAll(async () => await db.connect());
       6 |
    >  7 | afterEach(async () => await db.clearDatabase());
         | ^
       8 |
       9 | afterAll(async () => await db.closeDatabase());
      10 |

      at Object.<anonymous> (Tests/Controller/auth.test.js:7:1)


  ● Test suite failed to run

    thrown: "Exceeded timeout of 5000 ms for a hook.
    Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."

       7 | afterEach(async () => await db.clearDatabase());
       8 |
    >  9 | afterAll(async () => await db.closeDatabase());
         | ^
      10 |
      11 | describe("test updateSchemaField()", (done) => {
      12 |   it("get users list", async () => {

      at Object.<anonymous> (Tests/Controller/auth.test.js:9:1)

 PASS  Tests/Data/schemaCRUD.test.js
  ● Console

    console.warn
      Using NodeJS below 12.22.0

      at Object.<anonymous> (node_modules/mongodb-memory-server-core/src/util/MongoInstance.ts:21:11)
      at Object.<anonymous> (node_modules/mongodb-memory-server-core/src/MongoMemoryServer.ts:13:1)

 PASS  Tests/Utils/user.test.js

Test Suites: 1 failed, 2 passed, 3 total
Tests:       1 failed, 9 passed, 10 total
Snapshots:   0 total
Time:        12.859 s, estimated 13 s
Ran all test suites matching /.\/Tests/i.
Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

测试

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

beforeAll(async () => await db.connect());

afterEach(async () => await db.clearDatabase());

afterAll(async () => await db.closeDatabase());

describe("test updateSchemaField()", () => {
  it("get users list", async () => {
    const response = await request(app).get("/api/auth/test");
    console.log(response);
  });
});

【问题讨论】:

    标签: node.js jestjs


    【解决方案1】:

    脚本正在尝试连接多个 URI。您在测试文件中建立数据库连接很好,但在 app.js 中还有另一个具有不同 URI 的数据库连接调用。

    【讨论】:

    • 是的,但我不明白为什么我的其他测试文件中没有这个问题(我也有类似结构的 mongodb-memory-server 连接)?
    • 你能不能只运行失败的测试用例并检查结果。
    • 结果是一样的。为了让它工作,我不知道为什么,但我必须停止我的 docker 然后它才能工作。我不明白为什么,因为 mongodb-memory-server 不应该运行我在我的“真实”数据库app.js
    猜你喜欢
    • 2021-02-01
    • 2021-10-15
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 2020-05-07
    相关资源
    最近更新 更多