0.0.5 • Published 1 year ago

@berlund/openapicoverage v0.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

OpenApiCoverage for NodeJS

Measures code coverage in terms of calls being made to an API specified by an OpenAPI specification.

Installation

  npm install -D @berlund/openapicoverage

Usage

Just import the Code coverage class and hand over your Axios instance to it. Then point it to your OpenApi specification and you're good to go:

const axios = require('axios');
const { OpenApiCoverage } = require('@berlund/openapicoverage');

const axiosInstance = axios.create();
const coverage = OpenApiCoverage.use(axiosInstance).withSpecificationFromFile('./openapi.yaml');

Run your tests as usual, e.g.

  it('should run', async() => {
      await axiosInstance.get('https://example.com/some/path');
      await axiosInstance.get('https://example.com/some/path');
      await axiosInstance.post('https://example.com/path/to/post', {data: 'foo'});
  })

Multiple Axios instances

Multiple Axios instances can be chained:

OpenApiCoverage.use(firstAxiosInstance).use(anotherAxiosInstance)

Options for API definition

In case the path of your API calls isn't entirely defined in the API's paths but in the base path of your specification, a path prefix is accepted:

.withSpecification(path, { pathPrefix: '/v1' })

Report generation

Console

After your tests have been run, you can print a coverage report on the console.

 afterAll(() => {
    coverage.printCoverage();
  })

This will result in something like the following.

    ╔══════════════════════════════════╤════════╤════════╤═══════╗
    ║ Path                             │ Method │ Status │ Count ║
    ╟──────────────────────────────────┼────────┼────────┼───────╢
    ║ https://example.com/some/path    │ get    │ 200    │ 2     ║
    ╟──────────────────────────────────┼────────┼────────┼───────╢
    ║ https://example.com/path/to/post │ post   │ 202    │ 1     ║
    ╚══════════════════════════════════╧════════╧════════╧═══════╝

The following options will output all combinations of path, method and response status, even if they haven't been called:

coverage.printCoverage({ showZeroCounts: true })

File report

Another option is to configure a file output which is updated on every Api call:

OpenApiCoverage.use(axiosInstance, options)

where options can have the following properties:

PropertytypeDescription
outputFormatnone | htmlnone disables file output (default) or simple HTML formatted output
outputPathstringA file path where output should be written to. Ignored if output format is set to none. Will default to the current working directory
0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago