MongoError:池正在耗尽,在集成测试中使用MongoMemoryServer时禁止进行新操作 [英] MongoError: pool is draining, new operations prohibited when using MongoMemoryServer in integration test
问题描述
我正在使用MongoMemoryServer
编写集成测试.我有两个集成测试文件.当我运行IT测试时,会看到以下内容.我不明白为什么.我正在使用jestjs测试框架.
I'm using MongoMemoryServer
to write an integration test. I have two integration test files. When I run the IT tests I see the following. I don't understand why. I'm using jestjs test framework.
当我有两个IT测试文件时,看到以下错误
I'm seeing the following error when I have two IT test files
MongoError: pool is draining, new operations prohibited
37 | for (const key in collections) {
38 | const collection = collections[key];
> 39 | await collection.deleteMany();
| ^
40 | }
41 | };
这是我的设置
//db-handler.js
const mongoose = require("mongoose");
const { MongoMemoryServer } = require("mongodb-memory-server");
const mongod = new MongoMemoryServer();
module.exports.connect = async () => {
const uri = await mongod.getConnectionString();
const mongooseOpts = {
useNewUrlParser: true,
autoReconnect: true,
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 1000,
};
await mongoose.connect(uri, mongooseOpts);
};
module.exports.closeDatabase = async () => {
await mongoose.connection.dropDatabase();
await mongoose.connection.close();
await mongod.stop();
};
module.exports.clearDatabase = async () => {
const collections = mongoose.connection.collections;
for (const key in collections) {
const collection = collections[key];
await collection.deleteMany();
}
};
我所有的IT测试设置都像这样
All my IT tests setup looks like this
//example.it.test.js
const supertest = require("supertest");
const dbHandler = require("./db-handler");
const app = require("../../src/app");
const request = supertest(app);
const SomeModel = require("../Some");
beforeAll(async () => await dbHandler.connect());
afterEach(async () => await dbHandler.clearDatabase());
afterAll(async () => await dbHandler.closeDatabase());
describe("Some Test Block", () => {
it("Test One", async (done) => {
await SomeModel.create({a: "a", b "b"});
const response = await request.get("/endPointToTest");
expect(response.status).toBe(200);
done();
});
当我只有一个IT测试文件时,一切正常.当我引入一个类似于example.it.test.js
的新IT测试文件时,新测试将失败.上面的示例错误消息.
When I have just a single IT test files, everything works fine. When I introduce a a new IT test file similar setup up as example.it.test.js
, then the new test fails. The example error message above.
我想念什么?当我有多个IT测试文件时,是否需要更改设置?
What am I missing? Does my setup need to change when I have multiple IT test files?
更新一: 我的package.json文件看起来像
UPDATE ONE: My package.json file looks like
{
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js",
"test": "jest --runInBand ./tests"
},
"devDependencies": {
"jest": "^25.2.7",
"mongodb-memory-server": "^6.5.2",
"nodemon": "^2.0.2",
"supertest": "^4.0.2"
},
"jest": {
"testEnvironment": "node",
"coveragePathIgnorePatterns": [
"/node_modules/"
]
}
}
推荐答案
默认情况下,Jest与运行测试的子进程的工作池"并行运行测试(Jest CLI文档).根据Jest文档.
By default Jest runs tests in parallel with a "a worker pool of child processes that run tests" (Jest CLI docs). As per the Jest documentation.
每个测试文件都与导致错误的mogodb服务器建立了新连接.
Each test file is making a new connection to the mogodb server which is causing the error.
您可以运行Jest
测试sequentially
来避免此问题.
You could run the Jest
test sequentially
to avoid this issue.
通过Jest CLI的--runInBand或-i选项,您可以按顺序(非并行模式)运行测试.
The --runInBand or -i option of the Jest CLI allows you to run tests sequentially (in non-parallel mode).
如果您对节点js或mongoose使用官方的mongodb库,则可能会发生此错误(池正在耗尽,禁止进行新操作).建立mongodb连接时,请指定池大小.
This error (pool is draining, new operations prohibited) might occur if you are using the official mongodb library for node js or mongoose. Please specifying pool size while establishing mongodb connection.
module.exports.connect = async () => {
const uri = await mongod.getConnectionString();
const mongooseOpts = {
useNewUrlParser: true,
autoReconnect: true,
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 1000,
poolSize: 10,
};
await mongoose.connect(uri, mongooseOpts);
};
这篇关于MongoError:池正在耗尽,在集成测试中使用MongoMemoryServer时禁止进行新操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!