S
S
Sergey2020-11-18 07:59:28
Software testing
Sergey, 2020-11-18 07:59:28

How to solve multiple testing questions in NestJS?

Hello everyone, I'm trying to write the first tests in my life now)
Many questions have arisen from this:

1) I have a large service that interacts directly with repositories from other components.
Is it ok? if not, what is the best way?

2) When I wanted to write a unit test for one large method in this service, I ran into the problem of mocking repositories. I have specific queries like:

await this.otherComponentRepository.findOne({
    where: { message: Raw((alias) => `${alias} ILIKE '${code}'`) },
    lock: { mode: 'pessimistic_write' }
})

Just locking findOne will not work, below it is used again, but with a different FindOptions.
Will it be normal if for each such findOne I throw a separate function into the OtherComponentRepository so that it can be locked? I'll call it findOneByMessageWithLock for example. Are there other options? How do you do it?

3) Complementing the 2nd question: I suspect that such things generally need to be thrown not into the repository, but into the service (by the way, why do they do this? why findOneByMessageWithLock in the service, if possible in the repository).

4) If you need a service, then how to live with a huge number of small functions like findOneByMessageWithLock in the service and still have huge functions with a huge flow of business logic.

5) Is there any material on habr / medium where all such stupid questions have already been resolved? Is there a repository with normal examples of tests for nest and typeorm, where not a calculator is tested.

6) What if I need findOneByMessageWithLock to return one record in one test and another record in another? How to swap mocks between tests?

7) Is it worth spending time on unit tests or is it easier to just e2e? I want to cover the code with tests faster, I didn’t even think about TDD at the time of writing the backend.

8) Jest conducts many e2e tests at the same time, how to fumble one database between them? After all, it is necessary that the tests be independent of each other.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question