Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mocha 4+ doesn't exit when using mockgoose in tests #71

Closed
deployable opened this issue Feb 1, 2018 · 5 comments
Closed

mocha 4+ doesn't exit when using mockgoose in tests #71

deployable opened this issue Feb 1, 2018 · 5 comments

Comments

@deployable
Copy link
Contributor

deployable commented Feb 1, 2018

Mocha 4+ changed behaviour to "no longer force the process to exit once all tests complete".

Mockgoose runs into this behaviour change due to the child process for mongod stopping mocha from exiting.

@deployable deployable changed the title mocha 5 doesn't exit when using mockgoose in tests mocha 4+ doesn't exit when using mockgoose in tests Feb 1, 2018
@deployable
Copy link
Contributor Author

I've submitted a PR to mongodb-prebuilt to add the helper functions to enable this: mongodb-js/mongodb-prebuilt#49

deployable added a commit to deployable/mockgodb that referenced this issue Feb 2, 2018
@bbruneau
Copy link

+1, any word on this?

@pgerlach
Copy link

pgerlach commented Mar 10, 2018

Until #72 is merged, I use this code in my after() function in mocha 4+.

  after(async function() {
    await mockgoose.helper.reset();
    await mongoose.disconnect();
    mockgoose.mongodHelper.mongoBin.childProcess.kill('SIGTERM');
  });

@j3ko
Copy link

j3ko commented Mar 24, 2018

adding the above code works when there is one test file in mocha 5, but when there are 2+ test files (ie. multiple after's) it still seems to hang. Is there a better workaround for that?

@gsikorski
Copy link

@j3ko Something like this works for me:

// mongomock.js
const mongoose = require('mongoose'),
    Mockgoose = require('mockgoose').Mockgoose,
    mockgoose = new Mockgoose(mongoose);

module.exports = {
    setup: async function setup() {
        await mockgoose.prepareStorage();
        await mongoose.connect('mongodb://test');
    },

    teardown: async function teardown() {
        await mockgoose.helper.reset();
        await mongoose.disconnect();
        let retval = new Promise(resolve => {
            mockgoose.mongodHelper.mongoBin.childProcess.on('exit', resolve);
        });
        mockgoose.mongodHelper.mongoBin.childProcess.kill('SIGTERM');
        return retval;
    }
};

Then in your tests you can do:

const mongomock = require('mongomock');

    before(async () => {
        await mongomock.setup();
    });
    ...

    after(async () => {
        await mongomock.teardown();
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants