DEV Community

Discussion on: Unit testing NestJS with mongo in memory...

Collapse
 
deejoe79 profile image
deejoe79

Thank you for the article, this was very helpful. I also got the "Jest did not exit one second after the test run has completed. This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with --detectOpenHandles to troubleshoot this issue."

The advice was not practical for me because in VSCode I often run 1-2 tests only (using the extension: Jest Runner).

I share what worked for me. Go to the MongooseTestModule.ts (typescript please :)
add:

import mongoose from 'mongoose';
Enter fullscreen mode Exit fullscreen mode
export const closeInMongodConnection = async () => {
    await mongoose.disconnect();
    if (mongod) await mongod.stop();
};
Enter fullscreen mode Exit fullscreen mode
Collapse
 
bassochette profile image
Julien Prugne

Thanks for you help ❤
I'll try to update the post and the sample code repo on github.