Node.js 使用Mongoose和副本集运行Jest测试
我正在尝试使用SuperTest运行JESTAPI测试。我在事务中使用MongoDB 4.2。当我运行Jest时,MongoDB抛出一个错误。请看下面 db.js server.jsNode.js 使用Mongoose和副本集运行Jest测试,node.js,mongodb,mongoose,jestjs,Node.js,Mongodb,Mongoose,Jestjs,我正在尝试使用SuperTest运行JESTAPI测试。我在事务中使用MongoDB 4.2。当我运行Jest时,MongoDB抛出一个错误。请看下面 db.js server.js const express = require('express'); const app = express(); const logger = require('./services/logger'); const port = process.env.PORT || 3000; // get all rou
const express = require('express');
const app = express();
const logger = require('./services/logger');
const port = process.env.PORT || 3000;
// get all routes
require('./routing')(app);
app.listen(port, () => logger.info(`App is listening on port ${port}!`));
module.exports = app
jest.config.js
module.exports = {
coveragePathIgnorePatterns: ['/node_modules/'],
preset: '@shelf/jest-mongodb',
verbose: true,
}
user.test.js
const request = require("supertest");
const faker = require('faker');
const db = require('../db')
const app = require('../../server');
describe('user routes', () => {
beforeAll(async () => await db.connect());
afterAll(async () => await db.disconnect());
it('should register new user', done => {
const password = faker.internet.password();
request(app)
.post('/v1/users/register')
.send({
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
email: faker.internet.email(),
password,
confirmPassword: password,
termsConditions: true,
})
.expect(200, done);
});
});
这是我在控制台中收到的错误
> jest
(node:10487) [MDEP001] DeprecationWarning: "MongoMemoryReplSet.getConnectionString" is deprecated, use ".getUri"
(Use `node --trace-deprecation ...` to show where the warning was created)
info: App is listening on port 3000!
FAIL __test__/routes/users.test.js
user routes
✕ should register new user (153 ms)
● user routes › should register new user
expected 200 "OK", got 400 "Bad Request"
21 | termsConditions: true,
22 | })
> 23 | .expect(200, done);
| ^
24 | });
25 | });
at Object.<anonymous> (__test__/routes/users.test.js:23:14)
----
at Test.Object.<anonymous>.Test._assertStatus (node_modules/supertest/lib/test.js:296:12)
at node_modules/supertest/lib/test.js:80:15
at Test.Object.<anonymous>.Test._assertFunction (node_modules/supertest/lib/test.js:311:11)
at Test.Object.<anonymous>.Test.assert (node_modules/supertest/lib/test.js:201:21)
at Server.localAssert (node_modules/supertest/lib/test.js:159:12)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 1.976 s, estimated 2 s
Ran all test suites.
error: MongoError: This MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string.
at getMMAPError (/home/yehuda_fievel/repos/books/server/node_modules/mongodb/lib/core/topologies/shared.js:413:18)
at handler (/home/yehuda_fievel/repos/books/server/node_modules/mongodb/lib/core/sdam/topology.js:946:15)
at /home/yehuda_fievel/repos/books/server/node_modules/mongodb/lib/cmap/connection_pool.js:348:13
at handleOperationResult (/home/yehuda_fievel/repos/books/server/node_modules/mongodb/lib/core/sdam/server.js:558:5)
at commandResponseHandler (/home/yehuda_fievel/repos/books/server/node_modules/mongodb/lib/core/wireprotocol/command.js:115:25)
at MessageStream.messageHandler (/home/yehuda_fievel/repos/books/server/node_modules/mongodb/lib/cmap/connection.js:268:11)
at MessageStream.emit (events.js:315:20)
at processIncomingData (/home/yehuda_fievel/repos/books/server/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
at MessageStream._write (/home/yehuda_fievel/repos/books/server/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
at writeOrBuffer (internal/streams/writable.js:358:12)
at MessageStream.Writable.write (internal/streams/writable.js:303:10)
at Socket.ondata (internal/streams/readable.js:719:22)
at Socket.emit (events.js:315:20)
at addChunk (internal/streams/readable.js:309:12)
at readableAddChunk (internal/streams/readable.js:284:9)
at Socket.Readable.push (internal/streams/readable.js:223:10)
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.
>开玩笑
(节点:10487)[MDEP001]弃用警告:“MongoMemoryReplSet.getConnectionString”已弃用,请使用“.getUri”
(使用`node--trace deprecation…`显示警告的创建位置)
信息:应用程序正在监听端口3000!
失败_测试_/routes/users.test.js
用户路由
✕ 应注册新用户(153毫秒)
● 用户路由›应注册新用户
预计200个“正常”,收到400个“错误请求”
21 |术语条件:正确,
22 | })
>23 |预期(200,完成);
| ^
24 | });
25 | });
反对。(_test__/routes/users.test.js:23:14)
----
在Test.Object..Test.\u资产状态(node\u modules/supertest/lib/Test.js:296:12)
在节点_modules/supertest/lib/test.js:80:15
在Test.Object..Test.\u assertFunction(节点\u modules/supertest/lib/Test.js:311:11)
在Test.Object..Test.assert(node_modules/supertest/lib/Test.js:201:21)
在Server.localAssert(node_modules/supertest/lib/test.js:159:12)
测试套件:1个失败,共1个
测试:1次失败,共1次
快照:共0个
时间:1.976秒,估计2秒
运行所有测试套件。
错误:MongoError:此MongoDB部署不支持可重试写入。请将retryWrites=false添加到连接字符串中。
在getmmaperor(/home/yehuda_fievel/repos/books/server/node_modules/mongodb/lib/core/topologies/shared.js:413:18)
在handler(/home/yehuda_fievel/repos/books/server/node_modules/mongodb/lib/core/sdam/topology.js:946:15)
at/home/yehuda_fievel/repos/books/server/node_modules/mongodb/lib/cmap/connection_pool.js:348:13
在handleOperationResult(/home/yehuda_fievel/repos/books/server/node_modules/mongodb/lib/core/sdam/server.js:558:5)
在commandResponseHandler(/home/yehuda_fievel/repos/books/server/node_modules/mongodb/lib/core/wireprotocol/command.js:115:25)
在MessageStream.messageHandler(/home/yehuda_fievel/repos/books/server/node_modules/mongodb/lib/cmap/connection.js:268:11)
在MessageStream.emit(events.js:315:20)
在processIncomingData(/home/yehuda_fievel/repos/books/server/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
在MessageStream中写入(/home/yehuda\u fievel/repos/books/server/node\u modules/mongodb/lib/cmap/message\u stream.js:42:5)
在writeOrBuffer(internal/streams/writable.js:358:12)
在MessageStream.Writable.write(内部/streams/Writable.js:303:10)
在Socket.ondata(internal/streams/readable.js:719:22)
在Socket.emit(events.js:315:20)
在addChunk(internal/streams/readable.js:309:12)
在readableAddChunk(internal/streams/readable.js:284:9)
在Socket.Readable.push(internal/streams/Readable.js:223:10)
测试运行完成后一秒钟,Jest未退出。
这通常意味着有一些异步操作在测试中没有停止。考虑用“-Debug”句柄运行笑话来解决这个问题。
我尝试了他们的建议,将retryWrites=false
添加到Mongoose connect选项中,但没有任何帮助
任何帮助都将不胜感激。谢谢。我刚刚查看了
@shelf/jest mongodb
的github帐户,他们不支持事务的副本集。见下文
我想你没有把应用程序和应用程序分开。听着。检查这个被接受的答案:我试过你说的,但仍然得到同样的错误。我尝试在没有事务的情况下创建一个用户(在提出您的建议之后),结果成功了。这导致我假设jest mongodb包存在无法处理事务的问题。
> jest
(node:10487) [MDEP001] DeprecationWarning: "MongoMemoryReplSet.getConnectionString" is deprecated, use ".getUri"
(Use `node --trace-deprecation ...` to show where the warning was created)
info: App is listening on port 3000!
FAIL __test__/routes/users.test.js
user routes
✕ should register new user (153 ms)
● user routes › should register new user
expected 200 "OK", got 400 "Bad Request"
21 | termsConditions: true,
22 | })
> 23 | .expect(200, done);
| ^
24 | });
25 | });
at Object.<anonymous> (__test__/routes/users.test.js:23:14)
----
at Test.Object.<anonymous>.Test._assertStatus (node_modules/supertest/lib/test.js:296:12)
at node_modules/supertest/lib/test.js:80:15
at Test.Object.<anonymous>.Test._assertFunction (node_modules/supertest/lib/test.js:311:11)
at Test.Object.<anonymous>.Test.assert (node_modules/supertest/lib/test.js:201:21)
at Server.localAssert (node_modules/supertest/lib/test.js:159:12)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 1.976 s, estimated 2 s
Ran all test suites.
error: MongoError: This MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string.
at getMMAPError (/home/yehuda_fievel/repos/books/server/node_modules/mongodb/lib/core/topologies/shared.js:413:18)
at handler (/home/yehuda_fievel/repos/books/server/node_modules/mongodb/lib/core/sdam/topology.js:946:15)
at /home/yehuda_fievel/repos/books/server/node_modules/mongodb/lib/cmap/connection_pool.js:348:13
at handleOperationResult (/home/yehuda_fievel/repos/books/server/node_modules/mongodb/lib/core/sdam/server.js:558:5)
at commandResponseHandler (/home/yehuda_fievel/repos/books/server/node_modules/mongodb/lib/core/wireprotocol/command.js:115:25)
at MessageStream.messageHandler (/home/yehuda_fievel/repos/books/server/node_modules/mongodb/lib/cmap/connection.js:268:11)
at MessageStream.emit (events.js:315:20)
at processIncomingData (/home/yehuda_fievel/repos/books/server/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
at MessageStream._write (/home/yehuda_fievel/repos/books/server/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
at writeOrBuffer (internal/streams/writable.js:358:12)
at MessageStream.Writable.write (internal/streams/writable.js:303:10)
at Socket.ondata (internal/streams/readable.js:719:22)
at Socket.emit (events.js:315:20)
at addChunk (internal/streams/readable.js:309:12)
at readableAddChunk (internal/streams/readable.js:284:9)
at Socket.Readable.push (internal/streams/readable.js:223:10)
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.