We are using mongodb-prebuilt package for unit/integration testing with some npm packages, which performs interaction with mongodb database.
Common use case is following: launch testing mongodb server, perform test suite execution and then terminate server. First and second items are done great, but third one now causes some problems.
If mongodb-prebuilt-started server had not been terminated, test runner will hang forever. If try to terminate server by testConnection.command({ shutdown: 1 }) command, then unhandledRejection fires, which refers on closed connection - which of course had been forcefully closed by stopping the server.
Which is right way to dispose mongodb-prebuilt at afterAll test section? Test engine is jest, but it does not matter strongly.
Example code here:
import { MongodHelper } from 'mongodb-prebuilt';
import { MongoClient } from 'mongodb';
describe('Test suite', () => {
let testConnection;
beforeAll(async () => {
const mongodHelper = new MongodHelper(['--port', "27018"]);
await mongodHelper.run();
testConnection = await MongoClient.connect(`mongodb://localhost:27018/admin`);
});
afterAll(async () => {
await testConnection.dropDatabase();
await testConnection.command({ shutdown: 1 });
await testConnection.close();
testConnection = null;
});
/* Here follows some test suite that uses testConnection */
}
We are using
mongodb-prebuilt
package for unit/integration testing with some npm packages, which performs interaction withmongodb
database.Common use case is following: launch testing mongodb server, perform test suite execution and then terminate server. First and second items are done great, but third one now causes some problems.
If
mongodb-prebuilt
-started server had not been terminated, test runner will hang forever. If try to terminate server by testConnection.command({ shutdown: 1 }) command, then unhandledRejection fires, which refers on closed connection - which of course had been forcefully closed by stopping the server.Which is right way to dispose
mongodb-prebuilt
atafterAll
test section? Test engine isjest
, but it does not matter strongly. Example code here: