Skip to content

Commit

Permalink
Merge pull request #16 from lifeomic/expose-mocker
Browse files Browse the repository at this point in the history
feat: expose createAPIMocker utility
  • Loading branch information
swain authored Oct 25, 2022
2 parents 084abd4 + f157162 commit 9fd8d0b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,39 @@ test('something', () => {
});
```
### `createAPIMocker(...)`
This lower-level function is useful for creating a mocking utility if any of the following are true:
- You are not using `jest` as a test runner
- You need to mock more than one API
If none of the above are true, use `createAPIMockingUtility(...)`.
```typescript
import { setupServer } from 'msw/node';

// We recommend building your own utility, like this:
export const useAPIMocking = () => {
const server = setupServer();

server.listen({ onUnhandledRequest: 'error' });

// Specify your custom "APIEndpoints" type as the generic parameter here.
const mocker = createAPIMocker<APIEndpoints>(server, baseUrl);

beforeEach(() => {
mocker.reset();
});

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

return mocker;
};
```
### `mock(route, mocker)`
Mocks the specified route with the specified mocker _persistently_. The provided mock response will be used indefinitely until it is removed or replaced.
Expand Down
2 changes: 1 addition & 1 deletion src/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export type APIMocker<Endpoints extends RoughEndpoints> = {
reset: () => void;
};

const createAPIMocker = <Endpoints extends RoughEndpoints>(
export const createAPIMocker = <Endpoints extends RoughEndpoints>(
server: SetupServerApi,
baseUrl: string,
): APIMocker<Endpoints> => {
Expand Down

0 comments on commit 9fd8d0b

Please sign in to comment.