ExpressJS - Jest & Unit Testing
ฝัง
- เผยแพร่เมื่อ 4 ก.ย. 2024
- Support the Channel:
Become a Member: www.youtube.co...
Become a Patreon: / stuyy
Buy me a Coffee: ko-fi.com/anson
Donate on Streamlabs: streamlabs.com...
Donate on PayPal: paypal.me/anso...
Connect with me:
Twitter: / ansonthedev
Discord: / discord
GitHub: github.com/stuyy
Twitch: / stuy
Absolutely the best video I’ve found on testing an API. I plan on going back to the start and following along to really get a grasp on testing. Would be even better if you implement a little E2E testing.
thank you for this tutorial, helped me understand the fundamentals of unit testing
Thanks so much for this tutorial Anson! Like Adam said, This is the best explanatory video I've ever seen on the internet on JEST testing! Love from Sri Lanka!
Another awesome video, best explanation on testing I found so far on YT 🤘🤘🤘
This is the tutorial that i was needed , thanks
👍🙂 Thanks that will help a lot.
what theme of vs did you use in this video
This is integration testing not unit because unit testing haven't side effect
Thanks ❤
What vsc theme are you using?
Thanks for the sharing,
Im just confusing why the object {id: 1, email: "..."} is wrapped in parenthesis ? Since I read the documentation in jest
mockFn.mockImplementationOnce(fn) // where fn could be () => {// the code}
So hope you or everyone could share the reason
Here is the code in your video:
User.mockImplementationOnce(() => ({id: 1, email: "..."} ))
Without the parenthesis, everything inside the curly braces, { }, would need to be statements. The parenthesis wrapping the curly braces indicates the return value is an object. It's a shorthand for doing this: () => { return { ... } }
Hi,
in case we use next() to forward to other middleware (e.g. errorHandler function) , how could we mock and verify the next function to be called with correct parameter ?
When you call next() inside the middleware function, remember that "next" function is a parameter passed to the middleware. So you would pass in a mocked function, e.g:
const mockedNext = jest.fn();
Then you would assert that mockedNext was called, e.g: expect(mockedNext).toHaveBeenCalled() or expect(mockedNext).toHaveBeenCalledWith(...);
good video thnaks
Hi there, how would you mock the User model in typescript ?
It's the same exact way, only difference is you will have TypeScript complaining about fields missing. If your user model is simple, you can just fill out all the fields. If it is complex and has a lot relations which end up with an object full of aggregated data (fields that map to objects), then you can either manually mock those objects too, or just mock the fields you need for the mock User and cast the object to a User manually to avoid TypeScript errors.
@@ansonthedev Thanks a lot Anson. Keep up the good work :). I've been following your NestJs playlist and it helped me clear the coding round of one of the interviews I appeared for.