4

Its recommended to use frameworks like Mocha, chai or jest to make use of istanbul to get code coverage.

I have designed a testing tool, which will send a request to a api and based on the response, it will be termed as pass or fail.

The payload and the expected result is present in a excel sheet. For each row, it will send the payload present in the corresponding column and compares the response with the actual response we get. With this type of environment, will I be able to get the code coverage of my application.

Elson D'Sa
  • 99
  • 2
  • 9
  • 1
    Yes, the coverage is 0 – Matt Aug 25 '21 at 05:52
  • I also want to get fit without doing any exercise. – see sharper Aug 25 '21 at 06:04
  • @Matt i will fire set of routes externally, which will cover the application code. How to get that code coverage? – Elson D'Sa Aug 25 '21 at 06:57
  • @seesharper dear, as mentioned i am firing the routes externally which will cover my application, which is obviously not like becoming fit without doing exercise i suppose :) – Elson D'Sa Aug 25 '21 at 06:58
  • OK, but that is far from clear in your question. – see sharper Aug 25 '21 at 07:00
  • @ElsonD'Sa I figured. Could you edit your question to include a bit more detail of your test setup. You can instrument any node process via nyc/istanbul you just need to do a bit of work around shutdown/report generation. – Matt Aug 25 '21 at 08:20
  • hi @Matt I have edited the question, could you please go through once and check if the scenario is possible or not. Thanks in advance :) – Elson D'Sa Aug 25 '21 at 08:36

1 Answers1

4

nyc can instrument any node process (the more complex your build environment, the more complex the setup).

nyc node server.js

You just need a way to shutdown at the end of the tests.

A conditional finalise route could be added

if (process.env.NODE_ENV === 'test' ) {
  router.post('/shutdown', ctx => {
    database.close()
    server.close()
    ctx.body = 'Shutting down'
  })
}

Then run the test server via nyc

yarn add --dev nyc
NODE_ENV=test ./node_modules/.bin/nyc node server.js

Then you get the report and a .nyc_output directory

-----------|---------|----------|---------|---------|-------------------
File       | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
-----------|---------|----------|---------|---------|-------------------
All files  |   58.33 |      100 |      50 |   66.67 |                   
 server.js |   58.33 |      100 |      50 |   66.67 | 15-17             
-----------|---------|----------|---------|---------|-------------------
Matt
  • 68,711
  • 7
  • 155
  • 158
  • perfect answer!! The shutdown part is literally amazing, as i was simply doing ctrl + c for process to shutdown, which was not giving me the result. After creating API for the same, i am getting the coverage. Thanks!!! – Elson D'Sa Aug 26 '21 at 07:56
  • You could add a process.on SIGINT/SIGTERM handler to run whatever the nice shutdown code is if needed that way – Matt Aug 26 '21 at 08:18