MongoError: Cannot create collection users - database is in the process of being dropped

MongoError: Cannot create collection users - database is in the process of being dropped

我有一个 REST API,我正在为这个项目编写 TDD。我的 TDD 由两部分组成:路由和服务。我选择使用 Jest。我有一个用于测试的 MongoDB 数据库。每次测试完成后,我使用 afterAll() 方法重置我的数据库。在这个方法中,我 运行 mongoose.connection.dropDatabase 函数。

当我运行只有一个测试文件时没有错误,但是当我运行多个测试文件时,我得到一个错误。错误信息:

MongoError: Cannot create collection auth-db.users - database is in the process of being dropped.

我与您分享示例代码:

users.route.test.ts:

https://gist.github.com/mksglu/8c4c4a3ddcb0e56782725d6457d97a0e

users.service.test.ts:

https://gist.github.com/mksglu/837202c1048687ad33b4d1dee01bd29c

当我的所有测试 运行 时,“sometimes”都出错。我写了上面的错误信息。出现此错误的原因是重置过程仍在继续。我无法解决这个问题。如果您能提供帮助,我将不胜感激。

谢谢。

https://jestjs.io/docs/en/cli.html#runinband

您正在寻找的是--运行InBand 命令。这让 运行 连续开玩笑,而不是创建一个 运行 测试

的子进程工作池