NestJs Testing Tutorial - Integration Testing

In this video, I will be showing how to do integration testing in nestjs without mocking dependencies.
If you love this video consider leaving me a like to help with the youtube algorithm and subscribing if you haven't.
00:00 Integration Testing Introduction
06:20 Project structure
09:45 Test setup
10:00 Setting up Jest
18:00 Setting up testing environment
20:30 Push prisma migrations
21:30 createTodo() test
32:00 createUser() test
Github repository: github.com/vladwulf/nestjs-te...
Closed captions sponsored by Prisma 😄
Get yours for free here: pris.ly/closedcaptions

Пікірлер: 32

  • @AT-mx3bx
    @AT-mx3bx2 жыл бұрын

    Very useful video!! Thanks a lot for sharing

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

    Hi Vlad, It's a very nice feeling to write and thank you for all your videos, especially NestJS one's. They are very informatic and crystal clear. I am a backend developer working with express JS and Now when I received an opportunity from my organization to work on NestJS, your tutorials are really a life saver. I would humbly request you to kindly prepare a tutorial on writing unit tests with Jest for NestJS and prisma application. Looking forward for this tutorial. Thanks

  • @Alex-bc3xe
    @Alex-bc3xe Жыл бұрын

    I am a big fan of Docker and Nest JS and of course Angular for the frontend and you are really good at Nest JS thx a lot.

  • @victordepta4069
    @victordepta40692 жыл бұрын

    Well done. It's a classical way of testing I think, but ofc still kudos.

  • @anankondec
    @anankondec2 жыл бұрын

    Как всегда ТОП!)

  • @adamtak3128
    @adamtak31282 жыл бұрын

    Hey Vlad, when using Prisma with Nest, should I be using the Prisma generated types or create my own DTO class with class-validator? The benefit with class-validator is that I can make sure a field is not empty or has a min/max length etc so in that case I prefer it but idk ... When it comes to filtering options or unique input fields, etc the Prisma generated types are really handy.

  • @DevilDude
    @DevilDude2 жыл бұрын

    Hey Vlad, I came from the FreeCodeCamp tutorial for NestJS. Great job BTW with that course. It was the least painful way to learn. Hoping to learn more about other kinds of test as you mentioned some things being outside the scope of "that" video. I was trying to continue with the bookmark app project here but I am a bit confused about the nature of integration testing. If I have a service for creating a user in the user module. 0. Should I use that to create the user in the integration testing for the related data(todo, bookmark, etc...), or is it required to create a new user using the ORM (Prisma) directly? I would really love to hear some of your thoughts about the approaches to take with integration testing on backend. I am trying to figure out how Integration testing is different than other forms of testing (e2e in this case). Really look forward to learning and getting into TDD so we can make certain CI/CD pipelines more robust in our organization. (It's held together by duct tape and a can debug attitude for now.) So far the e2e testing is a no brainer as it help achieve large coverage of our entire API logic with which the front-end is going to interact with. (Majorly Rest Request) Questions: 1. Should integration testing even be required for the NestJS application if we improve our code coverage in the e2e tests? 2. Should integration testing be done with the frontend (assuming we have to spin up a test server for this)? 3. If the end goal is to maintain all the functionality intact with newer features and account for more edge case, should an approach where the e2e test coverage is increased on both front-end and backend and can the dedicated "Integration" testing be prioritized lower? Would love to hear more from you. Cheers.

  • @mrs013

    @mrs013

    2 жыл бұрын

    I would like to know the answer too

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

    Hello Thanks for all the things you shared ! Can you please do a video on How to deploy NestJs app ? Thanks a lot !

  • @ExperimentKing78
    @ExperimentKing782 ай бұрын

    not working for me, database connection issue, cant listen error from db, pls help

  • @binov5012
    @binov50122 жыл бұрын

    Thanks a lot Vlad! Can you make a tutorial for GraphQL with Nest.js?

  • @CodeWithVlad

    @CodeWithVlad

    2 жыл бұрын

    Yes, it's planned!

  • @ManuelZavattaCAS
    @ManuelZavattaCAS6 ай бұрын

    hi, thank you for the best tutorial of integration tests on nest. one question, can i add validation-class pipe to check dto?

  • @Fran-zq2fx
    @Fran-zq2fx2 жыл бұрын

    thank you so much for this excellent tutorial. Just one question: I have tried to "inspire" to this project and re-recreate it in order to train my self and my mini-app where I use MySQL as db. I am experiencing issues when using the concatenated scripts to stop the containers, start them, run the migrations on the test db and run the tests. Apparently, it looks like the migration is run before the containers are completely ready, even if I tried to increase the sleep time. In you experience, does mySQL produces this kind of issues? And how can I fix it? So far I solve with the hack of starting the db and THEN run the script for the migration & test

  • @CodeWithVlad

    @CodeWithVlad

    2 жыл бұрын

    Hi Fran. Would you mind sharing your docker compose file and the scripts you are using? I've implemented something that solves that in the past. It's a bit tricky but basically, it involves making some health check calls to the database in a loop.

  • @Fran-zq2fx

    @Fran-zq2fx

    2 жыл бұрын

    @@CodeWithVlad thank you so much for your assistance. I am very new so I found it very difficult to find a solution ^-^. This is the docker-compose file: version: '3.8' services: dev-db: image: mysql:8 platform: linux/amd64 restart: always ports: - '3308:3306' environment: MYSQL_ROOT_PASSWORD: developer MYSQL_DATABASE: devdb test-db: image: mysql:8 platform: linux/amd64 restart: always ports: - '3305:3306' environment: MYSQL_ROOT_PASSWORD: developer MYSQL_DATABASE: testdb

  • @Fran-zq2fx

    @Fran-zq2fx

    2 жыл бұрын

    @@CodeWithVlad and these are the scripts in the package json (so far it works if i run yarn db:restart and after I run yarn test:int) I have also tried to incrase the sleep time but it didn't help. I was reading about health checks but I do not know how to "throw" them in the yarn command: "db:restart": "docker-compose down && docker-compose up -d && sleep 10", "jest:int": "dotenv -e .env.test -- jest -i --no-cache --watchAll --config jest-int.json", "pretest:int": "yarn db:restart", "test:int": "yarn migrate:testdb && yarn jest:int"

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

    What font do you use in your editor?

  • @robertkwesiamoah
    @robertkwesiamoah6 ай бұрын

    i watched your video on creating bookmarks with nestjs that had the e2e tests. i kept having listen EADDRINUSE: address already in use after i had created the first two tests for sign up and sign in apparently because app.close() does not seem to work well on windows. Is there a work around you know of? I cant seem to find a solution.

  • @rained23JMTi
    @rained23JMTi2 жыл бұрын

    so integration test is basically doing testing of your code integration with other module/ 3rd party ? the principle is to not trust the 3rd party that you integrating ? I just don't get what the point of it because for sure prisma already run their own test suite to make sure their released code works another thing is should you do integration test and e2e too or e2e can considered enough ?

  • @CodeWithVlad

    @CodeWithVlad

    2 жыл бұрын

    So to me, integration necessarily means testing several modules together, as opposed to unit testing where external modules would be mocked. Whether modules would have a database is up to you though. In my case if i'm running integration testing I would probably set it up and leave mocking to unit testing. I haven't seen that many projects with full test coverage, most of them have a very basic test coverage. In most cases an e2e would be enough. Usually e2e + unit tests and mocks is already great. Testing is not a science. Depending of your use cases you will have edge cases where you would need to run certain tests in a certain way. So to summarize. Unit tests help you to write your code, enforce standards and make sure you haven't made silly mistakes in your function. E2E testing tests your application as a whole. It does not test all the possible flows. E2E is heavy. Integration testing is useful if you want to test a certain subset of your application in more detail (say you have users with 5 different roles and you want to make sure that every user can do certain actions).

  • @rained23JMTi

    @rained23JMTi

    2 жыл бұрын

    @@CodeWithVlad thanks that help a lot.

  • @CodeWithVlad

    @CodeWithVlad

    2 жыл бұрын

    @@rained23JMTi You're welcome!

  • @debarghyachakraborty
    @debarghyachakraborty2 жыл бұрын

    If anyone encounters the error of property 'user' does not exist on prisma, just update the @prisma/client package to latest version.

  • @CodeWithVlad

    @CodeWithVlad

    2 жыл бұрын

    I think the error comes from the fact that npx prisma generate was not run :)

  • @debarghyachakraborty

    @debarghyachakraborty

    2 жыл бұрын

    @@CodeWithVlad I tried that a few times but it always gave red squiggly in the client file. It worked only after I updated it. Don't know why.

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

    Hey, excellent guide. However, I'm getting the error "TypeError: this[modelKey].deleteMany is not a function", the line "await prisma.cleanDatabase()". Do you know, how can I fix it?

  • @user-jc5ly6eu3d

    @user-jc5ly6eu3d

    8 ай бұрын

    me too

  • @lukabeograd

    @lukabeograd

    2 ай бұрын

    Me three

  • @rainbow-jw3og
    @rainbow-jw3og Жыл бұрын

    I have this problem "Cannot find module 'src/modules/auth/auth.module' from 'app.e2e-spec.ts'" I copy your jest-int but i have problem :(

  • @dorrakadri1474
    @dorrakadri14743 ай бұрын

    TypeError: this[modelKey].deleteMany is not a function 31 | ); 32 | // console.log(models.); > 33 | return Promise.all(models.map((modelKey) => this[modelKey].deleteMany())); | ^ 34 | } 35 | }

  • @lukabeograd

    @lukabeograd

    2 ай бұрын

    async cleanDatabase() { if (process.env.APP_ENVIRONMENT === 'production') return; const tables = [ 'users', //Other tables ]; await Promise.all( tables.map(async (table) => { const query = Prisma.sql`DELETE FROM ${Prisma.raw(table)} CASCADE;`; await this.$executeRaw(query); }), ); }