【发布时间】: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);
});
});
【问题讨论】: