Skip to content

Commit

Permalink
Simplify and document MSW
Browse files Browse the repository at this point in the history
  • Loading branch information
djhi committed Jun 6, 2024
1 parent f96e328 commit b46a393
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Then configure it:
```js
// in ./src/msw.js
import { setupWorker } from "msw/browser";
import { getMswHandlers } from "fakerest";
import { getMswHandler } from "fakerest";

const data = {
'authors': [
Expand All @@ -41,7 +41,7 @@ const data = {
}
};

export const worker = setupWorker(...getMswHandlers({
export const worker = setupWorker(getMswHandler({
data
}));
```
Expand All @@ -54,7 +54,10 @@ import ReactDom from "react-dom";
import { App } from "./App";
import { worker } from "./msw";

worker.start().then(() => {
worker.start({
quiet: true, // Instruct MSW to not log requests in the console
onUnhandledRequest: 'bypass', // Instruct MSW to ignore requests we don't handle
}).then(() => {
ReactDom.render(<App />, document.getElementById("root"));
});
```
Expand Down Expand Up @@ -86,7 +89,7 @@ const data = {
const restServer = new MswServer();
restServer.init(data);

export const worker = setupWorker(...restServer.getHandlers());
export const worker = setupWorker(restServer.getHandler());
```

### Sinon
Expand Down
9 changes: 2 additions & 7 deletions example/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,8 @@ switch (import.meta.env.VITE_MOCK) {
break;
default:
import('./msw')
.then(({ worker, dataProvider }) => {
return worker
.start({
quiet: true,
onUnhandledRequest: 'bypass',
})
.then(() => dataProvider);
.then(({ initializeMsw, dataProvider }) => {
return initializeMsw().then(() => dataProvider);
})
.then((dataProvider) => {
ReactDom.render(
Expand Down
8 changes: 7 additions & 1 deletion example/msw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ restServer.addMiddleware(async (request, context, next) => {
return next(request, context);
});

export const worker = setupWorker(restServer.getHandler());
export const initializeMsw = async () => {
const worker = setupWorker(restServer.getHandler());
return worker.start({
quiet: true, // Instruct MSW to not log requests in the console
onUnhandledRequest: 'bypass', // Instruct MSW to ignore requests we don't handle
});
};

export const dataProvider = defaultDataProvider;

0 comments on commit b46a393

Please sign in to comment.