NestJS Unit Testing - Test Controller (with Guards) and Service

In this video we use Jest to create unit tests for our feed controller and service. The feed controller has guards and other dependencies are mocked. We also unit test our service, which has an observable. After watching this video you will understand how to do unit tests with Jest, particularly within a NestJS environment.
--------------------------------------------------
Recommended Udemy courses
--------------------------------------------------
NestJS - Zero to Hero: tinyurl.com/3r...
Angular - The Complete Guide: tinyurl.com/4h...
Understanding TypeScript: tinyurl.com/9w...
React - The Complete Guide: tinyurl.com/2t...
Docker Mastery (with Kubernetes + Swarm): tinyurl.com/ye...
Playlist: • LinkedIn Clone - Ionic...
GitHub: github.com/Jon...

Пікірлер: 14

  • @isaiahtokunbo7856
    @isaiahtokunbo78562 жыл бұрын

    i find the overall course extremely useful, thank you.

  • @JonPeppinck

    @JonPeppinck

    2 жыл бұрын

    I really appreciate that. Thanks Isaiah, glad it has helped!

  • @KhanhNguyen-wp5lz
    @KhanhNguyen-wp5lz2 жыл бұрын

    Bro is so Pro !!!

  • @dimitrisbellos01
    @dimitrisbellos01Ай бұрын

    Hey, I am looking for a setup for test suites in NestJS, but with addition of factories for classes, in order to help me with seeding and "Arrange" Step of the Test. Any idea where to watch that? Especially, for complex schemas where i.e. I want to test a module, which every entity depends on the creation of others.

  • @paulflmn3572
    @paulflmn3572 Жыл бұрын

    Hi, thanks for the video. I tried commenting the jwtguard parts in providers of the controller test file and even with the guard actives in the controller, the tests still pass so this part doesn't seem to do anything and the unit tests aren't blocked by the guard and stuff. Could you explain that ? Is it because the guards are actually blocking before entering the controller and here we are testing as if we are already in it ?

  • @saravanakumarps9214
    @saravanakumarps92142 жыл бұрын

    Hey ..I found this video so useful..Should we have to do the same for e2e testing with guards?

  • @JonPeppinck

    @JonPeppinck

    2 жыл бұрын

    That's awesome! e2e testing can be a bit easier to setup because you can just call the API end points directly. We do an integration/e2e test relating to the auth module in video 23. Cheers!

  • @shecodeshk
    @shecodeshk Жыл бұрын

    this tutorial is so useful, thank yoU! quick question - is it good practice to test guards/decorators/interceptors separately or is it better to test them together in the controller test? if separately, i have a hard time understanding how to write them as there aren't many examples out there it seems. if its good practice, may you also make a video for this? thank you so much in advance!

  • @JonPeppinck

    @JonPeppinck

    Жыл бұрын

    Thanks Jodi, probably it would be easier to test that the guard. decorator, and interceptors are working by having an integration test and having enough test coverage. For example, consider the auth module. The first test could be trying to access a guarded end point without being logged in, which should fail. Then, you log in as a certain role, but try to access a role protected end-point, which should fail. Then as a third test you could log in as an admin role and then you should be able to access the protected resource. The only reason to make things more difficult and set up all of the mocks for a unit test is if your company is trying to hit certain unit test code coverage percentages.

  • @shecodeshk

    @shecodeshk

    Жыл бұрын

    @@JonPeppinck Thank you for your reply and for sharing your tips!

  • @SolidousMdz
    @SolidousMdz2 жыл бұрын

    Maybe I misunderstood the test but since you are mocking the create method call and return, what exactly is being tested?

  • @JonPeppinck

    @JonPeppinck

    2 жыл бұрын

    Hi Moreno, the crux of this video is to demonstrate how to test controllers with guards - as most applications will have authentication and authorisation. Most of the work goes into the general setup, which this video focuses on, opposed to the actual test itself. In the previous video we did a unit test on the service, and in this video we tested the controller using mocks. It is ok to mock the request if you know what you are testing for, and this general purpose testing setup offers utility for more specific test use cases. Additionally, the next video we do an integration/e2e test which actually does test the end point and interaction between service and controller and data layer, so you have multiple tools to test your application the way you want. I've found that developers tend to gravitate to the extremes, in that thinking testing is either useless or it is essential - and of course things aren't so black and white as it depends on use case. Really, it is up to you if you value testing, and what things you think is worth testing.

  • @AjaySharma-vz9ei
    @AjaySharma-vz9ei2 жыл бұрын

    video is really helpful thank you so much i had a question, can we directly access the methods defined in services in the controller.spec.ts instead of mocking those functions ?

  • @JonPeppinck

    @JonPeppinck

    2 жыл бұрын

    You should be able to call the service methods directly if you don't override the provider. Although if you aren't mocking the service you will need to either have a separate testing database and/or clear the database mutations.