pact-msw
v1.1.0
Published
A Library to build Pact interactions by leveraging msw
Downloads
3
Readme
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
- Github: @ludorival
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