我们正在使用mongodb-prebuilt
软件包进行单位/集成测试(CI/CD),用于开发的软件,该软件与MongoDB数据库进行了交互。
常见用例如下:使用Prebuild软件包启动测试MongoDB服务器,执行测试套件执行然后终止服务器。第一个和第二个项目很容易完成,但第三项会引起一些问题。
如果尚未终止mongodb-prebuilt
启动的服务器,则测试跑步者将永远悬挂。相反,如果尝试通过testConnection.command({ shutdown: 1 })
命令终止服务器,则unhandledRejection
fires是指关闭连接时 - 当然,这是通过停止服务器强制关闭的。
在afterAll
测试部分处置mongodb-prebuilt
的正确方法是正确的?测试引擎是jest
,但并不重要。
示例代码在这里:
import { MongodHelper } from 'mongodb-prebuilt';
import { MongoClient } from 'mongodb';
describe('Test suite', () => {
let testConnection;
beforeAll(async () => {
const mongodHelper = new MongodHelper(['--port', "27018"]);
await mongodHelper.run();
testConnection = await MongoClient.connect(`mongodb://localhost:27018/admin`);
});
afterAll(async () => {
await testConnection.dropDatabase();
await testConnection.command({ shutdown: 1 });
await testConnection.close();
testConnection = null;
});
/* Here follows some test suite that uses testConnection */
}
有一些尝试解决问题的尝试:
1)不要等待 testConnection.command({ shutdown: 1 })
的承诺,并立即启动客户连接关闭 - 它在某些机器上工作,但很可能取决于执行速度,因此工作不稳定。
2)由于测试结束时客户端连接终止并不重要 - 可以在afterAll
部分中设置process.on('unhandledRejection', ...)
处理程序,而只是静音异常 - 但似乎在意识形态上是不正确的
所以,也许有刻板印象的解决方案?
重要注释:建议使用一些模拟软件包实时mongodb
根本不合适,因为开发的软件是mongodb
的特定适配器,应尊重实时数据库的所有方面因此,在
最初问题的最方便的解决方案是使用mongo-unit
NPM软件包,尽管名称,但它为使用Real MongoDB Prebuild Server提供了自动集成测试的API,包括启动和停止它:https://github.com/mikhail-angelov/mongo-unit/
带有mongo-unit
的仪表测试套件看起来如下:
import 'regenerator-runtime/runtime';
import mongoUnit from 'mongo-unit';
import { MongoClient } from 'mongodb';
describe('MongoDB-based test suite, () => {
let testConnection;
beforeAll(async () => {
const connectionUrl = await mongoUnit.start({ dbName: 'admin' });
testConnection = await MongoClient.connect(connectionUrl);
});
afterAll(async () => {
await testConnection.dropDatabase();
await testConnection.close(true);
await mongoUnit.stop();
testConnection = null;
});
/* Test suite goes here */
});
软件包还允许在开始时选择初始数据库,并在测试套件结束时执行清晰的MongoDB服务器进程。