R
R
Rag'n' Code Man2022-01-15 21:43:12
Node.js
Rag'n' Code Man, 2022-01-15 21:43:12

How to test NestJS?

UPD: I added an additional tag to the question, since Nestno one noticed it under the tag.

What is the difference between testing the controller and the service, if the controller will eventually go to the service anyway? ( Nest CLIgenerates tests for both services and controllers).

Why do all the tutorials interact with some kind of jest.mockImplementation, if 99.9% of the services interact with the database?

Suppose, instead of testing the written code that works with the database, we will test fake services, because it turned out to be the right practice, but then why do we need createTestingModulethe ability to import real controllers and providers there, if in the end we will not knock on them?

Maybe you know more about some nuances of testing NestJS?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Kirshin, 2022-01-16
@iDmitriyWinX

If we talk about unit tests:
Controllers - we test that the correct status codes are given and the necessary services are twitching. You can check the response, you can check the validation of the parameters.
Services - it's more convenient to test when the pattern with the repository is used, and only the business logic is hardcoded in the service without accessing the database.
We are just testing the business logic wired in the service, the implementation of the repository / ORM gets wet.
Speaking of e2e tests:
You can use supertest. As in other languages ​​/ frameworks - a test base is created, clogged with various data necessary for the test (using factories, fixtures). In the tests, there is a direct call to the endpoints and the result is compared, as if we were testing it manually in swagger

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question