Node js api testing with mocha and chai || Satyam sinha

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys so uh in this video i'm going to show you that how can you use booka for testing your apis in nurtures uh here i am going to target specifically node.js and i'm not going to go deep into mongodb for showing your api i am just using some kind of arrow to store data and all such stuff okay and we are also going to use shy uh if you don't know chai we use for exerting kind of a thing for testing your apis and all those things uh don't worry i'm going to refer your documentation on all such issues from where you can get all of this and we are going to read documentation together okay so uh so first head on to your browser and just type here mocha sorry maha matcha whatever it is and just go there and here you can see here mockajs.org and just click here here you can see they uh have a lot of sponsors we have and backers we have okay uh so installation is pretty simple you have to just use npm installed save dave mocha or okay so so before creating this we have to just initialize our application uh so that we can use it okay so head on to your whatever terminal or maybe whatever command prompt you have so just open that and in there just type npm in it oh yeah maybe you know that we are just creating a simple express app or node app so packers name um obviously uh wait i may i messed it up so first you have to create a directory or make him um test video whatever it is and hit enter we are we have but okay okay linux will not forgive me if i come to going to name this directory like that so we need to use some kind of underscore okay just like that and just like that hit enter now we got one folder just go there test video nothing is there and in there uh first we get to do is uh initialize our node application so just you can use npm in it and after that uh let it be mocker test video uh description let it be name it as app.js and in there just we need to use this test command i am not going to use that uh let it be yeah clear your terminal and after that we are going to open our code editor in in this case i am going to use visual studio code uh if you are a big fan of atom or sublime text feel free feel free to use that okay oh so and yeah so my visual studio is opening oh please be fast hey yeah now we have our package touchation we have something in here so first of all we need to install our mocha and what's that chai so before chai let me let me install mocha okay so just go there not there uh in their website they told you to use this npm install mocha just like that and but i am searching for a little bit different things in here yeah like this you maybe you may want to install it as a developer dependency not like application dependency so you should you should use this one like test save that step okay so after that just head onto your terminal uh and paste it not like here in here okay paste that it is going to install moga and in the meantime we are i'm going to introduce you one more uh fun library which is chai okay uh childhears.com and one more which is chai http chai http okay so this is our chai http and this is our child okay and here we have npm install chai and we're getting a lot in there we can use served expect assert you can use either of either either one of them okay in this video i'm going to show you that how to use should okay so that's pretty much it so if you were there just go to uh yeah and then move to your skype they have provided all the things that how to use edges in this case we are using here cert okay so okay looks good so head on to your editor and in the test part uh here we have mocha in the dev deficiencies sorry dependencies and then just replace this piece of code by maka okay and uh you're not you don't need to do anything apart from that okay now in here we are we need to create one more file which is app.js which is simple app.js and after that we need to install a couple of libraries like one of them is the express one is uh body parcel uh maybe we need that one posting some stuff we need that okay so one is body parcel and one is express one more we need which is our child okay so just go let me open up my visual studio terminal inbuilt terminal so in here uh so first we need to install what we can say shine npm i just as save dash dash plus just install that so now we have installed child successfully and if you go to the package situation we are getting shy and not covered okay now head on to your app.js first of all we need one more package which is express for creating your api and just go there and just install one more package which is uh sorry one not two more package which is one is express and one more thing which is body parcel okay so now we have installed successfully body parser and express both so let's clear up our screen and just go to this part and in here uh we need to first make our first bn minimum application for our api so first let's uh create our api so initialize our app first is equal to require sorry express is equal to require express next thing we want to install is const uh one word one is there express and one more we need which is body parcel after that we need to create our app first let's listen to port 3000 but we are not going to listen we just need to export this app okay like module dot export app that's it so that you can use that okay so the next thing we are going to use is app.use which is body parcel dot json okay so let's create our very first route which is get route okay but before that let us make one more thing which is a list of programmers okay or a list of something uh whatever you can say like not constructed we let let we have and um don't know maybe uh area of programmers whatever it is okay let's make the programmers okay let's prove grammars let's say we have data coming from some kind of a database whether it is mongoose sql postgres whatever it is let's say we order data some for coming from somewhere we have our id which is let's say one name maybe john um language maybe python other one thing can be experienced uh suppose three years so we got this first everything which i have created and then we just need to copy this we want some data okay let's rename this 2 three four sorry four five all this is a list of programmers let's the first is john uh next is alex uh it's language maybe javascript or having one year of experience um next one which is maybe c sharp having two years of experience let's see and one is rohan is also there who is having experience in python of same three years and one more is again rocky having python i mean same uh but having one one years experience sadly yeah now we got our point uh so this is how we are having a group list of programmers and here we are going to operate our current application which is deleting and all kind of stuff so the first ever thing which we are going to do is having a get route which will get all the list of programmers okay so let's have that so app thought um get slash programmers after that we are going to have something called uh api which is going to true like having a request and response in this request we are going to have a list of programmers which are going to send in the annotation format let's send that press dot uh mutation uh so here we are having json format or maybe as you start status and in here we are going to use something called uh dot json and after that injection we are going to throw that particular array okay which is programmers like how many programmers we have okay so so this is this pretty much it of getting all the programmers let's see if i want to delete a programmer then app dot delete and in here we are going to use uh slash programmer slash delete slash programmer slash clone id but our id we can give and this is it and we have request comma response and in here we are just going to delete that particular id from the list or yeah we can do that and okay so so let's create some simple a lot because i am thinking that it will create it will take a lot of time to just create all that out and you guys also don't want to see a whole lot of people because i know that you can already create all those api so just thinking of making two apis like having a grid and having a post post start for adding programmers i think that's pretty much it okay so so let's do that so app because it will take a lot of time so just i'm just trying to be more precise on that programmer slash slash create slash program and in that we are going to send it from body okay so like we are having request comma response and in that we are having all the things in the body and we are just going to add that into the programmer list okay so we're just going to use programmers dot push so what we need to push we need to push that request body that's it and if it is pushed successfully then uh then we can simply raise the less the status of 200 or maybe 201 because it just created one more so the session here we are going to say success go alone uh created a programmer okay so this is pretty much it and we can add we get the programmers and create a product programmers uh hope you got it so we need to just run that app also for testing it's not okay but yeah that thing is cool right now and in here we are just going to pass the callback and just here we can simply console.log uh listening at code 3000 i'm hardcoding it right now okay so this is it we have created a programmer and also things so just run this app right now so now we are listening to port and let's visit to slash programmer on our browser happened to my google i think it's freezed so locals slash 3000 slash true numbers hit enter you're going to get the list of all the programmers that we are having okay so next thing which are going to test is in postman so let's open up your postman so let's open up your post man yeah it is opening right here so let's hit it out which is localhost last 3000 slash create slash programmer let's go to that particular thing which what yeah and we are having a request id name language and all systems we need here uh yeah and in here we are going to use a post request in the body we need a raw in here we need applications restation and in here we need id which would be files and after that we are going to use name which will be let's say my name which is septem uh what's wrong about still oh oh language and experience language let's say myself has a notion here and we have experience experience of let's say five years okay and let's send that now we are getting success created a programmer so this was fine uh this uh this was fine because yeah we are successfully testing it on postman but now we need to suggest that on our markup so for that just create our your one more folder here which is test and that you need to just add one file which is api underscore test dot js here uh the first thing which you need to require is child okay so comes chai is equal to required try after that we need to use one more thing which is co and cons you bonds shy is equal to required chai http okay we need to install one more package which is chai http uh i have told you we are going to need this package npm install child as http so for that let's head on down to our terminal again and shine as http okay so now we are we have successfully installed chai so now we can use it right here try http and one more which is shy dot cert it should also be there because we are going to use third operator sorry certain assertion for it okay and chai dot use um shy http okay so this is it this is it and let's close this right now let's see if we are having any error or not npm test okay we are getting some error sha has already been declared okay sorry save that again you start that what's there again chai dot suit is not a function okay we need to install okay we got something wrong here okay let's go to the chai and oh okay let's have a look in here here is giving your child or suit but why is it not working in here is not a function okay so let's remove it for now i'm going to figure it out in later okay so first we are getting here chai and http so first we need to describe our api okay so let's uh we are going to describe some api which is uh again let's save that and run it right now then whether we are getting any error on whatever it is um okay currently we are not having any assertion that's why we are having this so we are going to use describes okay now my editor is a little bit smart enough so it automatically generates all the code so we are not going to generate it automatically i'm going to show you step by step process right how to generate it like describe and after that we are going to name whatever testing we are going to do which api testing we are going to do here uh so here we are going to check our slash programmer api which is listing all programmers we are going to paste that and we are going to write test cases for that and here in here we are going to use something like this and in here we are just going to use done uh so whenever our test is successful we are going to just pass this done uh to wherever i need to need it but we are going to now we are not going to use them right now uh we will use it whenever you need okay so first uh so the first test case i'm going to write so whenever i whenever you are going to write any test cases you are going to use it okay so in it uh first thing which you are going to write is what testing you are doing here okay uh that whatever testing you are doing here just right here so let's say you are checking here like response should be an edit do you so the first so you should write here like uh response uh okay or maybe more precisely you can say it like it should uh return a response of a response of type array means it would contain a response of type adding okay in here we are going to use this okay uh so cool now we are getting that and also we need to use one more thing in here which is server uh i forgotten that is equal to require dot slash let's see where we are having any error on or what uh uh okay okay we are not having any education currently okay cool so shy dot um request oh not like that uh again let's uh try http for which i don't use http um oh wait we need to uh require mocha also okay what is happening here oh let's suppose we are getting a chai thought to request let's go there first and see the documentation first okay child require childhood use childhood okay this okay childhood request app okay got it so we need to request that uh using our child okay chai dot us uh in this case we are going to request server dot dot post or maybe something different we need here yeah we are using your chai http let's maybe i have made some typo in here okay okay okay you have to use it like this here because we are not calling it here okay so that's why it is what's giving me error okay so we need to just use a chai dot request just like that chai dot best uh server dot get and in this state uh we need to pass the api whatever we are using slash programmers dot uh end and after end we are just going to pass here done like all things are done okay and suppose you need to pass it here okay so let's go there and see what error we are getting here okay chai dot use is not a function my show okay okay we are importing here chai http that's the problem we are having okay auto completion done the done all such mess okay so so let's uh give it a sort again so now we are having like a list of programmer passed currently we are just using it like forcefully we are just passing our request but we are not going to use it like that uh so in uh end end is always going to give you a response okay response from the server so we need to use that response and we need to check for it response dot um suit dot b uh like that maybe uh i need to refer to documentation first because they keep on changing that should okay you're waiting right that should be uh like that okay we're getting it uh response dot body dot insert dot b dot uh any okay so we're just checking that response should be an array because here we are getting a response of adding so this is the thing which are going to test it right now uh so after this is done we are going to just use like done in here and just hit it right here okay so let's do one more time so here we are waiting body of smell means you are not getting any response why should uh which response what is getting here is like this response dot body dot should be a okay let's console.log what's we are getting as a response then we can debug that debug that sorry response so okay we are getting a response of null in here uh maybe slash programmers yeah that's perfectly okay but why we are getting uh response of now let me figure that out uh slash programmers server is also there and we are getting a response let's check that okay we need here two functions right like error and response we need to use both that's why we are getting error error comma response then maybe this sponsor body would work like that okay uh darth sid dot okay we are getting it dot dot b dot a now we are getting a cert uh um let's have that here so dot b chi dot sir okay we need to use here like try dot cert like this in here then you can use that okay so here so now our test is also passing in here so so like you can see that you can just get a request and you can just simply like a array and all those things could be done easily okay the the bugs should also be there that's why uh in my any video i never uh means what you can say forward any bug like cut out any bug part or something so that you could you should know that how to fix your bug if that occurs in your application also so so bugs are part of coding so don't get afraid of bugs and all such things because we coders face a lot during our work time and workplace like having bugs and all such things okay that's quite normal okay so this is all we got uh in described programmers uh so this is it for having a get request and let's have a post request again okay so in here this is our chai part like this and in the game we are going to use something called shy start request and in request we are going to use server again not like that server and in here we are going to use something called dot post and where we are going to just post to post to slash create slash programmer okay then send that and we are going to send something called the id first of all we need an id you are going to send your id which is of maybe five not like that files okay and next thing we are going to pass is a name it should be septem like that after that we need to pass something called language modus after that we need something called experience which will be let's say five years uh after we have seen that uh like uh post uh send then we then we need a and also and and in there we are going to pass our error comma response okay and then we need to check that response also like response thought should dot uh like half property or something like that i need to check that again and again okay uh should have to have property okay so we are going to have like two dot have talked it's like an english language as you can see like we're just chaining out everything like it should contain a property whatever we have mentioned right here like success it should contain that property okay success.property dot equals one more thing we need to check here is the response dot status sub dot have thought status and we need to check that the status also should be 201 because we are creating uh reading one programmer so we need to have a status of 201 okay so and after all those things are done we need to just give your done again so that okay my testing is done you can move again move ahead okay sorry um so this is thing we need to do uh so so let's run our test again and see whatever problem we are having right now okay one more problem we are having uh uncaught itself accepted to have uh success as usual c e double s okay we need to pass a response dot body no this is okay but in here we need to pass our body because we've got a body in here so we need to pass one more time because we got the body okay in body we have all the all the things we got and power equal to true uh what have you got here uh okay we are getting a create a programmer and we are using here to not that that should not be the case so we need to just use that that's why we use testing so that we won't mess up anything around okay so when you just use that and you just use this part and save that and after that here again we are getting a problem done it's called multiple times in the test okay uh this is because we are using here it and here we are using done and again we are using this term that's why it is giving us a problem so we need to just cut that out and we are going to use that once only okay so this is deleted cut cutting and this is also cutting down and we are just going to use it right here like after after having all the testers just run that done okay and after that you can simply done that once more time this is what testing we have we got error and we just fixed that out okay so this should so we are getting a list of program and it should also return a response of type array and without things are done okay we are getting two things because you should write it in different okay like it is only used for this particular request okay i'm just showing you just for having a testing purposes and all those things but there are much more better ways to write all those things okay so hope you like this video um if you liked it do hit a like and subscribe to youtube channel and so see you see you in the next video okay do follow me on instagram also because i keep on posting regular contents over there also so thanks for watching this video
Info
Channel: Geeky Satyam
Views: 363
Rating: 5 out of 5
Keywords: mocha and chai, api testing with mocha and chai, node js api testing with mocha, node js api testing with mocha and chai, api testing, satyam sinha, innovative minds
Id: KERWu-kUQwY
Channel Id: undefined
Length: 44min 21sec (2661 seconds)
Published: Sat Sep 05 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.