Open chiahao opened 2 months ago
Hi, @chiahao. Thanks for reporting this.
Can you please follow the Debugging runbook and let me know how far you got?
Hi, @kettanaito . Thanks for so fast response.
Modified browser.js
:
import { setupWorker } from "msw/browser";
import { handlers } from "./handler";
export const worker = setupWorker(...handlers);
worker.events.on('request:start', async ({ request }) => {
// Read the request body as text for every request
// that occurs in your application.
const payload = await request.clone().text()
});
worker.events.on('request:start', ({ request }) => {
console.log('Outgoing:', request.method, request.url)
});
Test the version where the homepage
is not set. The request is intercepted and shown in the console:
But still, once homepage
is set, the request is not intercepted:
Hi, @kettanaito , does this test right?
Can you please await the worker.start() promise? That doesn't look right.
Hi, @kettanaito ,
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
async function enableMocking() {
if (process.env.NODE_ENV !== 'development') {
return;
}
const packageJson = await import("../package.json");
const { worker } = await import("./mocks/browser");
// `worker.start()` returns a Promise that resolves
// once the Service Worker is up and ready to intercept requests.
await worker.start({
serviceWorker: {
// Provide a custom worker script URL, taking
// the "homepage" into account.
url: `${packageJson.homepage}/mockServiceWorker.js`,
},
});
console.log(worker.listHandlers());
}
enableMocking().then(() => {
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
});
still no luck with await
.
But there is a warning:
Does this mean MSW not support custom project path? Thanks for any helping.
Prerequisites
Environment check
msw
versionBrowsers
Firefox, Safari
Reproduction repository
https://github.com/chiahao/mymswtestapp.git
Reproduction steps
Describe the bug
create-react-app with homepage setted causes MSW not to mock
Environment
react: "^18.3.1" msw: "^2.4.4" nodejs: 21.1.0
To Reproduce
in firefox, send request
http://localhost:3000/api/code/NAT/1
can be succefully intercepted.Add
"homepage": "myPage",
topackage.json
.Modify
mytestapp/index.js
:npm start
still can start server, and show[MSW] Mocking enabled.
.But requests won't intercepted.
Current behavior
Firefox get this:
Cannot POST /myPage/api/code/NAT/1
Expected behavior
"data": [ {"CODE": "11", "DESCR": "UNIT1"}, {"CODE": "22", "DESCR": "UNIT2"} ]