This is a 3 part series for unit testing the controller, service, and repository layers in a typical REST architecture. There are many approaches to doing this, I’m sharing a pattern that I find very flexible and hopefully, you can pick up some tips and tricks along the way.
Part 1 — Unit Testing the Controller | Git branch can be found here.
Part 2 — Unit Testing the Service | Git branch can be found here.
Part 3 — Unit Testing the Repository | Git branch can be found here.
Following on from part 2 of testing the service, the minimum was done to move on to the repository. As before, you can following along.
Story: ‘The information about the space ship needs to be saved.’
We now have the parsed and valid data to a SpaceShipEntity
. We want to save this to the DB.
The repository is responsible for DB interactions in this case. As before we can think:
Given — a SpaceShipEntity.
When — the entity is saved.
Then — it should exist in the DB.
And — it should return the entity.
#tutorial #javascipt #jest #nestjs #typescript