gpt4 book ai didi

javascript - 由于待处理的 MongoDB 连接(@hapi/lab、mongodb-memory-server、node),测试未完成

转载 作者:行者123 更新时间:2023-12-02 22:16:47 25 4
gpt4 key购买 nike

我遇到了这个问题,所有测试都成功通过,但最终,该过程永远不会结束。我可以看到始终有两个 mongod 进程实例在运行。

我正在使用 mongodb-memory-server 库来模拟数据库连接、最新版本的 mongodbhapijs 环境。

const { expect } = require ('@hapi/code');
const lab = exports.lab = require ('@hapi/lab').script ();
const { it, describe, after, before } = lab;
const Sinon = require ('sinon');
const DAO = require ('./index');
const CaseBaseDao = require ('case-base-dao'); //from private npm repo

let baseDaoFindStub;
let updateOneStub;

describe ('Profiles DAO', () =>
{
before (async ({ context }) =>
{
const { MongoClient } = require ('mongodb');
const { MongoMemoryServer } = require ('mongodb-memory-server');
const mongoServerInstance = new MongoMemoryServer ({ instance: { port: 27018, dbName: 'administration' } });

const url = await mongoServerInstance.getConnectionString ();
const dbClient = await MongoClient.connect (url);
const collection = await dbClient.db ().createCollection ('profiles');
await collection.insertMany (require ('./examples/profiles.json'));
const dao = DAO () (dbClient.db ());

context.dbClient = dbClient;
context.mongoServerInstance = mongoServerInstance;
context.sandbox = Sinon.createSandbox ();
context.dao = dao;

baseDaoFindStub = context.sandbox.stub (CaseBaseDao.BaseDao.prototype, 'find').resolves ();
updateOneStub = context.sandbox.stub (dao.collection, 'updateOne').resolves ();
});

after (async ({ context: { sandbox, dbClient, mongoServerInstance } }) =>
{
await dbClient.close (); // only added for debug purposes
await mongoServerInstance.stop (); // only added for debug purposes
sandbox.restore ();
});

it ('Should expose DAO methods', ({ context: { dao } }) =>
{
expect (dao).to.be.an.object ();
expect (dao.findByEmail).to.exist ();
expect (dao.find).to.exist ();
expect (dao.findByQuery).to.exist ();
expect (dao.updateOneById).to.exist ();
expect (dao.addSmartPerformanceDashboards).to.exist ();
expect (dao.deleteSmartPerformanceDashboards).to.exist ();
expect (dao.updateSmartPerformanceDashboards).to.exist ();
});
});

还有很多测试,但如果我只执行这个,它就会卡在最后。

如果我尝试在 after block 中手动关闭连接,则会出现以下错误:

对于await dbClient.close()

message:"Illegal invocation"

stack:"TypeError: Illegal invocation\n at handleWriteReq (internal/stream_base_commons.js:46:26)\n at writeGeneric (internal/stream_base_commons.js:139:15)\n at Socket._writeGeneric (net.js:771:11)\n at Socket._write (net.js:783:8)\n at doWrite (_stream_writable.js:431:12)\n at writeOrBuffer (_stream_writable.js:415:5)\n at Socket.Writable.write (_stream_writable.js:305:11)\n at Connection.write ({path}/node_modules/mongodb/lib/core/connection/connection.js:265:21)\n at {path}/node_modules/mongodb/lib/core/connection/pool.js:1208:40\n at checkStatus ({path}/node_modules/mongodb/lib/core/connection/pool.js:732:21)\n at Pool.destroy ({path}/node_modules/mongodb/lib/core/connection/pool.js:739:3)\n at Server.destroy ({path}/node_modules/mongodb/lib/core/topologies/server.js:897:15)\n ...

对于await mongoServerInstance.stop();

message:"Method Promise.prototype.then called on incompatible receiver #"

stack:"TypeError: Method Promise.prototype.then called on incompatible receiver #\n at Promise.then ()"

我几乎尝试了所有方法:更改端口、使用 Promise 而不是 async/await、避免使用 Sinon 和 stub (以防万一)等等。但我已经没有主意了。

我在其他项目中使用完全相同的代码来模拟数据库,它工作得很好,不需要手动关闭数据库连接,它会自动关闭。

这发生在我的本地环境以及 Bitbucket 管道中,因此,将其丢弃为与我的计算机相关的内容。

最佳答案

终于解决了。

不知道为什么会发生这种情况,但解决方案不是通过 @hapi/lab context 使用 mongo 实例相关的变量来关闭 MongoDB 连接,而是在 before 之外定义它们> 阻止。所以:

let baseDaoFindStub;
let updateOneStub;
let mongoServerInstance; // Now defined here
let dbClient; // Now defined here

describe ('Profiles DAO', () =>
{
before (async ({ context }) =>
{
const { MongoClient } = require ('mongodb');
const { MongoMemoryServer } = require ('mongodb-memory-server');
mongoServerInstance = new MongoMemoryServer ({ instance: { port: 27018, dbName: 'administration' } });

const url = await mongoServerInstance.getConnectionString ();
dbClient = await MongoClient.connect (url);
const collection = await dbClient.db ().createCollection ('profiles');
await collection.insertMany (req
...
...
...

after (async ({ context: { sandbox } }) => // Don't get vars from context
{
sandbox.restore ();
await dbClient.close ();
await mongoServerInstance.stop ();
});

我不是第一次使用 @hapi/lab context 遇到此类问题,所以请小心!

关于javascript - 由于待处理的 MongoDB 连接(@hapi/lab、mongodb-memory-server、node),测试未完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59363929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com