Generating Test Coverage for Rest API Tests with frisby.js

1k Views Asked by At

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?

1

There are 1 best solutions below

6
Marco Talento On

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