Currently, I am conducting tests on a typescript express-mongoose app using jest, supertest, and mongo-memory-server. Interestingly, all the tests are passing successfully, but an error keeps popping up in every test involving mongo-memory-server. It seems like the root cause of this error is the mongoose.disconnect function, yet I have been unable to resolve it so far.
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 = '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c8bcadbbbc88a5a9a1a4e6aba7a5">[email protected]</a>';
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)