My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Need advice: How do you write tests?

Gijo Varghese's photo
Gijo Varghese
·Jan 3, 2019

We've our Rest API server built on NodeJS and we've started to implement "test".

Our app consists of a lot of DB calls (mostly using ORM), API calls to FB and other 3rd party services, Message queue etc.

We started implementing Unit tests. However, it looks like we need to mock 80% of the code and found it too much time-consuming. Moreover, at last, we will not be 100% confident with our code

A good example would be a createUser function written in raw SQL. It worked perfectly if the name is "Gijo". But will fail if the name is "O'bama". If I write mocks for createUser function, both Gijo and O'bama will succeed in the unit test but will fail in production (correct me if I'm wrong).

Another way I found is to write integration tests instead of unit tests. This seems to be much more efficient and gives us more confidence. And it's easy to test, sending multiple post requests to https://localhost/users with different names like "Gijo", "O'bama" etc and check whether it returned 200 and it's correctly inserted to our DB. In such a way we're also making sure our Queue, API calls, DB and everything is working.

What will you suggest in this scenario? Any advice?