Mocking a Database in Node with Jest
ฝัง
- เผยแพร่เมื่อ 5 เม.ย. 2021
- Learn how to use jest mock functions to mock a database in an HTTP server.
🔗 Previous Videos:
Introduction to Writing Automated Tests With Jest: • Unit Testing in Javasc...
Introduction to TDD in JavaScript: • TDD in JavaScript | Te...
Testing Node Server with Jest and Supertest: • Testing Node Server wi...
Dependency Injection: • Dependency Injection i...
🔗 Text version:
sammeechward.com/mocking-a-da...
🔗 Code:
github.com/Sam-Meech-Ward/exp...
🔗 Jest Mock Functions:
jestjs.io/docs/mock-functions
🔗 Moar Links
My Website: www.sammeechward.com
Instagram: / meech_ward
Github: github.com/orgs/Sam-Meech-Ward
TikTok: / meech.s.ward - วิทยาศาสตร์และเทคโนโลยี
@SamMeechWard, your videos are great. Thank you so much! I'd waiting to see the next video in this series that you talked about : testing databases in Node. I'm sure you have more than enough project ideas, but that's one I'd love to see!
I'm struggling with what should be tested in REST api and your vids really help me understand the idea of it neatly, thanks.
Great Playlist (y) looking forward to the next episode !
Solid work, i watched and loved all of your videos regarding "testing" subject, thank you
Amazing, love that content and methodical explanation!
Nicely explained. Thanks for creating this.
Awesome video Sam. Keep Going... Love your teaching style..
Great Content, I love how make it easier to understand
Nice how everything was explained and using some tdd for test 💯
Great video Sam, keep it up!
Thanks men, very good content!
I love Jest mock functions! Reminds me of `action` in Storybook. Saves a ton of time and trouble for the dev.
Also, great video Sam!!
Also, is that your campus??
Yes it is. Completely empty right now so not bad for filming 🤗
best jest tutorial on youtube
Great content! Really appreciate the effort put into this. I had a question in regards to what the console displays when running the tests, might be something I have to add in a config file.
When I'm running my tests, the console outputs "PASS folder/myTestFile.js", but not the describe and test description. Any idea on how to show this output?
Awesome, great content!
This is awesome! Thank you so much!
These videos should be more popular
For a moment I thought they title of this implied that you were gonna make fun of a database
great video, helped me a lot :)
Very helpful!
I love you man. You are awesome
Great series.. thank you really.
Can you please make the remaining video you talked about ? 👀
Agreed
Great content. One question though, the object database being passed in a jest instance for unit testing. How do we ensure that, for normal execution, the database object points to the database ? Without changing the business logic code or adding if blocks
Hallo Sam, Thank you for TDD concept. Can you make error handling in Nodejs in your way
This is indeed useful to test an API independently from the database, but what if I want to actually test my database functions without modifying the database the real application connects to? From the title I thought that the video was going to talk about this. I can think of some ways to do it, but it would be good if you'd shown an example of how this is done in a proper way.
Great video 👍🏼👍🏼,How we can test if real database throw an error like email is already exist ?
Quoting previous comment: great playlists
great stuff
how to send the jwt token while login (i am talking about cookies not bearer token ) i stuck here
Is it wrong to test the code actually accesses and saves on the database? Like what if I have a flag for testing and it connects to a test database
Hi guy, very helpfull but I don't understand how I can mock my db with a separate route/controller/dao/app architecture
Exactly, this seems to only work if all logic; routes, controllers, services/business logic are in one file and Db calls all happen directly inside the createApp function. Would be useful to know how to mock the data access layer for a layered architecture.
bit of a late reply but if you have separate router files, turn them into static classes that get exported. that way, app.js can send your database into the classes via a static function in each class to get the router and set the database, the classes save them as static properties, and now you got routers in separate files all using the database passed into app.js. Hope that helps.
Can anyone tell me how to mock transactions?
You are actually testing the API endpoint, not the actual function that creates the user in the database.
How does Jest.fn() even know what queries to call when the database.js was not even connected or when not a single database query is written??? 5:09
It isn't connected to the database. Jest.fn() can 'mock' a database call. For example, if your database query returned `{username: "chilly21"}` when a request was sent to it with `{userId: 1}`, then you could simulate this with Jest.fn(({userId})=> return {username:"chilly21"}).
The idea is that you completely remove the connection to your database so that it speeds up your testing and so that your database is preserved. The Jest.fn() function allows you to simulate a database call and also gives you lots of other features, such as allowing you to test number of times called (eg `expect(mockCallback.mock.calls.length).toBe(2);`).
damn good video. I wish I could donate
please upload the next episode
Sam, 🥺 thank you
You’re welcome 😊
Great video!!!
Can I get the song's name pls?
Toby Tranter - Memories Don't Lie
great content my guy but you rush so much that its hard to keep up
Good instruction, but could do without the music -- or at least, turn the level down so it's not louder than the speech.
I agree, I don't do that anymore
hello sir ...