Writing tests for REST api NodeJS MongoDB

I am writing smal REST API in ExpressJS using Mongoose. I wrote unit tests for all the logic inside the application. The only thing left unchecked now is that the API returns the correct status codes, and if the limited resources are actually limited.

What I've tried so far:

I tried to use Request to do GET / POST / PUT / DELETE on a resource, but I find it very awkward to throw stuff in the db, keep the object references and delete them later.

Is this the smartest way to approach this? Is my implementation wrong if I need to validate something via actual queries against the actual database?

If this is the preferred way to test the API, you need to test the internal functionality as a complete solution, or just check the status codes and access.

+3


source to share


1 answer


We usually create a test database and run tests with NODE_ENV = 'test' ... which starts a connection to the test database.

This way you can erase the test DB without any problem and you don't have to worry about cleaning up before / after testing.

A useful tool for REST testing is supergenic (or supertest, although I prefer an agent):



Full disclosure: I am the author of the Super Agent.

+3


source







All Articles