pact-msw
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

Welcome to pact-msw 👋

Version Documentation Maintenance License: BSD--3--Clause

pact-msw

pact-msw is a Node.js library that allows you to build Pact contracts by leveraging msw. This library provides an easy way to generate contracts that can be used for testing and verifying API interactions between consumer and provider.

Install

yarn add -D pact-msw

Usage

Here is an example of how to use pact-msw:

import { setupServer } from 'msw/node';
import { pactProvider, rest } from 'pact-msw';

const server = setupServer();

const provider = pactProvider({
  consumer: 'test-consumer',
  provider: 'test-provider',
});

provider.removePact();

beforeAll(() => {
  server.listen();
  provider.loadPact();
});

afterEach(() => {
  server.resetHandlers();
});

afterAll(() => {
  server.close();
  provider.writePact();
});

it('get all movies', async () => {
  const mockMovies = rest({
    description: 'a request to list all movies',
    method: 'get',
    path: '*/movies',
    response: [
      {
        id: 1,
        name: 'Movie 1',
        year: 2008,
      },
      {
        id: 2,
        name: 'Movie 2',
        year: 2008,
      },
    ],
  });
  server.use(...provider.toHandlers(mockMovies));

  const movies = await fetchMovies();

  expect(movies).toEqual(mockMovies.response);
});

You can find more example to mock

Author

👤 Ludovic Dorival

Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2021 Ludovic Dorival.
This project is BSD--3--Clause licensed.


This README was generated with ❤️ by readme-md-generator

Package Sidebar

Install

npm i pact-msw

Weekly Downloads

1

Version

1.1.0

License

BSD-3-Clause

Unpacked Size

39.3 kB

Total Files

25

Last publish

Collaborators

  • ludorival