I'm trying to write unit tests in order to check if a database connection is ok or not and verify the existence of db, collections, insert, update and so on. However, when I consult the jest documentation I found this guide. It uses a jest-mongodb package in order to run the database in memory. However, this example assumes that the connection is ok and we have a db then tests the insertion of a document in a collection. Even I found some articles, examples and code on the net, everyone concentrates about the CRUD without paying attention to the connection of the database and its configuration. My question is: Should I check the database connection and the configuration on one side? On the other side, I mock the different CRUD to do the unit test? I want to get different opinions about this issue and if anyone has any link, example, official documentation or code in github about this subject feel free to share with us.
j
stuff ;)
u don't unit-test a connection or you just unit-test the connection to a local socket which is more of an integration issue.
A unit-test should be the smallest possible set (atomic) with the minimum of external dependencies.
We can argue about the granularity based on the language.
In other words you want an integration test, also as soon as there 2 actors (app, db) it's already not really a unitest but an integration test.
Integration test means you have 2 parts that need to integrate with each other.
End to end test means you test for example the client -> app -> database -> app -> client.
So yes most of the people don't test the connection since you should get a runtime error anyways and it's the job of the db abstraction to unitest that