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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys on a previous video we created a RESTful API using node.js and Express in this video we're going to test that API using mocha and chai so what is Makka pakka is a testing framework for JavaScript you can use it on the server and on the browser so it runs on nodejs and the browser you can use any assertion library you wish in this case we're going to use child maka provides the hooks before after before each and after each for example we can use before or before each to set up the database for example and then we can use after and after each to clean up the database maka make CDC to test a synchronous code and it also provides extensible reporting what is chai chai is an assertion library for no Jes and the browser and it can be used with any JavaScript tested framework in this case we're going to use it with mocha and it provides different styles to write our assertions so let's quickly review them we have three assertion styles we have should expect and assert we should we can do things like foo should be a string or foo should be equal bar or we can use expect and write things like expect full to be a string or expect food to have lengths of 3 and then we have assert that I think it's the most common way to do or to write assertions in most languages for example you can do things like assert type off and then you pass parameters the object and the expected value of that object or variable or you can do things like assert property the name of a variable and then the value in this case flavors ok remember to subscribe to the channel and let's get started as you may remember on a previous video we created our restful api using express in not Jes so here is where we implemented all the different HTTP verbs for the restful api so we here we are importing the Express library and we also use show to pala date the data so we are also so we'll being also using that library and here we basically created a local array and we've been using that array to ask database for our API so we implement that the get operation to get all our tasks we implemented the get PI ID to get and a specific task given this parameter or this identifier for the task then we created the post method here we also implemented the put method we implemented the patch method and finally implement that the delete method so we are basically able to get all the tasks we can get and a specific task by ID in this case we are using yet to get all the elements or get a specific task by using the identifier we can create new tasks we can update tasks we can modify a specific attributes of the object or we can remove an element we check now that the first task is not longer part of the array okay so what we are going to do now is we are going to test the API using mocha and chai so first let's stop the server let's clear this and let's install the dependencies that we need that are basically mocha and chai okay let's install mocha first NPM install mocha we are going to install this as a development dependency because we are going to use this just to test on our development environment so I'm going to add save there so these dependencies are going to stay on development and when we create the version for the version for production let's say we don't include this dependencies so I'm going to start mocha first and now I'm going to install child and if we go to the package that JSON file we're gonna see that we have these depth dependencies chai and mocha so what I'm going to do now is I'm going to add a script just to make it easier to run these tests so I'm going to write here scripts let's call this best and the command that we are going to run is actually mocha and now what we're going to do is we're going to create a new folder let's call it tests and we're going to add a new file here let's call you that Janus because we're going to test our tasks API okay let's import the different dependencies so in this case we need to import child require try we are going to import the server that in this case is the index exactly this one this file so we need to put that one require index and we are going to define the assertion style we have should we have expect and we have asserted so we are going to use should in this case so we say assertion style so this style and we say try that should and using this we find that we are going to use that assertion of style I need to have another dependency that I forgot to add that is chai HTTP so I need to install that one chai TCP okay and now I need to use that first I need to be Ani to include that dependency here is gonna be child HTTP and I need to glue the library that is child HTTP okay any need to use it try that use chai HD okay and now we are able to call our restful api using the HTTP protocol okay so in order to create a test using mocha we need to describe our test let's call it tasks API and then we define an arrow function where we close our test okay okay what we're going to test as part of the API we're going to test the cat route we are going to test the get by ad then we're going to test the post operation we're going to test total operation and the punch operation as well and finally we're going to test the delete operation okay let's get started with the get operation in this case we're going to get all the existing tasks so first we describe our test so let's say get yet for slash API slash tasks and we pass in our function here and here we say it [Music] it should get all the tasks and with us done object as a parameter and here we use chai where we say chai the request we request and we pass the server that is actually the API and we are going to test to get method and we pass the resource URL so in this case going to be slash API slash tasks and here we expect using the end method we are going to receive an error in case of an error on the server and a response and here we have our assertions so response we are going to use the shoot style so in this case our assertions will be like like this response should have status and in this case we expect a successful result so response should have status 200 response that body body should be an array and we also expect that that array includes three elements so if we go back to the server or to our API we are returning this array with these three tasks so we have these three elements so response that body that length should be equal to three okay and we need to call the done right over here okay so let's run this and BM test and the test runs successfully so we are getting the the tasks that we expect so let's make some changes and let's test for example I don't know if we pass the wrong URI we should have a photo for as a response as a resource not found so if we run this in this case we need to change this it it should not get all the tasks because we are using a wrong URL so let's run this again and now we get green for the tasks when we use the right URL and we get a green test because we are passing the wrong URL and we are getting a photo for us t HTTP status code okay let's move on to be get by ID route so I'm going to copy this okay so get slash API slash asks slash column ID and it should get a task by ID so here let's say that we find the task ID task ID we say one we are using one because we have three tasks the first one has this ID one the second has 82 and the third one day D is equals to 3 so we're going to use that one that we know that is part of the response of the API so we are going to concatenate that here and here we are going to change the assertions we are going to expect a 200 as as a response to finding here okay so we expect the 230 stones but I need to change these assertions because what we expect here as a response body we expect an an object actually so in order to specify that assertion we need to do something like this response that body that should that be an object option and we also expect the body should have a property ID and also the response that body should have a property name and the same for the other property that is completed so we expect these three attributes or properties okay and also we expect that the value of the ID property to be equals to 1 so we have that assertion at the end ok let's run this and as we can see all the tests are passing as we expect so if we let's say that we expect that PID is 2 if we run this we get an error because the expected value is not the one that would receive and that's a way okay I'm going to change this this is going to be one and now we get green in all our tests okay so let's create another test using a random identifier value that we know that is not part of our array of tasks so let's say 123 and in this case we expect 404 and if we go back to the API we are returning an actually a message here that says the task with the provider ID does not exist so we are going to copy this and we are going to add that assertion so we say that the response that text that should be equals to that string so in this case it should not get a task by ID let's run this and as we expect we are not getting this idea we get a 404 and we get this error message so if we go back to postman for example and we execute the gate operation this is an identifier that is valid so if we run this oh sorry let me start the server I'm going to create a new terminal okay no in Ex the tree yes okay so here if we execute the cat operation with an existing identifier we get this object with this 3 properties ID name I'm completed and the same with the AIRAID right we get the array if we execute using this resource but if we use a random value that is no part of the array we get a photo for 3 guns here and we get that message ok let's go back let's clear this and let's move on to testing our post method ok I'm going to copy this and here we change the description post API tasks it should post a new task and here we are going to create the task that we want to create so let's say tasks and here we create an object literal and we pass two properties we pass the name that's gonna be task 4 and we pass the completed property as false okay and here we change the HTTP method I post we remove this and we need to pass the new task using sand sand how many passes in this case we expect 201 as the status if we go back to the API we are using yes we are using the 201 HTTP code as a response when we create a new resource so we are going to make an assertion where we say that we expect that HTTP code as a response and this is going to be pretty much the same because we expect we are returning the object that we create here so we expect an object and we expect a centerfire we expect for sorry for us the value of the identifier for this new object and we can also add some other exceptions so we expect that e equals to 4 what copied is 2 we also expect the name to be task 4 and we expect the completed property as false ok I'm going to remove these assertions because are redundant here we are checking not only the value but also that the property exists so you can read those assertions and I need to assign each one this here and let's run these tests and we get an error yes because there is a space between dusk and four so we're gonna change the assertion here and now this should pass okay and completed equals to false and yes there's a neural here I need to close and now we should get okay unexpected token okay here we go let's run this again and now we get green in all our tests okay let's create another one for the negative so let's say that we pass with an passed the name and as you may remember we are using showing here where we find the schema and we define the name as I required property or so required attribute so if we don't pass that attribute we're gonna get 400 error so we expect in this case that should not post any tasks without without the name property so we expect here a 400 and we also expect a text message here and I need to replace this with this one the name should be at least three charts long so I'm going to add that message okay let's run this and our tests are passing just fine so let's say that I put 200 here now I'm going to force these two to fail because we're going to get up 400 and yes so we get 400 and we expect a 200 that's why it's failing so I'm going to undo this and put the right assertion there and if we run it all our tests are passing okay let's move on to the put route and in this case what we're going to do is we're going to modify an existing task so I'm going to copy this this is a very put API this is going to be put slash API slash tasks slash 1 or slash ID to make it more generic and should put a task and we're going to find the identifier of the task let's say const dusk IV equals to 1 and we are going to change the name of the task 1 disk name this task 1 changed and we are going to set completed to true ok and here we change the post by put and we concatenate the task ID in this case is going to be equals to 1 okay in this case we expect 200 and we expect the response to be an object the ID should be equals to 1 we expect the name to be task 1 changed and we also expect the completed property as true okay so let's run this and we get green in our new test ok so let's say that I change this and I expect that value has the name of the task you're gonna get a read on that test because the expected value is different from the actual value so let's remove that let's run this again and we get all green again now let's test the put now we are not going to pass the name or the correct name remember that we define it here if we go back here we define that as minimum we need to pass three characters as the name of the task so we are going to pass just two so in this case we are going to expect a different result we are going to spread we're going to expect a $400 and we are going to expect an error message so I'm going to copy this because this is the error message that we expect that is basically the one that says that we should pass at least a three charles long name let's run this and i'm going to change this it should not put a new task with three characters so let's run this again okay and now we get all on green if we have a new character here we're gonna get a read on that test because the actual value is different from the expected one okay let's remove let's run it again and we get all green again okay now let's move on to the patch route so I'm going to copy this this case it's going to be the patch HTTP method and we're going to change just one attribute task one changed and it will match an existing task I guess I didn't change you should not put it should put it is actually an existing task so existing task okay I need to change that remember that the tests are part of the documentation of any application so it's important to get this right okay and to actually provide the right description of the tests okay okay here we can use our put or update so whatever you you prefer okay it should batch an existing task we'd find the identifier of the task that we want to patch we find the attribute that we want to update and here we create the assertions that should be 200 will receive an object the ID should be first one and we expect this so let's change this let's put put just to use a different value that we are using here sorry let's use patch but okay and we expect completed equals to true okay this one is that was a copy/paste issue because we are not we are using put here and we need to use but essentially now this is going to pass yeah now we are able to use it because when we is put we require all the attributes name and completed but we patch we only pass we only require the name of the task so let's test passing name with less than 3 characters so in this case we pass to a and in this case we expect a 400 error [Music] and as you shall expect text message like this one let's run this in this case it should not touch an existing task with a name with less than 3 cutters [Music] ok let's run this and all our tests are passing ok and now let's move on to testing the delete route this is going to be the last HTTP verb that we need to test okay let's copy this and delete we're gonna need to pass the ID we need to change the HTTP verb for delete here replace put by delete and here we expect a 200 response and that should do it ok any need to close this and we need to say that it should be get an existing task ok let's run this and this is failing because worse I need to task we don't need to send the task in this case we just pass the identifier and that's enough so I'm going to run this again and now all our tests are passing again ok and now let's say that I execute these delete operation but I use some random identifiers I mean it fire that is not part of our database so I'm going to pass 1 5 and I expect here a photo for and I also expect text message and I'm going to grab that message from here that is actually this one the tasks will be provided it does not exist and of the copy this and I'm going to put that on the assertion okay so I'm going to run this and now our tests us I need to change the description it should not believe an existing sorry a task that is not let's run this again and now all our tests are passing and we have the right description of the test ok guys that's pretty much it so we added multiple tests for all the HTTP verbs of our restful api so remember to subscribe to the channel and I see you guys in the next one take care bye
Info
Channel: Pragmatic Reviews
Views: 42,069
Rating: 4.9061584 out of 5
Keywords: mocha js api testing, mocha js testing tutorial, chai tutorial, rest api, express api, express rest api, http requests, mocha js, mocha chai, mochajs, mocha tutorial, javascript testing, js testing, restful api, node js express, mocha, unit testing, javascript, chai, testing framework, assertion framework, assertion library, rest api testing
Id: I4BZQr-5mBY
Channel Id: undefined
Length: 40min 48sec (2448 seconds)
Published: Wed Nov 06 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.