DEV Community

Discussion on: Setup in-memory database for testing Node.js and Mongoose

Collapse
 
tperrinweembi profile image
tperrin

Thanks for this awsome article. But since mongodb-memory-server version 7.0.0 we can't use anymore const mongoServer = new MongoMemoryServer(); but must use instead const mongo = await MongoMemoryServer.create(); . But I didn't succeed to adapt this to your code since that make me run await at file top level so I got the error "SyntaxError: await is only valid in async function". How would do you migrate your code to version 7?

Collapse
 
tperrinweembi profile image
tperrin

Simply move the create() into the connect method. Thanks! (answer from this article : dev.to/paulasantamaria/testing-nod...)

Collapse
 
ryuuto829 profile image
Dmytro Rykhlyk

Thank you for your comment!
I've updated article using the latest @7.2.1 version of mongodb-memory-server.
Basically, I define a new instance of "MongoMemoryServer" in the connect async function to automatically start server:

/tests/db.js

- const mongoServer = new MongoMemoryServer();
+ let mongoServer;

const connect = async () => {
  await mongoose.disconnect();
+ mongoServer = await MongoMemoryServer.create();

// ...
};
Enter fullscreen mode Exit fullscreen mode
Collapse
 
tperrinweembi profile image
tperrin

Ok! thanks a lot for your answer