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
  • วิทยาศาสตร์และเทคโนโลยี

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

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

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

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

    I'm struggling with what should be tested in REST api and your vids really help me understand the idea of it neatly, thanks.

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

    Great Playlist (y) looking forward to the next episode !

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

    Solid work, i watched and loved all of your videos regarding "testing" subject, thank you

  • @--------------------.
    @--------------------. 2 ปีที่แล้ว

    Amazing, love that content and methodical explanation!

  • @happy..1907
    @happy..1907 3 ปีที่แล้ว

    Nicely explained. Thanks for creating this.

  • @sangitsigdel4971
    @sangitsigdel4971 3 ปีที่แล้ว

    Awesome video Sam. Keep Going... Love your teaching style..

  • @anasaljawa858
    @anasaljawa858 3 ปีที่แล้ว

    Great Content, I love how make it easier to understand

  • @carlosdelgado5632
    @carlosdelgado5632 3 ปีที่แล้ว

    Nice how everything was explained and using some tdd for test 💯

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

    Great video Sam, keep it up!

  • @eduardocarvalho4232
    @eduardocarvalho4232 3 ปีที่แล้ว

    Thanks men, very good content!

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

    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!!

    • @AwesomeEnterpriseInc
      @AwesomeEnterpriseInc 3 ปีที่แล้ว

      Also, is that your campus??

    • @SamMeechWard
      @SamMeechWard  3 ปีที่แล้ว

      Yes it is. Completely empty right now so not bad for filming 🤗

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

    best jest tutorial on youtube

  • @Opim97
    @Opim97 3 ปีที่แล้ว

    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?

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

    Awesome, great content!

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

    This is awesome! Thank you so much!

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

    These videos should be more popular

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

    For a moment I thought they title of this implied that you were gonna make fun of a database

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

    great video, helped me a lot :)

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

    Very helpful!

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

    I love you man. You are awesome

  • @youssefmohamed1618
    @youssefmohamed1618 ปีที่แล้ว +3

    Great series.. thank you really.
    Can you please make the remaining video you talked about ? 👀

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

    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

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

    Hallo Sam, Thank you for TDD concept. Can you make error handling in Nodejs in your way

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

    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.

  • @blameItleaveit
    @blameItleaveit 3 ปีที่แล้ว

    Great video 👍🏼👍🏼,How we can test if real database throw an error like email is already exist ?

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

    Quoting previous comment: great playlists

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

    great stuff

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

    how to send the jwt token while login (i am talking about cookies not bearer token ) i stuck here

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

    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

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

    Hi guy, very helpfull but I don't understand how I can mock my db with a separate route/controller/dao/app architecture

    • @JohnWilson-mk2jp
      @JohnWilson-mk2jp 2 ปีที่แล้ว +1

      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.

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

      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.

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

    Can anyone tell me how to mock transactions?

  • @eromengreek
    @eromengreek 4 หลายเดือนก่อน +1

    You are actually testing the API endpoint, not the actual function that creates the user in the database.

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

    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

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

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

  • @DR-ee4wv
    @DR-ee4wv 2 ปีที่แล้ว

    damn good video. I wish I could donate

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

    please upload the next episode

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

    Sam, 🥺 thank you

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

    Great video!!!
    Can I get the song's name pls?

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

      Toby Tranter - Memories Don't Lie

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

    great content my guy but you rush so much that its hard to keep up

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

    Good instruction, but could do without the music -- or at least, turn the level down so it's not louder than the speech.

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

      I agree, I don't do that anymore

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

    hello sir ...