Jest tutorial with Node | testing Node.js applications

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so what is jest and how can you use it for unit testing with your own express server so today we want to take a look at how to set up this framework here or this library which is developed and maintained primarily by facebook and it's a very nice tool for uh unit and integration testing your express or your node server and to demonstrate how this works i have a little example project over here so i already created the um like the skeleton and the logic simply for time reasons and i just want to show you what it does and then i want to show you how to set up just to test to unit test a particular functionality so this endpoint or this whole server has like one endpoint and this endpoint is called register and the idea is that let's assume you sign up for like one particular service and it's asking for your name like your first name your middle names and your last name and the thing is that the first name always stops at the first space so if you enter thomas alexander adrian smith as a first name then your actual first name is only thomas and your middle names are alexander and adrian and the last name is smith and that is the idea so basically this server like this endpoint you can send these three parameters there and then it's kind of normalizing it's all it's kind of like splitting up the first name into or it's splitting up the names into first name middle names and last name okay so everything is properly normalized and then stored inside of a database although i haven't implemented the data based part yet so if i send this request you can see that in the console here you can see okay it basically split up thomas as first name right and then the middle names are now alexander adrian and smith okay so it basically pulled out this alexander from first name and added it to the middle names and this is the functionality we would like to test yeah the service is pretty simple i will link the code in the description down below basically there's just one route i didn't add like a controller level the only thing it has is it has a service layer and this service layer has like one method which is called register register takes like the parameters and then normalizes them so in particular we want to test this method here sanitize names so we don't have to go over like the functionality of this but this is like a perfect candidate for unit test and in order to unit test something we need a test runner so we need to have something that runs our tests and one of the options we have is jest so that's why i would say let's go to this website let's go to documentation and you can see okay all you got to do is run npm install safe dev chest so that's what i'm going to do i'm going to go here um all right let me like pull this up i know it doesn't work like this okay so it could be that you don't see like the command here but i just copy and pasted it from the website anyway okay so i just copy pasted the installation instructions and now we should have it in here yes and what we now need to do is we need to say okay we want to have a new script called test and then all we have to do is say jest so this is the most basic form of setting up just right you install the package and then you add like for npm test you add like just and then just is going to automatically check like all your files for test files and then it's going to run all of these and there's multiple ways on how you can organize your tests one of which is you can create a separate directory and put all of them there however that tends to get like a little bit tedious when the project grows bigger but it's like a matter of taste so some people do it like this what i typically do is i co-locate them so i let's make a new directory here under service and let's just call it underscore underscore test underscore underscore and the naming here is important so just looks for like files in the test directory and also specifically for underscore underscore test underscore underscore files and what i'm going to do is i'm just going to name this user dot test dot js right so now just knows ah okay this is like a test file so once it sees okay there's like a test.js file inside of underscore underscore test it's going to run it and by the way it doesn't matter where this underscore underscore test directory is so you can have tests for your routes you can have it or in the routes directory in the service it doesn't matter and that's a nice thing about this approach and what we want to do is we only want to test like this method here it's called sanitize names and in order to do that what we need to do is we need to import like the service so i'm going to require it i'm going to go up and then i'm going to say user so remember we're in this underscore underscore test file so i'll go one up and then i'll go to user so this is our user service and what you can do ingest is also in mocha and other frameworks is you can group tests together in a described block and that makes a lot of sense if you have multiple tests so they are sort of like grouped together so what i'm going to do is i'm just going to call this user service and you will quickly see what this does or like how things are grouped right so you have this describe on a global level and by the way you can also nest things so in here you could have even another describe block but just don't overdo it so typically i use like one or two levels at maximum yeah so what could we test we could test this okay we want the service to not do anything if the separation is already correct so i'm going to say test and then does nothing if separation is already correct you can play around with the description here anyway right so you say okay this is the comment or what the test is doing and this is the actual test and in here what i'm going to do is i'm just going to call this method called sanitize names so i'm going to say user service dot sanitize names and then i'm just going to pass something so i'm going to pass john and then alexander and then smith okay so this is the happy case this means everything was entered correctly and this is the first name this is um the the middle names and this is the last name right technically we could also have i know brian here maybe okay and what this thing returns here you can see it returns like an object with fname middle names and last name so i'm just gonna copy this i'm gonna go here and then i'm gonna destructure it so i'm gonna take this away and this and this and by the way i would really recommend that once you start testing that you really start with something simple so something like that right start with a simple unit test don't start directly with testing the database and that kind of stuff just start with a simple unit test with testing like just one function and then once you get going you can do more complex stuff in just like global setup global tier down run things before each test run things after specific tests and so on what i'm now going to do is i'm going to say okay i expect that the first name is equal uh john right that's what we passed right because it should do nothing and now i'm gonna copy this three times two two more times and i'm gonna say uh middle names and last name and i'm gonna say this is alexander bryan okay and this one is smith yeah and that's it that's like your first unit test yeah and that is pretty much like our first unit test so as you can see it's dead simple we're just testing the functionality of one function well that's like sort of like the happy case so i'm just going to take this i'm going to copy it i'm going to say okay let's say test correct separation of first name if it contains spaces so if i have something like john alexander brian smith then what it should do is the first name should be john then the middle name should be alexander bryan and smith should be the last name right so now what he did is it's supposed to take like this alexander and add it to the middle names so we can also try that so let's try to run it again uh npm test would have been fine as well yeah now you can see that it executed two tests and this is basically how you can move forward right so continue to add test cases and continue to add like simple test cases at first don't do the complex stuff and this is how you get started with jest so now we now you got now that you got it running it will be much much easier to add like more an additional test but since i don't want to copy and paste the documentation here i think i can just leave you with that and check out the documentation for just it's pretty good and just start step by step i would recommend to first test the service layer and this is now a unit test and later you might also add like additional tests or additional integration tests with library like super tests where you test like the actual route and then the behavior cool so that's it pretty much thank you so much for watching i will post the code in the description down below on github if you have any questions just let me know in the comments you can also write me a tweet my twitter handle is at productioncoder so thank you so much for watching and i'll see you in the next video bye
Info
Channel: Jan Goebel
Views: 18,917
Rating: undefined out of 5
Keywords: jest tutorial node js, jest tutorial, express jest testing, node express jest, node js express jest, test jest node js, testing node js applications, testing node js with jest, testing node js, express testing with jest, javascript jest testing, javascript jest tutorial, javascript jest example, js jest example, js jest tutorial, testing node, jest example, jest example nodejs, jest example tests
Id: KqQoiFBp36A
Channel Id: undefined
Length: 10min 56sec (656 seconds)
Published: Mon Jun 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.