Node testing with Jest | Supertest | Writing test cases | Test API's | Node testing

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to another exciting session i'm your host mahishkaria you're watching me on youtube now and today i'm gonna take you through the testing note testing so in this tutorial we are going to learn how to write test cases how to write test cases in chest and from scratch we i'm going to cover each and everything so without wasting any time let's deep dive into it so i already have terminal open here i'm going to create new folder mkdir name it test with chest okay and cd test with chest okay and here we go let's open the code editor code space start okay vs code is open and you need to create normal npm init or create node project this will be based on the top of node only okay so yarn innate i'll be using hyphen y or npm indeed you can use after that i am going to create new index.js file i am going to create new folder test i am going to create new file inside that index.test.js ok i am going to use just for that i need to create yarn add hyphen d just capital d just okay it will take some time till then we create just dot config.js okay here we need to write module dot exports which will be having it now you can write here the all the parts related to which you have to look into while testing so test match is a keyword that will help you to write all the directories like root dir which will be a keyword to identify root directory inside that i'll be having a test folder inside that in files which will be uh having test.js name in name that you can write here that we i have just wrote test.js so it will identify the name from that and inside package.json you just need to write the scripts okay so let's wait for the installation till then now we can start with the normal const add function which will get the parameters a comma b let's say and here we need to return a plus b this is it i guess for right shift f for formatting and model dot exports like this and will be writing add okay now add equal to require dot dot slash index okay and here you can write test function directly you can describe something no we are using dot to be to b is the thing that we are going to test now which is function okay which will be having a callback and inside that callback we will write the expect and excite expect you need to call the function with the parents a comma b okay and which is to be three so this is my very first test case now inside package.json i'm going to write my script let's rely on the suggestion chest so test will be there so now this will be my very first test case that i am going to run might be possible that will get errors i guess no it's taking time to execute okay fine yeah we got past first very first test case so this is the normal test space there are a number of functions that you need to know before getting started this is 2b now we'll be using two to equal okay to equal the more uh function names that you will know the more better you to write test cases okay this both things are similar don't worry about it here it will execute okay two equal is there to be is there to be defined is there okay so you can write to be defined to be okay like this so uh you can use dot naught as well so dot not to be defined okay or dot to be defined like this so it will execute again so there are a dot to be truthy like uh that you can use or to be defined or to be falsie fa lsy or to be null like i want to check like to be analyst there which is i generally use the common functions that i generally use to be null okay it will get not to be enough like this so it will work then only let's run this thing again okay after that you can write to be greater than to be or less than to be greater than or equal to be less than or equal okay to be greater than okay or let's say to be greater than one so it will work so to be greater than we'll say to be less than we'll test apis as well don't worry about it guys because these are the common functions that we generally use less than four okay so you can write less than or equal okay you can write which one got okay this one not less than four what was that received three to be less than four okay this is weird let's try to understand what okay not we are using okay this was the mistake or equal you can use like this as well so it's all your choice okay let's come back to next thing uh the next thing is that we can use to be close to okay uh to be less than one one plus two is three which is not possible we will write to be less than seven okay after that uh we'll write something kind of to be close to okay let's say we are having one point one one one one one two point two two two two two or let's say four four four four four four four to be close two is there those two is there which will be close to 3.5 i guess okay let's start to run this thing and now this time it will work i guess now to be close to or too close to i guess i'm confused but to be close to is there is so somehow this is not working well i i don't use this function on daily basis but i don't know why this is not working okay three point five five five five point one okay so we can write to be close to six i guess it should work i'm not sure it should work uh hopefully no not working anyways i don't know if you find something please let me know inside comment guys i don't use this thing on daily basis but this is something that you can use so after that we have another string that i generally use for testing with the string function like let's say i'm having a hello comma word let's say and i need to check matching element let's say that i can choose hello okay so if i'm if i get something matching like hello inside the returning string then it should return the uh expected it should execute the success okay okay fine so it's working fine uh for now and the next thing that we can use is like how we can throw error how we can get error and now we can write test cases for error for that i'll be using err so let's say in err we are going to written kind of written or we can throw new error okay throw new error and inside error i'll write i am new error like this so if you need to you can export this thing and you can import this thing here okay and after that you can do one thing like uh you can get this thing and here's somewhere i'm going to just need to remove this thing not in use okay ctrl c ctrl v and this thing that i'm going to check with uh to throw like this so here i'm going to check that this one is returning error or not to throw you can check like this now consider if you have n number of test cases with re related or to something like uh two new throw this is not working i am new error okay because there is one thing that i missed i guess here we need to call function then only it will work okay so now let's see okay working fine now imagine if you have a n number of test cases which is related to something in that so we use describe okay let's say i am block okay kind of block so so we can differentiate between the test cases this is related to something this is related to something i'm executing in a block okay like this okay so now i can write a number of test cases like this so let's try to run this thing so you can see guys how it looks like excuse me like this you can now create the blocks while writing the test cases now the reverse and now the very next thing that we need to know that how to handle promises that i guess here i can write let's say promise test promise test we can say where we will return promise directly written new promise which will be having resolve reject and here will be having a comma b and if we get uh let's say if a comma b if a minus b is greater than something like zero then we can say uh this is positive number okay resolve i am positive like this or else we can say that i'm negative like this else reject okay negative like this so this is uh you um kind of promise that you can handle in test cases first one there are two ways the first one is very easy way that i'm going to tell you guys like let's not execute this okay so uh now the thing is i am going to import this promise test here inside my um inside here okay test file and here we need to check i'm not going to use expect here you need to run one comma two it will return on dot then you will get data okay if you get data you can expect something from data okay and the data to be i guess positive if you get something inside then or you can catch error or e you already have error function now okay so here you can write like expect okay inside expect you can write to be which is to be negative so this is one kind of common or test case that you can write but um just comes with the lots of more cool functionalities that i'm going to tell you guys like before writing this thing you just need to wrap this thing inside expect and once you do this you can okay okay i made mistake i guess okay expect a dot results dot results okay so uh you can write dot results dot uh let's say to be or uh you can directly write rejects results directly here okay which is and after that i can write to be instead of writing this much of things but it will not execute because for that i need to write success case which will be two minus one okay two plus one i guess i'm not sure ah two minus one yeah so this is the easy way okay this is the old way let's try to run this thing with reject as well rejects to be negative which will be one comma two okay and let's start to run this thing again so yeah this is working fine so after that you need to do one thing uh let's try to use other things like there are if you have array let's say okay here let's say if you have a rr and you need to perform operation on the top of that okay like inside that i'll return directly kind of uh what was that bkt bat okay and this arr i'll export here okay and i'll import here okay and here somewhere i'll test that dot to contain i guess contain is there or yes to contain let's try err let's execute this arr let's use dot to content not to contain let's write bat so it should work i guess if you need to check the array contains something then you can write like this so yeah this is working so we have uh used everything i guess dot matches remaining we have code match less than greater than everything most of the regular things that i have covered dot not as well now we can write eps and taste on that as well i guess so without wasting any time let's write something let's add yarn add express here as well inside index you need to write kind of normal right hard-coded data first const express equals to require express normally const app equals to express after that app.you express dot json app.use again for body pass we use express dot url encoded extended url encoded extended true and after that i'll be using app dot gate normal user slash get users okay which will be having request response signed inside the target or json will return now kind of users area of users inside that will be having a name let's see email will be having my shared gmail like this and password let's say so this is something i'll get there so for that we need one another dependency as well so here i can i'll check status over there as well so i get 200 success and here app.listen 3000 and will export app here you need to export app in order to test okay here you need to import app and once you got the app you can use the test format will be same simple okay one more dependency you need to know that is yarn and hyphen d superset per set i guess i i'm not sure super saiyan it is super test okay so uh let's import super test equal to require super test okay like this and for that we need a sync thing okay async and of it here super test will pass app here we need to import app which is already there after that we need to check or expect or before that we need to use dot get flash users dot expect 200 dot then something like result we can say and inside that we can write expectations of result and let's say if result dot body okay and result dot body dot users let's say okay or type of body we can check type of results dot body okay will equal to equal to object like this so let's try to run this thing this is uh similar for everything like forget post you can write post here dot put delete if you want to send something you can send like this okay like this so rest of the result that you need to test always right here so you can test like this so you can see all the things are running fine and then this is test get users api like this name should be like this so you can see everything is working fine i so yeah this is working so uh this is uh uh you can expect you can write there are other frameworks as well other like chai mocha other things that if you know if you want some more tutorials please let me know inside comment box guys uh i'll try to make more advanced tutorial on testing as well so thank you very much all of you guys for being with me supporting me all the time it's been pleasure to be here if you haven't subscribed my channel already please hit the like button and subscribe my channel and if you want some more videos from my side please let me know inside comment box thank you very much bye bye good day
Info
Channel: Mahesh Kariya
Views: 29,423
Rating: undefined out of 5
Keywords: node, jest, test cases, testing, test API's, supertest, tutorial, nodejs testing, jest tutorial, test with jest, jest testing tutorial, testing in nodeJS, Node JS Testing Tutorial
Id: KQjeU0mBQU8
Channel Id: undefined
Length: 23min 42sec (1422 seconds)
Published: Wed Oct 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.