Testing a REST API in Node JS with Express using Mocha and Chai

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 พ.ย. 2019
  • In this video we are going to test our REST API in Node JS with Express using Mocha and Chai.
    ➥ Buy Me a Coffee: www.buymeacoffee.com/pragmatic
    Mocha is a testing framework for JavaScript that runs on Node.js and the browser. You can use any assertion library, in this case we are going to use Chai to validate the http requests we make against our ExpressJS Restful API.
    Assertion with Chai provides natural language assertions, expressive and readable style. Chai provides three assertion styles: Should, Expect and Assert.
    Stay tuned for more!
    ➥ If I have helped you in any way please consider becoming a patron for just $1/month: / pragmatic
    ➥➥ Learn even more! ➥➥
    NestJS Crash Course ➥ bit.ly/2QevL9X
    TypeScript Crash Course ➥ bit.ly/37lJEsH
    Nuxt.js Crash Course ➥ bit.ly/37kQiPG
    Vuetify.js Crash Course ➥ bit.ly/2sArggU
    Vue.js Crash Course ➥ bit.ly/37mCaFM
    Automate.io Crash Course ➥ bit.ly/2SRGlph
    Zapier Crash Course ➥ bit.ly/37q1BGt
    SEO Crash Course ➥ bit.ly/2QbIhqy
    Wordpress Crash Course ➥ bit.ly/2ZBZRr5
    #Express #Mocha #Chai

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

  • @PragmaticReviews
    @PragmaticReviews  4 ปีที่แล้ว +8

    ➥ Here is the source code for this video: gitlab.com/pragmaticreviews/node-mocha-chai
    ➥ Get a Certification as a Python Data Scientist or Data Engineer: pragmaticreviews.com/datacamp
    ➥ Get a Free Golang Cheat Sheet here: pragmaticreviews.com
    Thanks!

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

    I got a job following your lessons in doing the assessment task that was given to me. Thank you so much

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

    Exactly what I needed. The crux of mocha and chai. Thank you so much sir.

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

    thank you! nice tutorial! easy to understand, keep up the great work!

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

    This is very clear and to the point mocha tutorial i ever see, Thanks a lot @pragrmatic review

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

    Thanks! nice and easy to follow

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

    Great tutorial mate !
    Thanks

  • @amitshukla2268
    @amitshukla2268 9 หลายเดือนก่อน +2

    Great tutorial, Very Easy to understand, thanks man :)

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

    Thanks for the video, very well explained.

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

    Thank you so much for the help!

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

    Does anyone know how can I like a video 5 times?
    It REALLY IS THAT GOOD!
    The way you explain, oh my god, you're a legend bro.
    Thank you so much for this. Srsly, I wish I could Like it more than once.
    Needless to say, I just subscribed.

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

    Awesome man!

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

    Me vino genial. Muy buen canal, muchas gracias!

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

    Thanks!! Super useful :)

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

    thank you ..that's really helpful

  • @WojciechPietrzak-vj2fr
    @WojciechPietrzak-vj2fr หลายเดือนก่อน

    Well Done !!!

  • @RKrol-xx1js
    @RKrol-xx1js 2 ปีที่แล้ว +1

    Thank you, i t was very helpful

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

    Hi Sir, Thanks for the video, I was looking for the tutorial of node.js based API unit testing using mocha & chai, however I got but not that much easy way you have done it. Thanks to save my life

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

      Awesome. Thank you!

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

      By the way, here is the source code for this video: gitlab.com/pragmaticreviews/node-mocha-chai

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

    Awesome tutorial

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

    Hi Can you please tell how to generate code coverage report?

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

    Hi. when fetching the data from real mongodb database, we use Promises, async, await, so it function is not accepting async await or its rejecting done cb called inside then block. what would be the solution for this?

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

    thanks for sharing

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

    THANKS BRO

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

    good job! but how can i stop running mocha test ???

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

    I have an honest question. If it is so easy to test API calls using JS, why do we need tools like Postman? What are the advantages of such a a tool?
    Thanks for this excellent video.

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

    Hi.., can u explain that how to use assert using API

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

    if we have used MongoDB as a Database, then will these steps will work, or for the Database, we have to add more conditions to get work.

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

    Everything worked fine, thank you. A question, how would you run tests for PUT or DELETE methods for random generated IDs?

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

      Before running the test you need to create one element, then you get the first element (you need a "findAll" endpoint) and finally you run the test.

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

      If you want to delete the Id then you need to use the get api to fetch that id and use delete API and for each the ids and and store it in variable and delete

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

    WHERE I CAN GET THE RESTFULL API
    CODE THAT YOU ARE USING?

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

    You said in introducing that will use a beforeEach and another method but on video, I don't get it. Thanks

    • @PragmaticReviews
      @PragmaticReviews  4 ปีที่แล้ว

      We can use before o beforeEach to setup the test (e.g. initializing the database) and after or afterEach (e.g. to close the database connection). Thanks for watching!

  • @MuhammedShamnad-rj1ry
    @MuhammedShamnad-rj1ry ปีที่แล้ว +1

    This will create some test data in the database right. We should do a mock instead of calling actual API

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

    Hi, this is great thing by you. I have a question that, how we can do authentication of APIs?, using jwt for the running api collection.

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

      Hey, I've just found this great tutorial buddy.works/tutorials/unit-testing-jwt-secured-node-and-express-restful-api-with-chai-and-mocha Thanks!

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

      Thanks a lot

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

      Hi, stuck once again that how to use this for token authentication??

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

    can't access your site

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

    Hi Sir, Thanks for the video. I wanna ask something, in your tutorial when the id is not in the response body and must be retrieved from the database. how do we do it?. Thank you

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

      You have to map the id field with Mongoose for example so you can retrieve it. Thanks!

    • @januarypanjaitan3307
      @januarypanjaitan3307 4 ปีที่แล้ว

      @@PragmaticReviews I am already get the id, so how I do to put the id to const of mocha?

    • @PragmaticReviews
      @PragmaticReviews  4 ปีที่แล้ว

      You can do something like: response.body.should.have.property('id').eq(1);

    • @januarypanjaitan3307
      @januarypanjaitan3307 4 ปีที่แล้ว

      @@PragmaticReviews No No, I mean that how I do change the value when i get the is from database to const id = null

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

      @@januarypanjaitan3307 That sounds like a problem with either the code creating the objects and assigning their ID, the code that's presenting that as JSON, or the code on the front-end that's interpreting that JSON

  • @PragmaticReviews
    @PragmaticReviews  4 ปีที่แล้ว

    ➥➥ Learn even more! ➥➥
    Golang Crash Course ➥ bit.ly/39ZH8dy
    Vue.js Crash Course ➥ bit.ly/37mCaFM
    Vuetify.js Crash Course ➥ bit.ly/2sArggU
    Nuxt.js Crash Course ➥ bit.ly/37kQiPG
    TypeScript Crash Course ➥ bit.ly/37lJEsH
    NestJS Crash Course ➥ bit.ly/2QevL9X
    Automate.io Crash Course ➥ bit.ly/2SRGlph
    Zapier Crash Course ➥ bit.ly/37q1BGt
    SEO Crash Course ➥ bit.ly/2QbIhqy
    Wordpress Crash Course ➥ bit.ly/2ZBZRr5

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

    Thank You for the video. All websites currently use token and APIs cannot be used without that. How to request Api with bearer token?

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

      Hi , you can set token using this way by assign it into a variable,
      it('it should fetch All Historical Questions', (done) => {
      chai.request(server)
      .get('/incard/v1/user/questions')
      .set('token', token)
      .end((_err, res) => {
      res.should.have.status(200);
      res.body.should.be.a('array');
      done();
      });
      });

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

      @@SAHILBHARTIIPU Thank You very much for the response :) One more question: How to use the token taken from the endpoint response in other requests?
      Example: there is an Authentication endpoint which response with token. That token always needs to be sent in the Headers for the other endpoints. How to keep the token given from Authentication endpoint in the variable before running other tests and make it globally available in other suites/test cases in order to use that variabe for the rest requests?

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

    TypeError: Joi.validate is not a function
    its showing this in all of my methods except get , how i can resolve it

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

    Hi, POST, PATCH, GET is ok but for the DELETE test, we cannot know exactly the id that we have to delete. I want to get the id from the object that I posted before with the POST test. How could I assign it to the variable outside the test and call it? Because if I dockerize the program, I will not know exactly the ID of the object that I want to delete. Hope to know your answer

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

      Hi, if you create a new record by doing a post call then there will be a Id for that record will get created, so you call a get call where you look for an id and store it in variable var newId = response.body.newid;
      Then pass this newid in the delete call ex: api.delete(“endpontUrl/‘+newid+’”)

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

    Hello sir iam also running the integration test on crud api which is also built with express js framework
    So my question is whenever I ran the test for any task it is throwing me the timeout error
    Can u please help me😭😭😭

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

    great,please send me the link to this rest API development totorial

    • @PragmaticReviews
      @PragmaticReviews  4 ปีที่แล้ว

      Here you go gitlab.com/pragmaticreviews/node-mocha-chai

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

    hello sir , do you have the full course on testing rest api with mocha and chai.

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

      Here is the source code: gitlab.com/pragmaticreviews/node-mocha-chai Thanks!

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

      @@PragmaticReviews do you have further videos or full course on this ?

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

      @@mdfaisalhossain9317 Hi sorry, I thought you were asking about the code, my mistake. I don't have any other course on Mocha and Chai, this is the only video. I do have a video on Vue.js testing using Vue Test Utils and Jest here: th-cam.com/video/TZj60NV70QA/w-d-xo.html and another one for testing automation with Cypress here: th-cam.com/video/_f61LfPHyAo/w-d-xo.html

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

    good

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

    why should we use mocha and chai if my postman show correct output?

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

      If you want to automate your regression tests by doing continuous integration, this is a good option. Also, you can run your tests in the background and get a report afterwards. Postman is great but you have to be there.

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

    Hey, I do not want to watch building API in node js which I already know, can you provide me with the link from where I can clone it to use as a base for this tutorial.

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

      Node API here, thank you for watching man: gitlab.com/pragmaticreviews/node-tasks-api

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

      @@PragmaticReviews Thank you so much for responding quickly. You are one of the active tech youtube.

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

    Very nice Tutorial! Thanks for that. Keep in mind hearing your typing all the Time is a nightmare to follow your Tutorial.

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

    Good Video.. but you never test exactly length of the response from the server, otherwise if there will be a small change (like if some post a new user) the test will failed.. in fact if you do that it's force you all the time to go to the DB and count how much users the server will get.. So don't test to specific or to general.

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

    Is your name is Alex??

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

    How we can mock the DB with REST API call, actually I wanted to cover my whole code in code coverage report and don't want to save anything in DB how can we achieve this?

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

      API testing is integration testing because you test the endpoints of an api. Your tests are outside the API. You are talking about unit tests where you mock the database but then you shouldn't call the API endpoints. You should call methods inside the API project and you can do that just inside the project itself because those methods are not publicly accessible by outside tests.

  • @njoyproductionz
    @njoyproductionz 4 ปีที่แล้ว

    Why trying to break that keyboard? :D

    • @PragmaticReviews
      @PragmaticReviews  4 ปีที่แล้ว

      I need a new keyboard now! :-)

    • @njoyproductionz
      @njoyproductionz 4 ปีที่แล้ว

      @@PragmaticReviews Ahahahah :) Nice tutorial though. Thank you for your time and effort!