Skip to content

Commit

Permalink
chore: support msw v2 (#128)
Browse files Browse the repository at this point in the history
* chore: support msw v2

BREAKING CHANGE
mswInspector.getRequests returns a promise

* docs: update changelog
  • Loading branch information
toomuchdesign authored Dec 27, 2023
1 parent de69ad4 commit 394905d
Show file tree
Hide file tree
Showing 12 changed files with 319 additions and 660 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 3.0.0

### Breaking change

- Support `msw` v2+
- `mswInspector.getRequests` method switched from sync to async

# 2.0.0

### Breaking change
Expand Down
35 changes: 17 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,20 @@ describe('My test', () => {
it('My test', async () => {
// Perform your tests

expect(mswInspector.getRequests('http://my.url/path')).toHaveBeenCalledWith(
{
method: 'GET',
headers: {
'my-header': 'value',
},
body: {
'my-body': 'value',
},
query: {
'my-query': 'value',
},
expect(
await mswInspector.getRequests('http://my.url/path'),
).toHaveBeenCalledWith({
method: 'GET',
headers: {
'my-header': 'value',
},
);
body: {
'my-body': 'value',
},
query: {
'my-query': 'value',
},
});
});
});
```
Expand Down Expand Up @@ -112,7 +112,7 @@ createMSWInspector({

### `getRequests`

Returns a mocked function pre-called with all the request records whose absolute url match the provided one.
Returns a promise returning a mocked function pre-called with all the request records whose absolute url match the provided one.

The matching url can be provided as:

Expand All @@ -121,10 +121,10 @@ The matching url can be provided as:

```ts
// Full string match
mswInspector.getRequests('http://my.url/path/foo');
await mswInspector.getRequests('http://my.url/path/foo');

// Url matching patter
mswInspector.getRequests('http://my.url/path/:param');
await mswInspector.getRequests('http://my.url/path/:param');
```

By default, each matching request results into a mocked function call with the following request log record:
Expand Down Expand Up @@ -163,15 +163,14 @@ const mswInspector = createMSWInspector({
`getRequests` accepts an optional options object

```ts
mswInspector.getRequests(string, {
await mswInspector.getRequests(string, {
debug: boolean, // Throw debug error when no matching requests found (default: true)
});
```

## Todo

- Consider listening to network layer with [`@mswjs/interceptors`](https://github.com/mswjs/interceptors) and make MSW inspector usable in non-`msw` projects
- Todo find out why `SetupServer | SetupWorker` union causes a type error in lifecycle events
- Consider optionally returning requests not intercepted by `msw` (`request:start`/ `request:match`)

[ci-badge]: https://github.com/toomuchdesign/msw-inspector/actions/workflows/ci.yml/badge.svg
Expand Down
Loading

0 comments on commit 394905d

Please sign in to comment.