Testing Services (Deprecated)
This is a deprecated approach for testing services. For new services, please refer to Testing Services (Well-Known-Component).
Services SHOULD be mainly tested using API tests, that is, a set of tests that exercise the code of the service through its API by getting the service up, with its external dependencies being mocked (DBs and APIs). Unit test SHOULD be included in cases where utility sections of code with a high number of cases to be exercised or non-performant code is found, making it impossible or hard to test with API tests. The rationale behind this is that due to our micro-services architecture, that reduces the responsibilities thus the amount of code to be tested, the logic of the services is simple, making API tests the most time per value option when ensuring that the services work correctly.
Testing stack
Services MUST be tested using Jest as the main testing framework with supertest as the framework to set up the server and test it.
Directory structure
As services MAY have both API test and unit tests, there will be more than one place where the tests will go:
Unit tests MUST go alongside the file or module that they're testing, with the same name, but with the extension
spec.tsinstead ofts.API tests will exercise code all along the service, so they MUST be placed in a
testdirectory, in the root path. The files will be named as the service's resource that they will be testing, that is, if the API tests will be testing the resourcebooks, usually under/books, the name of the file MUST bebooks.spec.tsand MUST be placed in thetestdirectory.
What to test
Middlewares applied to the route (auth, etc), to ensure that the route works as expected.
The controllers logic, to ensure that the route does what we expect it to do.
API testing services
As mentioned before, API tests will be done using Jest and supertest.
To illustrate how a developer will do API tests, we have the following example:
We find in the example one route with different HTTP methods, GET and POST. Both endpoints refer to the same resource, books, so a file, named books.spec.ts MUST be created under the test directory to hold the tests for them.
All the routes and the different flows of executions MUST be tested throughly. These are the cases that the developer MUST test for these endpoints:
Requesting all the books works, responding with a 200 and the list of all books.
Requesting all the books of a given author works, responding with a 200 and the list of all books of that author.
Posting a book unauthenticated fails, responding with a 401 and an error message.
Posting a book authenticated works, responding with a 200 and inserting the book.
These cases will definitely get their its and the expectation of those its MUST contain at least the expected response status and what is expected in the response body (if there's any).
This is how these endpoint tests should be written:
Last updated