MongoMemoryServer ;未处理的错误
Posted
技术标签:
【中文标题】MongoMemoryServer ;未处理的错误【英文标题】:MongoMemoryServer ; Unhandled Errors 【发布时间】:2019-09-17 07:06:19 【问题描述】:我正在使用 jest、supertest 和 mongo-memory-server 测试 typescript express-mongoose 应用程序。所有测试都通过了,但是我在使用 mongo-memory-server 的每个测试中都遇到了这个错误。 我发现 mongoose.disconnect 导致它,但我仍然无法修复它
import MongoMemoryServer from 'mongodb-memory-server';
let mongoServer: any;
jasmine.DEFAULT_TIMEOUT_INTERVAL = 600000;
describe('createUser', (): void =>
let mongoServer: any;
const opts = ;
beforeAll(async () =>
mongoServer = new MongoMemoryServer();
const mongoUri = await mongoServer.getConnectionString();
await mongoose.connect(mongoUri, opts, err => );
);
afterAll(async () =>
mongoose.disconnect();
await mongoServer.stop();
);
it('creating user', async (): Promise<void> =>
const email = 'test@mail.com';
const username = 'testUser';
const password = 'testPassword';
const userId = await createUser(email, username, password);
expect(userId).toBeTruthy();
expect(typeof userId).toMatch('string');
);
);
console.error node_modules/jest-jasmine2/build/jasmine/Env.js:289
Unhandled error
console.error node_modules/jest-jasmine2/build/jasmine/Env.js:290
Error [ERR_UNHANDLED_ERROR]: Unhandled error. (MongoNetworkError: read ECONNRESET)
at Connection.emit (events.js:178:17)
at Socket.<anonymous> (C:\Users\PC\Desktop\typescript-mern-budget-app\server\node_modules\mongodb-core\lib\connection\connection.js:321:10)
at Object.onceWrapper (events.js:277:13)
at Socket.emit (events.js:189:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
【问题讨论】:
【参考方案1】:afterAll(async () =>
await mongo.stop();
await mongoose.connection.close();
);
问题是你没有在等待这两个函数。
【讨论】:
以上是关于MongoMemoryServer ;未处理的错误的主要内容,如果未能解决你的问题,请参考以下文章