Skip to content

Ability to detect untested paths #260

@mefellows

Description

@mefellows

Are you using OpenAPI 2, 3.0.X, or 3.1.0?

3.0.0

Would this solve a problem or make something easier?

Solves a problem.

What would you like to happen?

Checking that your code is compatible with a schema is helpful, but most people I'd argue are looking to say "I have a level of confidence that my code implements the schema (not just part of it)". At the moment, there is no way to know that all paths in a schema have been tested.

An example to illuminate the problem.

Given this OAS file:

openapi: 3.0.0
info:
  title: Example API
  version: 1.0.0
paths:
  /example:
    get:
      responses:
        200:
          description: Response body should be an object with fields 'stringProperty' and 'integerProperty'
          content:
            application/json:
              schema:
                type: object
                required:
                  - stringProperty
                  - integerProperty
                properties:
                  stringProperty:
                    type: string
                  integerProperty:
                    type: integer
        404:
          description: Not found
          content: {}

and this test:

// Import this plugin
import jestOpenAPI from 'jest-openapi';

// Load an OpenAPI file (YAML or JSON) into this plugin
jestOpenAPI('path/to/openapi.yml');

// Write your test
describe('GET /example/endpoint', () => {
  it('should satisfy OpenAPI spec', async () => {
    // Get an HTTP response from your server (e.g. using axios)
    const res = await axios.get('http://localhost:3000/example/endpoint');

    expect(res.status).toEqual(200);

    // Assert that the HTTP response satisfies the OpenAPI spec
    expect(res).toSatisfyApiSpec();
  });
});

You might have the false sense of security that your code implements the specification, but of course the 404 path is untested.

A method such as expectOASIsSatisfied() (or something much better) that checks all the main paths have been tested at least once would be a great place to start.

It's probably going to be impossible to prove all correct paths have been tested (if you consider polymorphic payloads such as oneOf or anyOf), but it should be fairly easy to check if all of the main paths + response code pairings have at least been tested once.

Describe alternatives you've considered

Tools like Dredd and Optic will give you a level of coverage

Additional context or screenshots

n/a

Are you going to resolve the issue?

I'd be open to a PR if I could get a steer on if this is worth doing and possibly where to start.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions