How to Integration Test Express Controllers with Supertest

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there welcome to the channel this channel is all about professional coding practices in this video I'm gonna show you how to integration test Express controllers with super test in this video we're going to install super test a super awesome library to test your controller super easily and then we're going to add tests to our controllers and that's it so let's get started we have a module that's missing tests for its controllers controllers so there are two controllers you can see it maybe has business logic that shouldn't really be here so you want to refactor this to a service layer let's start with integration test so you're making sure that you're not breaking anything here we have the mpm package so let's go there and now let's start with a simple test foreign this for typescript we need the app so let's import that request all right so let's start with this success okay but there's some warning here and that's uh make this asynchronous and do a weight here okay not working so we're missing what they did for our Outlet tests and that's to prepare mongodb so while we don't have this centrally we always have to import this going forward we might change that foreign let's add a few failing tests for the creation about the quest or just start with empty let's create another test fetch a list of one name Target yes so this is not going to be exposed again can you fetch it you will get the title you will get an IP you create dot IP and we will get created X all right so let's start with a simple test shoot fast and empty list of gestures this route is actually not supported so we might actually have a test for that as well to cut an empty list of gases one name Target does not exist so let's start with the create I think actually you need a name Target ID to be able to create let's start with that not create a guess okay it's working so we need should create I guess when simple let's create a guess here we need a network Name ID equals [Applause] 30. and this should be okay idea yes name um yes so that's another one should create a correct guess now we're gonna guess the correct name here no correct this oh true let's clean this up a bit so this one can be pulled straight into this one and like it does not exist you should create an incorrect guess so this is assuming that we have a name Target so let's take a look at that so let's do uh four each here [Music] so we're going to create a name Target with correct name and then here skip this one then we're gonna put in the next case I already have a name secure the correct guess and then you're gonna have here all right name maybe just for consistency we can also if you're incorrect name should not touch guesses by using no SQL injection so this time we create a guess and then we fast by name Target ID but they're not presenting in the name that I expected ID query so something like equals one not equals so that's everything that does not equal one and then you're expecting this to be empty okay so seems like as well no no SQL injection is not working so that's good what are some of the things that we might do next well we might refactor business logic out for the controllers and test it and we might might add husky to run tests pretty to your lint before you come in that's a super nice framework thank you all for watching I hope you liked the video you can of course refactor the test more going forward but make sure to keep them readable readability is more important than duplication please like And subscribe and comment if you like me to cover something so thank you all see you later
Info
Channel: Code Made Simple
Views: 534
Rating: undefined out of 5
Keywords:
Id: NLjiTMJDIcY
Channel Id: undefined
Length: 8min 33sec (513 seconds)
Published: Sat Dec 31 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.