Unit Testing in Nest.js with Jest #1 - All About Mock, Testing Service Files

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so welcome back to this channel so in this video we are going to discuss about unit testing in sjs okay so I have already created an API of Library I have I have also made videos on how we can made a rest API in sjs you can see that this is official kit repo you can also watch that videos on my channel so you can see that here these are some videos that I have created before in which I have discussed about what is Nest JS okay how we can connect the uh how we can connect robot database how we can implement the authentication how we can handle the errors we have discussed about the pipes in an implemented the pagination search and how we can protect about all from unauthenticated users and all stuff like that so on this video we are going to unit test this complete API okay so let's see how we can do that so before starting this video make sure that you have the code from this git repo I will add the link in the description of this video so I have four branches make sure to download uh this one with the latest code okay so now let's see how we can you know task an sjs API so if I go back to my vs code first of all um if I go to package.json to run this we have to use this command which is the uh npm run test okay so it will use the just so just is a JavaScript testing framework okay it is maintained by meta and you use to test our applications okay so first of all we have to go to our source file in that we have two resources one is this book and one is the auth so let's test the book first of all in the book we have two main files which is the service.ts and then the controller.ts okay so first of all we will test our book.service.ts file okay so these are all the functions okay so let's see how we can do that first of all in the book I will create a file with the name book dot service dot spec dot TS or you can type here Dot test dot yes okay you can either use spec or either use test uh just will automatically detect that file that this is our testing file so I will use here book.service.spec.ts in that we are going to write all the unit tests for this book service okay so first of all I have to use here describe okay so also describe is just a Blog in that we can simply write our test so first of all what file we are testing so that is the book service okay now I'll pass here a callback function okay so now in that book service you will write all our unit tests so now if I simply go back to my Chrome you can see that here I am on the official documentation on the sjs so here we have this testing so you can see that here uh in the describe we will simply first of all type the name of the file that we are testing this is our block okay in that first of all we have to use this before each so before each will run before every test in that we will simply create our testing module so we will use this uh test from JS testing we will use this create testing module in that we can pass our controllers providers models and then we will compile that and then we will get our service or controller or model from the on this module reference okay and then we can describe our on blocks again for different unit tests so now let's use this before each and create our testing module so I go back and I will simply type here before each okay and that is going to be async because I have to use here await in that we have to in that we have to create our testing module so I will simply type here cons module that is going to be testing module we will import that from sjs slash testing and that is going to be await test dot create testing module and in that we have simply passing our providers we can pass any hover controllers okay so in this in this case we only have to pass in here the providers because we are testing here the service file okay so in that first of all we have to pass in here um the book service so I will pass here book service because we are testing the book service so I will pass that here and now also we need to set in set up here our uh our model if I go back you can see that here we have also injected the book model here okay because we have used this book model to find the um basically books in the database to delete books in database okay so you also have to pass that so for that I will simply use here provide okay then I will use here get module token okay in that assembly person here the book dot name okay and then after that we have to pass in here the use value and we are going to use for the value of the book mock book service that I will explain in a minute so I will simply copy that and I will create that here so cons book service is going to be this okay and we also have to import here the token and we have to import that from at nest.js slash mongoose okay so we have to import from here the get a model token dot module token that is model token save it and then after that we have to use here dot compile okay so now it will compile this testing module and give us back the service and then the model after compiling that we have to get our book service so book service and let's define here that first of all on the top so I copy that and I will Define that here book service let's call it let because we have to replace the value so let book service and that is going to be book service and then for the model so I will use here model and that is going to be I will import the model from Mongoose and type is going to be book okay so this is our book service and this is our model so book service is going to be a module okay we will use this module so module dot get okay in that we have to pass in here the book service so you will get that and we also pass in here the book service for the type and then um for the model I will simply use here model module dot get okay and that is going to be model which is this book model okay and then get model token we pass in here the book dot name okay so now we have successfully create our testing module in that we have access to our book service and then the model so now we can use this book service and module to run the unit test so now we have written the boilerplate for our bookservice.spec.ts okay so now let's go back to our book.survey.ts now we have to unit test this entire file so uh let's first of all unit test this function which is the uh find by ID okay because this is the basic function and after that we will test other functions as well so in defined by ID we are simply passing a ID of a book okay we are validating the ID and then if the ad is not correct we will simply return enter correct ID and we will then search the book in database if book is not there we will simply throw not found exception that book is not found then we will simply return the books okay now in this function you can see that here we are using this is valid object ID which is the function of this third party mongos then we are doing the find by ID which is also Mongoose function okay so we have here two dependencies now keep in mind that in unit testing we don't send any actual request to the database okay and also we don't call our third party dependencies we have to functions okay mock mean that we have to give the fake implementation of those functions we know that this valid object ID is going to return true or false there are two conditions for this function so we are going to mark this function and return the value in the same way we know that find by ID will give us a document an object okay that contains the book data okay so we know that this is going to return an object which is a document so we will give the fake implementation of this fund by ID we will mock this function so that we don't have to send the request to the database or we don't have to call any third party dependency which is this function okay so that's why in unit testing we always mock the uh dependencies so now in this function you can see that here we have two dependencies one is this is valid object ID and one is this find by ID so we have to mark this and then we have to mark this and then we will write the unit test okay so there are three different cases for this unit test one is that either it is going to throw this error or it is going to throw this error or it is going to give us back the book so there are three possible outputs from this find by ID okay so this means that we have to write free unit tests for this function all right so let's see how we can do that I hope that you understand if I go back to my book.service.ts let's type here first of all describe blog okay so we are going to test find oops we are going to test here find by ID function okay and now in that we are simply going to write all the unit tests okay so now in the describe I will use it block so it will you can say that represent a single unit test that it then the name of the test so name of the test you can see that we have to type here the name of the test so we will soon type here that it should find and return a book by ID okay that is going to be async passing her callback function okay so let me know that the first possibility is that we will get back our book if the ID is valid and if book is in the database so now we have to first of all mark this find by ID okay we know that we have passed in here the mock book service okay now in this book mock service we are going to pass all our dependencies of the model okay or you can say that all the functions of the model because we know that when we create a model we get all the Mongoose function like find find one find by ID finder by ID and update create and like that okay we have parcel here this mock book service for this model so we know that in Mongoose model we have a function that is called find by find by ID that is going to be just dot FN so just dot FN will create a empty mob function that will do nothing okay it is simply our empty function so now we have this find by ID in this mock book service okay and we have passed that in the um a testing module so now we can simply mock this function so how we can mark that I will simply use here just dot spy on okay so we are going to spy on on to the model okay because we know that find by ID is a function of the model and what is the name of the function you can see that here we get list of all the Mongoose function here automatically so we have to search in here the find by ID which is this one so we have to spy on on to this find by ID of this model and give the fake implementation or the mock implementation and how we can do that we can simply use here dot mock resolved value so where the mock resolved value it will return um a value that is resolved okay we know that find by ID is going to return a object which is a book okay so we have to create a mock book here so I will go to the top and I will paste that here so you can see that here we have here Bob mooc and we will import the category from the schemas okay so this is a mock book that contains an ID the user that has created their book the title of the book book the description author price and then the category you can write that and now we have this MacBook here we can simply pass that right in here so mock book okay so now this find by ID will return this MacBook which is the fake implementation of this function now I will simply have to call the find by ID and pass the ID of the book in that like we have to call this and pass in here the ID so what I will do here is I will simply type here const result is going to be await a weight then I will use here bookservice dot we have this find by ID which is this one okay now defined by ID we have to pass in here the ID so I will get the ID from the mock book dot underscore ID okay so now it will call the find by ID from the book service and pass the ID in that now what we can expect from this result we can expect that this find by ID is called with this ID okay and we have to expect that the result we will get from here is equal to this MacBook okay so I will simply type here expect okay so I will use here expect function in that we have to pass in here uh what we want to expect so I will expect here that model dot find by ID okay dot to have been called with okay to have been called with book a mock book dot underscore ID okay so first of all this is code and assertion so if you write one expect this means that we have written one assertion you can write multiple assertions here so now we are expecting that the model dot find by ID is called with this a mock book dot ID because we have passed that here okay so and the second assertion is that we will expect that result dot 2 equal to this MacBook that we have passed in here okay so we are expecting that first of all that this model dot find by ID is called with this ID because we have passed that here and then we're expecting that this result is going to be this because we have given the fake implementation uh to this function okay so now we have successfully written our first unit test and to write and to run that I will simply first of all let's move this terminal to um right okay and I will type here npm run test okay so it will run all the unit as so you can see that here book service which is the file name find by ID and first you know task which is the should find andotana book by ID is working successfully here okay so now let's run this in watchword so we don't have to run it again and again so I type here watch okay and then I will use here this P to filter the file the file is going to be book dot service dot spec dot TS okay so now it will only run the unit test for this file and you can see that our first unit test is running here successfully so now we have handled the first case for this find by ID we know that this is the fun case now the second case that it might throw this error which is the bad request exception that please enter correct ID so now let's handle this case first of all so now what I will do here is after this it I will create another it block that it should throw bad request exception if invalid ID is provided okay so to throw the error if we pass any invalid ID okay so we we will simply have to first of all uh create here ID that we want to pass so it is going to be invalid Dash ID and note that this is invalid Mongoose ID so we will pass that okay and then we have to mark this Mongoose function which is the uh is valid object ID we know that this is a third party function we have to mock that so for tomorrow that I will simply use here const is valid object ID Mark is going to be just dot spy on okay so we will spy on on the mongoose okay and now we have to spy on on to the is is valid object ID this function okay then we have to give here dot mock uh mock return value it is not a promise so we have to use here mock return value now the value it will return is going to be false so if it is going to return false this means that we will get on this error okay because now this is false okay so now we have successfully given the fake implementation or the mock implementation of this object is valid object object ID and now first of all let's type here away expect okay in that I will call here my book service dot find by ID in that I will pass in here this ID which is correct and I will expect that dot reject dot two throw the um bad request exception okay so we are expecting that this book service dot find by idb willpower we have passed here invalid ID dot rejects dot through dot two throw the bad request exception because we are passing here the invalid ID okay now for example if I type here not found exception that we are expecting that it will return the node mod exception if I save it um you can see that the test is failing because we have expected node found but we have received the bad request exception okay and we have received this message please enter correct ID so this means that we have to type in here bad request exception because this is actually a bad request save it so now it is passing so now we can write more assertions I will simply type here expect that is valid object ID mock dot to have been called with the ID okay save it and now the test is also passing here and now we have to also use here is valid job object ID mock dot mock restore so it will restore the original implementation of this is valid object ID okay so once we use this it will remove the fake implementation and give the original implementation back okay so now our unit test is also passing here successful this is our second unit test in which you have handled this bad request exception now there is a third case that it might throw book not found if you don't have any book with this ID in the database then we will get this error okay so to handle that what I will do is I will simply copy this from here first of all copy that and then I will paste that here so I will type here that it should throw not found exception if book is not found okay so now we have to mark this find by ID and this time it is going to uh return here null okay we know that if if the document is not in the database this Mongoose function return the null okay so mock resolved value is going to be null so this means that this is going to throw an error so I will simply copy this from here okay and then I will simply paste that here so you're expecting that book not service dot find by ID we have to pass in here mock book dot underscore ID we have passed the ID then it will reject and it will throw the note found exception okay and our second test exception assertion is going to be model dot find by Ali to have been called in this book dot underscore that if I save it you can see that the test will fail because now uh we are expecting the bad request exception but we received the node found exception so we have to pass this in here save it and now the test will pass so you can see that all the three tests are passing for this find by ID so we have successfully tested this complete find by ID function okay so now let's test this find all function so for that I will simply go to the top and I type here I describe in that we have to write new test for find all okay I will simply type here that it should return an array of books okay so we are expecting that this find algorithm a complete array of books okay so now we are using here three different functions one is find one is limit and one is just skip we are changing the functions so we have to mock all these okay first of all we have to we are passing here query and the query we are passing the page and then the keyword so let's create here an object with the name query and let's type here that for example page is going to be one and then the keyword keyword let's call it test okay so we will pass this query in the function now we have to give the fake implementation of this find so just dot spy on onto this model okay and this time the dependency is defined Okay then if I go back you can see that here we have this find and now with that fine we are also changing the limit and we are also changing the dot skip so we have to mark these threes uh in here okay so what I will do here is I will give here dot mock implementation so I will give the implementation of this function and now in this implementation I will simply return here this in that I have my limit and on further for the limit I will simply return this in that I'm going to have this skip okay if I save that is this okay and for the skip I will use here just dot FN like this and then I type here simply as any okay so let me explain that first of all uh you can see that the test is failing okay so if I go back you can see that here we have three different functions one is just fine the limit and the skip but we are not calling them one by one we are chaining that and after find we we have called The Limit after that we have called escape and now for that we have to give the the mock implementation okay in that we have our limit and then we have our skip further so now we can give the fake implementation to this skip that it will brjs.fn which is our empty mock function dot mock resolved value we know that we have to return here an array of books so I will type here array and pass in here the mock book so now this just dot find is going to return an um array of book okay and after that we are simply have to call our const result is going to be a weight bookservice dot find all we will pass in here the query remember that we also have to pass this query and then after that I will simply expect the result dot to equal to the array of mock book save it and now you can see that our unit test is failing okay because cannot Spire the find property because it is not a function not a function undefined um okay so we have to type here just dot spy on on the model and that is the uh find okay so to resolve this error what I have to do here is I've simply go to the top we have to also pass that find in the mockbox service so that is going to be fine okay and that is going to be just dot FN okay so now if I press here command s and now you can see that the test will pass because it has done that book if I remove this array from here and if I save it it will definitely fail the test you can see that here we are we are expecting an object but we get here an array of object so you have to type in here the array save it we can also write one more assertion here that expect the model dot find dot 2 have to have been called with um the title pass in here dollar if I go back um I will copy this from here and I paste that here that is going to be test save it and now I think so it will pass you can see that the test is passing okay so we will expect that this model load find to have been called with this title because we are searching the book in the title so we have used here rejection and the options so we also can write this research and also we can write that the result to be equal to the array of the books that we have given here the implementation so in this way we can also mock the functions if we have that in in this form if they are chaining like this okay so now we have also written the unit test for defined all we only have here one case so we have written that and now let's write the you know test for this create function okay so if I go back and let's write that so I will scroll down and I will create another describe block here like this okay so let's close this one and now also let's close this one okay so that is going to be create in that the original type here that it should create and return a book okay so async and then I will simply type here just dot spy on onto this model okay now if I go back let's see first of all the implementation here we only have one dependency that is the create which is the Mongoose function so we know that create will save the book in the database and return that book back okay so we have to give the fake implementation of this create or we have to mock that so I will spy on onto the model and the function is going to be create we can also use one more function like we can give here mock implementation okay now we have here the mock implementation and also we have here mock implementation once so there is a little difference between these two functions so you can see that mock implementation basically accepts a function that should be used as implementation of that mock but if we use the mock implementation once then it is same that it takes abstract function that will use as implementation of the mock but it is only valid for the first call or the one call okay so let's use here this mock implementation once okay and now in that we can simply give the mock implementation like we want okay so this is also a way to give the fake implementation also you can use a mock resolved value so we have to just return here A Promise so I will use here promise dot resolve and I will simply pass in here the mock book we know that this is going to return a book so after that we simply have to call the function so const the result is going to be a weight bookservice dot create in that we have to pause in here the book that we want to create so we need here a new book so what I will do is I'll simply copy that how can I scroll down okay and I will paste that here now we know that this is a new book that you have to create it doesn't contain any user or ID okay so in that we will simply pass in here the book sorry new book as a create book dto okay then we have to pass in here the user because we also have to pass the user so we need here the mock user so in the mock user we are going to contain some mock user we have our name email so we have this ID name and email so this is mock user so we will simply pass in here the mock user as user like this okay and we have to import the user with dot dot like this okay so if I scroll down you can see that here we have simply pass the new book as create create book dto and the mock user as user okay and after that you can write our assertion so we can simply write here that expect the result dot 2 equal the mock book if I save it from here you can see that the test will pass so we again have that issue so let's go to here and let's type here create that is going to be just dot f and which is a empty mock function if I simply save it from here and now the test will pass okay so we have successfully written our test.find all create find by ID and now we have to write in the unit test for our last functions uh which is the update by ID and then delete by ID so let's first of all write zero test for this update by ID so I will simply go back and let me actually copy this part from here this will save some time so I copy that okay and then I paste that after this describe so that is going to be update by ID okay now in that first of all we need to uh pass the book that we want to update so what I will do here is I will simply first of all spread whatever in the mock book okay so basically we will get here a copy of the MacBook and now we have to update the title so let's type here title that is going to be updated name okay so we will expect that uh after updating we will get this book that contains whatever in the MacBook and the title will be updated name okay and on the title that you have to pass is going to be const let's call it updated book okay because this is what we will expect after updating the book and then I will simply type here um the book that is going to contain the title that we want to update so title is going to be this which is the updated name okay so now we have here updated book and then the book now let's uh mock the functions that is going to be Jazz dot spy on we have to spy on onto the model and the function that we are using here is the only one that which is defined by ID and updates I will copy that and I will simply put that here okay and then I will give here the mock a resolved value you can also use in mock implementation if you want like I have shown you before so mock resolve value that is going to be on the updated book okay so it will return this updated book that contains the updated title now I simply have to call the function I will simply remove this from here so book I will call here Dot um update by ID and I pass in here the book okay or you can see that mock book dot underscore ID okay and then for the body I mean because we know that you have to pass it here ID and then the book book as any and then then I will simply expect that model dot find by ID and update dot to have been called with first of all you have to pass in here this uh MacBook dot ID that it is called with this ID and then book and then we also have passed in here the ID book and these two options so I will simply copy that from here and I put that here okay save it and now we also have to pass this find by ID and update right in here that is going to be just dot FN Simply Save it and now if I scroll down uh you can see that here the test is failing um title is new book updated book okay we have to type here that the result dot title should be equal to book dot title okay Simply Save it and now if I run the test again you can see that it is now passing okay so we will simply expect that this model.find by ID is called with the ID the book which is this one and then these two options that you have passed in our original implementation and then we expect that this result dot title should be equal to this title which is the updated title okay so this is our find by update by ID it has only one case so we have written that now let's write unit test for this delete by ID okay so I will simply copy this from here copy that and I will simply paste that here so that is going to be delete by ID so it okay so we also have to change here the test name that is going to be it should uh update and return a book it should delete and return a book okay so this we don't need here these two Fields so we will simply Mark the find okay find by ID and delete and more resolved value that is going to be mock book okay so we have given the fake implementation to this find by ID and delete and then after that we will simply call this book service dot delete by ID and pass in here the ID of the book like this okay and then we expect that model dot find by ID and delete dot to have been called with the ID okay so we will move this part from here and then we expect that the result should be equal to the mock book like this save it and now if I run the test so you can see that here the test is failing because again we have to pass that in here that is going to be just dot FN if I save it now so now the tests are passing so now we have successfully written the unit test for all the book service find all create find by ID update by ID and delete by ID I hope that you understand so now let's write this uh unit test for this oath OS dot service.ts which is this one okay and then in my next video I will write the unit test for the controller files so what I will do here is first of all I will simply go to the auth create a new file here that is called alt dot service dot spec dot TS okay so now um I will first of all what I will do is I will um you can say that I will copy this code from here copy that and put that here so now here let's um remove this code because we don't need here the book service we don't need here this part okay and that is going to be auth service and then we don't need here this mock book we need here mock user I need here this auth service which is going to be auth service okay we need here model which is going to be the user model okay and then the mock auth service and we have just passed in here two functions one is the create and one is the find one okay so I will remove both of these and pass this in here and that is going to be auth service just replace the values that is going to be user.name in the same way we have this auth service all the service put that here that is going to be user and that is also going to be user okay so now we have here our all service that contains the basic code save it okay so now um if I go back to the all service you can see that here we only have two functions one is the sign up and one is the login so that's first of all handle the sign up so when they sign up we have this B group dot hash one dependency and the second dependency is this create which is the Mongoose function so you have to mark these two functions okay so now and also we need here this sign which is the jwd function we also have to mark that okay and also you can see that here we have injected the user model and also we have injected here this JWT service so this means that we also need to inject this datability service in our testing module so I will simply type here let JWT service and the type is going to be J WT service okay and then what I will do here is I will simply pass that after this auth service which is the JWT service okay now we get here auth service model and we also need here this um jwd service so I simply copy that and that is going to be JWT service and that is going to be the JWT service save it so now you have successfully injected that as well we have user model JWT service and on the service file now we can simply write our unit as for the sign up okay so we have issue here we have to put in here comma save it so now let's write the unit test now we also can write one unit test also like if I simply type here that it should be defined okay I can simply also write this basic note as that expect the service okay which is this service um auth service dot 2B defined okay if I save it and now let's run the test for this all service if I type here W then P to filter out that is going to be auth Dot um service Dot spec dot TS and if I run the file you can see that we have first unit test which is the auth service and you can see that it should be defined as passing successful here so you can also write this unit test as well in the other testing file also okay so now let's write other Unitas for the sign up so I will simply type here describe okay in that the test name is going to be sine up function okay in that we are going to have our all unit test so first of all that it should register the new user okay I will use here async and then I will simply I have to first of all um that is it actually so now if I go back you can see that here we first of all have to mark this hash and then create and then this sign okay so let's first of all mark this B grid hash function so I will simply type here just dot spy on onto the B Crypt okay and we have to also import the B Crypt from the B group JS so I will import that like this okay so I will spy on onto this B grip and the function um is going to be hash okay so we have here bcrypt actually yes all right so we are going to mark this hash function and we know that hash is going to return a hashed password so I will use here dot mob resolved value and that is going to be hashed password okay we will expect that it should return this hash password and then you have to move our second dependency which is the create okay the Mongoose functions I will simply a type here just dot spy on onto the model and the function is going to be create and then I will use here dot mob implementation once and I will simply turn here promise dot resolve and I pass in here the mock user okay so it will return this user after creating that and then um I will spy on onto my third dependency so just dot spy on onto the JWT service remember that this sign the sine function is from the JWT service so you have to mark this function such as jwd service and then the function that is going to be sine dot mock resolve value and more resolved value is going to be JWT token okay and that is not actually resolved value that is mock return value because it doesn't return a promise so mock return value which is this data Beauty token okay so now we have moved all the dependencies now we simply have to call the sign up functions of cons result is going to be await then service or we have here our auth service DOT sign up we will have recently pass in here sign up DDO and the sign up video contains the name email and the password of the user so I will Define that here so that is going to be this sign up dto so I will pass Here sign up dto like this okay and now we will simply expect that expect that decrypt dot hash dot to have been called okay and then we can expect that the result dot to equal to the token okay and now if I go back you can see that here at the end of the return a token so we have expect that it should be um the token okay so we have to also Define a token here if I go to the top so I type here let token is going to be JWT token okay oh we have to move this inside here okay so now if I simply save it and save the file you can see that our test is passing here successfully okay so if I remove this curly bracket and save it you can see that here we have expected this ring but we got here token in the form of object so we have to type here the object like this Simply Save it and now our user is registering successfully here now there is one more condition that if user passes uh the duplicate email okay so for that I have to write another function so what I will do here is I'll simply copy that and I put that here I don't need to mock here the hash and this one and I also remove this one I simply move this create and give here more implementation once and promise dot reject so it will reject the promise and we have to Simply pass here the code so the code is going to be 11 000 so if you don't know uh if you pass duplicate value in the Mongoose and in the mongodb database you get back the error and that error code is eleven thousand this means that this is a duplicate resource in this case this is duplicate email okay so that will throw this error so now we can simply expect that I will simply type here a width expect that service or auth service Dot um sign up in that we will pass and have our sign up dto dot rejects dot to throw the conflict exception save it and off and on the you can see that here we get here um the test is failing that the expected Constructor conflict exception received a function returned through okay so um we get this error because if I go to the here you can see that I didn't have any code for this error so let me write that first of all I will simply use here a try catch block after this hash password I'll use here try catch and I move the entire code in the try block and the error assembly type here that if the error Dot code is going to be eleven thousand okay then I will simply throw new conflict exception in the wrestling passing here duplicate email entered save it okay so now if there is an error with this code it will throw the duplicate email enter and consider now the test is passing but before that you haven't this implementation okay so now that is going to be it should throw duplicate email Android save it so now we have written the unit test for our sign up now let's write the unit test for our sign in function okay so when they sign in or sorry login we have first of all this expected result this one and then this one okay so if user pass incorrect email or incorrect password and you both are correct then we get back our token so we actually have to write here three unit tests and for the dependencies we have here find one compare and then the sign okay from the JWT service so let's try that I simply go back and then I copy this from here just to save some time and I close this from here okay so I will simply uh save it so that is going to be login dto in that we have to pass in her name and sorry email and password only I will remove this name from here and after that let's define our first unit test so that is it should login a user and return the token okay so I will simply type here just dot spy on we have to spy on onto the model and for the function if I go back you can see that here we have this find one okay so what I will do here is I will simply give here the find one dot mock resolved mock resolve value once in that I will simply pass in here the mock user okay so this find one will return that user this means that the login is successful okay and then we have other two dependencies and now if I type here that expect the result dot 2 equal to the token and before that actually we have to call this subconscious result is going to be await okay auth service Dot Login and that will recently pass in here login dto okay and now if I simply save it and run it um let's see what we'll get back we get back here the error because B group dot compare is not a okay so we have to move these dependencies as well which is this um compare and then the sign so I will simply type here just dot spy on onto the bcrypt okay and then for the function I will use here the compare so dot MOG resolve value once I will turn here true okay and then for the second so that is going to be just dot spy on and it is going to be jwd service and the function is going to be sine and Dot mock return value okay and it will return the token okay if I save it now um you can see that now the test is passing so um should okay that is the um login okay or you can say that sign in I think so we have here login so now you can see that under the login we have this should login and it on the user and the test is passing so now you have to write two other unit tests so I will simply actually copy this from here copy that and I put that here so now I will simply type here that we will mock this find one now it is going to return here null okay so this means that user is not in the database so we will expect that um so we will expect that auth service DOT sign Dot Login we will pass in here login login dto dot reject dot to throw the the unauthorized exception Simply Save It and you can see that the test is passing okay so for the second unit test I will simply copy that and I put that here okay so now this will return here the you mock user okay now the password is incorrect and I simply type here that it should a throw invalid email error and it should throw invalid password error throw invalid password error so now this time on the find one will return the mock user but this compare is going to return false because the password is wrong so I will type here false and now if I simply save it so now you can see that our third test is also passing here successfully so now we have successfully written the unit test for all the functions of book service and all service I hope that you understand if you have any question you can pause and the comment section I will remember to respond there and now in my next video I will write the unit test for the controller files for this author controller.ts and also for this book Dot controller.ts and then we will move on and learn about e to e testing okay so I hope that you understand so that's it for this video I will see you in the next lecture
Info
Channel: Coding With Abbas
Views: 13,030
Rating: undefined out of 5
Keywords: nestjs, nestjs crud typeorm, nestjs tutorial, nestjs microservices, nestjs graphql, nestjs with mongodb, nestjs crash course, nestjs authentication, nestjs project, nestjs course, nestjs prisma, nestjs jwt authentication, nestjs testing, nestjs in hindi, nestjs typeorm, crud, unit testing, jest, nestjs unit testing, unit testing in nestjs, jest mock, testing service in nestjs
Id: aBjmdLmE2zI
Channel Id: undefined
Length: 53min 39sec (3219 seconds)
Published: Wed Jun 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.