I've been working on creating an application using Node.js and Mongoose where all data stored in the cloud MongoDB Atlas. My goal was to test API e...
For further actions, you may consider blocking this person and/or reporting abuse
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 insteadconst mongo = await MongoMemoryServer.create();
. But I didn't succeed to adapt this to your code since that make me runawait
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?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:Ok! thanks a lot for your answer
Simply move the create() into the connect method. Thanks! (answer from this article : dev.to/paulasantamaria/testing-nod...)
Hello, thanks for your guide.
How do i test something that requires a JWK, since i first need to create something in the DB for it to provide a token and then use that token for the update request
Hi, thank you :)
Its a good question! Please, check my updated Github repo where I added a simple authentication using JWT and some tests.
My solution is based on this issue
You can create a user and generate a token in each test or just populate it through
beforeAll
:Hi great article. My project is also very similar. How can I learn how to write unit tests using mongo in memory server. Its my first time writing unit tests as a developer.
Thank you!
It's hard for me to give you some concrete advice, I'm still learning how to write tests :) For my small project though, I've tried to write tests for each API endpoint using superset, Jest and mongodb-memory-server official documentations along with some examples.
Created a dev.to account just to heart this article.
Thanks a lot! 😉
Hey thx for the article. In the function clear from the db file in test. I think deleMany should have an empty object as parameter
Hi, thank you!
I've tested it with / without empty object and in both cases, all data was properly deleted after each test, so everything should work fine.
Anyway, I'll add it as in the MongoDB documentation. Thx :)
I have followed your post but still getting errors on my project.
Any help?
Do you have any similar repo with examples for nestJs testing using mongoMemoryServer?
Sorry, I don't have examples for nestJs 😔