@smartlyio/oats-nock-adapter
TypeScript icon, indicating that this package has built-in type declarations

7.4.7 • Public • Published

Nock server adapter for smartlyio/oats

Nock adapter for creating mock servers from openapi definitions using oats.

To mock the request POST /item define the route as for any oats server adapter and then bind the route defining object to the nock adapter. The bound mock servers are namespaced using the OpenApi server strings as nock basepaths so that multiple nock mocks can be used simultanenously so long as the basepaths differ. All of the mocked routes are persisted so any number of requests are served.

// yarn ts-node examples/example.ts
import * as nockAdapter from '../src/nock';
import * as api from '../tmp/client.types.generated';
import * as types from '../tmp/openapi.types.generated';
import * as runtime from '@smartlyio/oats-runtime';

nockAdapter.bind(api.createRouter(), {
  '/item': {
    post: async ctx => {
      return runtime.json(
        201,
        types.typeItem
          .maker({
            id: ctx.body.value.id + ' response',
            name: ctx.body.value.name
          })
          .success()
      );
    }
  }
});

The bind call is just a constructor for a Server class that provides a mock method for adding new mocked routes or overlaying new handlers on top of already mocked routes. If the overlaying mock throws Next the previous mock for the route is called instead.

// yarn ts-node examples/server.ts
import * as nockAdapter from '../src/nock';
import * as api from '../tmp/client.types.generated';
import * as types from '../tmp/openapi.types.generated';
import * as runtime from '@smartlyio/oats-runtime';

const server = nockAdapter.bind(api.createRouter(), {
  '/item': {
    post: async ctx => {
      return runtime.json(201, types.typeItem.maker(ctx.body.value).success());
    }
  }
});

server.mock({
  '/item': {
    post: async () => {
      throw new nockAdapter.Next();
    }
  }
});

Package Sidebar

Install

npm i @smartlyio/oats-nock-adapter

Weekly Downloads

0

Version

7.4.7

License

MIT

Unpacked Size

30.1 kB

Total Files

8

Last publish

Collaborators

  • smartlyio