2

I'm using frisby.js for implementing automated Rest API tests together with Mocha. All tests are implemented in a separate testautomation project. The REST API implementations are implemented in other projects. The Rest API is based on Swagger. So, I'm able to reach/get the swagger.json as API definition.

Here is a simple example of an implementation:

const frisby = require('frisby');
const chai = require('chai');
const assert = chai.assert;
const config = require('config');
const { adminUser1 } = config.get('users');
const { adminUser2 } = config.get('users');
const restHelper = require('../restHelper');

describe('REST API endpoint \'/auth\' on environment: ' + process.env.BASE_URL + '/api/auth', () => {
  // POST /auth
  describe('POST /auth', () => {

    // POST /auth - Status Code 200
    describe('Status Code 200', () => {

      it('create a new session for user ' + adminUser1.username, () => {
        return frisby
          .post(process.env.BASE_URL + '/api/auth', {
            'username': adminUser1.username,
            'password': adminUser1.password,
          })
          .expect('status', 200)
          .then(function (result) {
            assert.isNotNull(result.json.token);
          });
      });
    });
  });
});

Now I want to get an information about the test coverage for this Rest API. So, I want to get an overview what is already tested and which API endpoints are not covered at this time.

Are there any tools, packages, etc. to generate and implement such a test coverage report?

GRme
  • 2,707
  • 5
  • 26
  • 49

1 Answers1

0

You can use NYC a new version of istanbul. NYC (https://github.com/istanbuljs/nyc) it's probably the most known and used coverage report for nodejs. And it integrates with mocha. Let me know if you need help!

You just need to add a coverage script to your package.json:

scripts: { "test": "mocha --exit --recursive test", //your test command "coverage": "nyc --exclude dist/test --reporter=html npm run test", }

EDIT:

Check this lib: https://github.com/mobilcom-debitel/got-swag

Marco Talento
  • 2,335
  • 2
  • 19
  • 31
  • Thanks for your really fast answer. I'm a little bit confused, because NYC does not need the REST API definition in form of the `swagger.json`. How can it work if the requirement (swagger.json) is not known? How is a test coverage possible in that case? Or does NYC fetch all REST API endpoints implicity? frisby.js is using `mocha`, but the testrunner is based on `Jest`. So, I start the tests via `jest --config=./jest.conf.js`. So, is it also possible to combine it with NYC? – GRme Jul 04 '18 at 15:29
  • Since you are using Jest you should known that Jest already supports coverage because istanbul is built in. Read docs: https://istanbul.js.org/docs/tutorials/jest/ and https://github.com/facebook/jest/blob/master/docs/Configuration.md – Marco Talento Jul 04 '18 at 15:42
  • The fact is that my implemented tests are completely separated from the REST API. Both implementations are in different projects. So, I don‘t have the source code of the REST API. so, the API is completely a blackbox for me and my tests. I don‘t want to generate a code coverage, I want to generate a coverage report with information about which API endpoint methods of the Swagger REST API I already test and which ones are not tested. Do you know what I mean? – GRme Jul 04 '18 at 15:46
  • Well, i think I understand now and it's not straightforward! I think you don't have a direct solution. One possible solution would be to fetch the swagger.json file and generate a matrix with all methods that you would like to test. You can do this before start running the api tests. Mocha supports a require flag! Then when running the tests you could mark in the matrix if the test was executed and at the end of the tests you report the result of the matrix. I am not sure if using nodejs is the best way for testing this case ... I would use JMeter or a similar tool... – Marco Talento Jul 04 '18 at 15:59
  • Check this library: https://github.com/omninews/swagger-mocha and this https://github.com/mobilcom-debitel/got-swag it might solve your problem! Your question is a good challenge :) – Marco Talento Jul 04 '18 at 16:04
  • If I use these libraries I have to refactor my implementations and I have to switch from frisby.js to the other tools, but I want to use frisby.js because it is really comfortable in our case :/ – GRme Jul 04 '18 at 16:20