Home > database >  Generate test coverage for API tests? (for node / jest preferably)
Generate test coverage for API tests? (for node / jest preferably)

Time:11-04

I have service in nodejs which exposes API (rest/graphQL). I have different kinds of tests (unit, integration, functional = API). I can get coverage for unit and for integration. BUT

I would like also to somehow generate test coverage for functional tests (API / black box), preferably for node, jest test framework. Meaning that test generates actual HTTP request against a running service, now when it hits system-under-test, it should map lines affected by requests & pass it back to the test runner. Didn't find such features in official doc. Maybe some kind of plugin?

I remember ~10 years ago it was possible with php & some phpunit magic.. where it would intercept http requests and map it onto filesystem. OFC when you're running tests, you shouldn't do any other requests against the system.

I would imagine the architecture like:

 ---------------                     ------------------------ 
| jest API test | ==> http call ==> | HTTP server            |
 ---------------                    | coverage middleware    |
                                    | actual routes & code   |
                                     ------------------------ 

CodePudding user response:

Apparently you can do this with nyc. It just wraps node and when you stop the process, it will generate coverage of affected lines

nyc node index.js
  • Related