How can I test a function which adds a record in the database in jest?
I don’t know why the function works , it adds a record in the database but when I try to do inside a jest test file it throws an error saying that the relation does not exists, even it does in the database.
relation "facilities" does not exist
Do you have any idea why it is like this? Can you give some suggestions?
Generally Jest is used to either manage tests within a different context, or used to write and run the tests directly. Most of the time this means writing unit tests, which by design don’t touch the database, as that makes it slower and more flaky.
You could use jest to test against the database, but then you run into the issue of external context possibly dictating the result of the test. Imagine if your test suite only works if its sunny outside, its similar to that. It works sometimes.
If you do want to test against an actual database you probably also want to “clean up” and “setup” the database every time your running the test suite, so it can be tested with a reliable consistent environment. This is not unit testing and more integration or e2e testing.
Or you take the other route of not testing against the database and mocking that behavior so you can focus on your code, and not so much what your database is looking like. As my guess is your database is making a query against things that don’t exist within the database for whatever reason. Or your database isn’t setup correctly and the test is actually finding a problem.