mongodb-js / mongodb-prebuilt

Install MongoDB prebuilt package using npm https://npmjs.org/package/mongodb-prebuilt
ISC License
56 stars 50 forks source link

Terminate testing mongodb server #47

Closed IhostVlad closed 6 years ago

IhostVlad commented 6 years ago

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  */
}
IhostVlad commented 6 years ago

Solved in mongo-unit library last version, which is test wrapper for mongo-prebuild