code100x / algorithmic-arena

91 stars 96 forks source link

[SUGGESTION] How about writing E2E Tests (Backend Specific)? #31

Open Manik2708 opened 2 weeks ago

Manik2708 commented 2 weeks ago

I could see three services in this repository: 1) A PostgreSQL DB 2) Redis (Caching and Queue) 3) Express as a store of APIs

We should move towards writing tests for it! Unit Tests are fast but only test the business logic (they can't test whether the SQL query written is actually correct, a code passing all the unit tests might fail the code in production!) and Integration tests are slow (can become a headache for developers). Therefore we can move to E2E Tests. These tests are faster than Integration tests and can be extended to test the action of these services!

So the route is simple:

1) We need to setup testing instances of all the services, as tests should not contact the real instances. docker compose can be a good idea for this.

2) We need to replace the the actual services with the testing instances when tests are running, that means Dependency Injection.

But there are some problems:

a) Dependency Injection Design Pattern: As far I know, there is no good library available for this in Nodejs. tsyringe is there, but it also uses a global instance of container, hence second point can't be achieved!

b) Docker might make the CI slow!

Solutions:

a) Traditional way of Dependency Injection (in JAVA) is by using Constructors and decorators on controllers but JS is a functional programming, so either we have to migrate to a new OOPs based framework like Nestjs or we might use a hack by just changing the protocol to GraphQL and using the context variable for Dependency Injection.

b) Caching in DockerFile and multi stage builds can fix this.

See this repo: https://github.com/Manik2708/Hi_Server which is implemented in Nestjs with E2E Tests and docker compose in CI.