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

ความคิดเห็น • 19

  • @adamsackfield589
    @adamsackfield589 2 ปีที่แล้ว +4

    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.

  • @UdayKiranK-t9q
    @UdayKiranK-t9q หลายเดือนก่อน

    thank you for this tutorial, helped me understand the fundamentals of unit testing

  • @hashanhemachandra4832
    @hashanhemachandra4832 2 ปีที่แล้ว

    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!

  • @sarcastichistoryoftheworld
    @sarcastichistoryoftheworld ปีที่แล้ว

    Another awesome video, best explanation on testing I found so far on YT 🤘🤘🤘

  • @jDelestal94
    @jDelestal94 9 หลายเดือนก่อน

    This is the tutorial that i was needed , thanks

  • @lovelesslol
    @lovelesslol 2 ปีที่แล้ว +1

    👍🙂 Thanks that will help a lot.

  • @pandagamedev1177
    @pandagamedev1177 หลายเดือนก่อน

    what theme of vs did you use in this video

  • @rady6546
    @rady6546 ปีที่แล้ว +2

    This is integration testing not unit because unit testing haven't side effect

  • @ericsiddiq7634
    @ericsiddiq7634 11 หลายเดือนก่อน

    Thanks ❤

  • @marshelaillon
    @marshelaillon 11 หลายเดือนก่อน +1

    What vsc theme are you using?

  • @learningforever2531
    @learningforever2531 ปีที่แล้ว

    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: "..."} ))

    • @ansonthedev
      @ansonthedev  ปีที่แล้ว +1

      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 { ... } }

  • @learningforever2531
    @learningforever2531 ปีที่แล้ว

    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 ?

    • @ansonthedev
      @ansonthedev  ปีที่แล้ว +5

      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(...);

  • @raphaelspitz3682
    @raphaelspitz3682 10 หลายเดือนก่อน

    good video thnaks

  • @AdnanDev-su5no
    @AdnanDev-su5no 5 หลายเดือนก่อน

    Hi there, how would you mock the User model in typescript ?

    • @ansonthedev
      @ansonthedev  5 หลายเดือนก่อน +1

      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.

    • @AdnanDev-su5no
      @AdnanDev-su5no 4 หลายเดือนก่อน

      @@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.