npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jest-openapi-coverage

v1.4.0

Published

Report OpenAPI coverage for Jest integration tests.

Downloads

3,068

Readme

jest-openapi-coverage

npm version

Reports coverage based on OpenAPI specifications for integration tests run using Jest.

It intercepts outgoing requests then reports coverage based on the OpenAPI paths and operations that were run and the query parameters included.

Installation

Install using npm:

npm install jest-openapi-coverage -D

Or yarn:

yarn install jest-openapi-coverage -D

Usage

Create a file that will be passed to setupFilesAfterEnv (e.g. jest.setup-after-env.js) and add the following contents:

import 'jest-openapi-coverage/setup-after-env';

Add the following to your Jest config:

module.exports = {
  setupFilesAfterEnv: ['<rootDir>/jest.setup-after-env.js'],
  globalSetup: 'jest-openapi-coverage/global-setup',
  globalTeardown: 'jest-openapi-coverage/global-teardown',
};

You will also need to provide the OpenAPI docs that will be used to determine coverage. This can be done via the docsPath configuration option or by loading it dynamically when your tests are running, for example:

import fetch from 'node-fetch';
import { setupOpenApiDocs } from 'jest-openapi-coverage';

beforeAll(async () => {
  const res = await fetch('http://localhost:1234/docs.json');
  const docs = await res.json();

  setupOpenApiDocs(docs);
});

Configuration

You can configure the coverage reporter by placing a file called jest-openapi-coverage.config.js|ts|cjs|json in the root of your repository, for example:

const config = {
  format: ['json'],
  outputFile: 'oapi-coverage.json',
};

module.exports = config;

Or using TypeScript:

import { JestOpenApiCoverageConfig } from 'jest-openapi-coverage';

const config: JestOpenApiCoverageConfig = {
  format: ['json'],
  outputFile: 'oapi-coverage.json',
};

export default config;

The available configuration options are described below.

coverageDirectory

Default: The value of coverageDirectory from the Jest config.

Specifically an alternative directory to store the coverage reports.

module.exports = {
  coverageDirectory: '/path/to/coverage',
};

coverageThreshold

Default: undefined

This will be used to configure minimum threshold enforcement for coverage results.

module.exports = {
  coverageThreshold: {
    operations: 80,
    queryParameters: 80,
  },
};

docsPath

Default: undefined

Specify the path to your JSON OpenAPI docs.

module.exports = {
  docsPath: './path/to/docs.json',
};

collectCoverage

Default: The value of collectCoverage from the Jest config.

Specifically enable or disable coverage for all test runs.

module.exports = {
  collectCoverage: true,
};

format

Default: ['table']

Specify the output format(s). The options are table and json.

module.exports = {
  format: ['table', 'json'],
};

outputFile

Default: undefined

A path to the JSON report (applies only when the format is json).

module.exports = {
  outputFile: './path/to/output.json',
};

server

Default: undefined

The server to consider for intercepted requests. By default we consider requests to 127.0.0.1 or localhost as being requests to your API. To target an alternative server you can do something like the following:

module.exports = {
  server: {
    hostname: 'example.com',
    port: 3000, // optional
  }
};

throwIfNoDocs

Default: false

Throw an error, rather than log a warning, if no docs were provided and therefore coverage could not be checked.

module.exports = {
  throwIfNoDocs: true,
};

Manual setup

If you already have Jest setup files that you want to reuse you can call the relevant jest-openapi-coverage functions from those files directly instead, for example:

globalSetup file

const { globalSetup } = require('jest-openapi-coverage');

module.exports = (globalSetup) => {
  globalSetup(globalSetup);
};

globalTeardown file

const { globalTeardown } = require('jest-openapi-coverage');

module.exports = () => {
  globalTeardown();
};

setupFilesAfterEnv file

const { requestInterceptor } = require('jest-openapi-coverage');

beforeAll(requestInterceptor.setup);
afterAll(requestInterceptor.teardown);