UPDATED 2020-09-14
I've encountered an issue with a test case I wrote. While the testcase passes, it raises a complaint about improper teardown and an open connection. Can anyone help identify the problem:
Approach to Solving the Issue - Memory Leak
import { Connection, createConnection } from 'mongoose';
import __MONGO_URI__ from './__MONGO_URI__';
let conn: Connection | null = null;
const getConnection: (MONGO_DB_NAME: string) => Promise<Connection> = async MONGO_DB_NAME => {
if (conn == null) {
conn = await createConnection(__MONGO_URI__, {
dbName: MONGO_DB_NAME,
bufferCommands: false,
bufferMaxEntries: 0,
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
});
}
return conn;
};
const MONGO_DB_NAME = 'mongo-test';
let db: Connection;
describe('mongo - connection test to ensure setup teardown', () => {
beforeAll(async done => {
db = await getConnection(MONGO_DB_NAME);
done();
});
afterAll(async done => {
if (conn) {
await db.dropDatabase();
await conn.close();
}
done();
});
it('true = true', () => {
expect(true).toBe(true);
});
});
Error:
A worker process has failed to exit gracefully and has been force exited. This is likely caused by tests leaking due to improper teardown. Try running with --runInBand --detectOpenHandles to find leaks.
Simplifying the code doesn't solve the issue:
Another Attempt - Memory Leak
import { Connection, createConnection } from 'mongoose';
import __MONGO_URI__ from './__MONGO_URI__';
let conn: Connection | null = null;
const getConnection: (MONGO_DB_NAME: string) => Promise<Connection> = async MONGO_DB_NAME => {
if (conn == null) {
conn = await createConnection(__MONGO_URI__, {
dbName: MONGO_DB_NAME,
bufferCommands: false,
bufferMaxEntries: 0,
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
});
}
return conn;
};
const MONGO_DB_NAME = 'mongo-test';
let db: Connection;
describe('mongo - connection test to ensure setup teardown', () => {
beforeAll(async () => {
db = await getConnection(MONGO_DB_NAME);
console.log('db = ', db);
});
it('true = true', () => {
expect(true).toBe(true);
});
});
The problem persists even when trying it another way:
Alternative Approach - Memory Leak
import { Connection, createConnection } from 'mongoose';
import __MONGO_URI__ from './__MONGO_URI__';
let conn: Connection | null = null;
const getConnection: (MONGO_DB_NAME: string) => Promise<Connection | null> = MONGO_DB_NAME =>
new Promise(resolve => {
if (conn == null) {
conn = createConnection(__MONGO_URI__, {
dbName: MONGO_DB_NAME,
bufferCommands: false,
bufferMaxEntries: 0,
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
});
conn.on('connected', () => {
console.log('connection established');
resolve(conn);
});
}
resolve(conn);
});
const MONGO_DB_NAME = 'mongo-test';
let db: Connection;
describe('mongo - connection test to ensure setup teardown', () => {
beforeAll(async () => {
db = await getConnection(MONGO_DB_NAME);
console.log('db = ', db);
});
it('true = true', () => {
expect(true).toBe(true);
});
});
This new approach also presents the same issue:
Finding a Solution - Memory Leak
import mongoose from 'mongoose';
import __MONGO_URI__ from './__MONGO_URI__';
let conn: typeof mongoose;
const getConnection: (MONGO_DB_NAME: string) => Promise<typeof mongoose> = async MONGO_DB_NAME => {
if (!conn) {
conn = await mongoose.connect(__MONGO_URI__, {
dbName: MONGO_DB_NAME,
// bufferCommands: false,
// bufferMaxEntries: 0,
// useNewUrlParser: true,
// useUnifiedTopology: true,
// useCreateIndex: true
});
}
return conn;
};
const MONGO_DB_NAME = 'mongo-test';
let db: typeof mongoose;
describe('mongo - connection test to ensure setup teardown', () => {
beforeAll(async () => {
db = await getConnection(MONGO_DB_NAME);
console.log('db = ', db);
});
it('true = true', () => {
expect(true).toBe(true);
});
});