Open RayOei opened 9 months ago
In the failing test can you try to await 0ms timeout?
I am having same issue in react app and wondering if this is something similar, for me doing await for 0ms worked:
export const wait = (ms: number) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
it('shows error if api failed', async () => {
server.use(
http.get('/donation-settings', () => {
return HttpResponse.json({}, { status: 404 });
}),
);
await wait(0);
const contentText = 'Content';
render(
<Container>
{({ currentSettings }) => (
<div>
{contentText}
{JSON.stringify(currentSettings)}
</div>
)}
</Container>,
);
const error = await screen.findByTestId('error');
const content = screen.queryByText(contentText);
expect(error).toBeVisible();
expect(content).not.toBeInTheDocument();
});
without waiting it fails
The await doesn't help me in the RN app described by @RayOei
I can't say that this is a problem, but I noticed that the initial array of handlers is processed by the init() function (see code in screenshot). However, a handler added with use() is not processed in this way. They are only added to the beginning of the handlers array and that's all.
@RayOei @andrew-tv Did either of you manage to find a workaround for this? I'm also seeing the same behaviour with the latest versions of MSW + Detox with a React Native app. Initial handlers work just fine but attempting to add handlers as part of a test seem to be ignored.
Prerequisites
Environment check
msw
versionNode.js version
21.6.2
Reproduction repository
https://github.com/RayOei/MSW-test/tree/main
Reproduction steps
The repo contains a simple RN app (RN v0.73.4) with Detox (v20.18.1) tests which illustrates the expectations.
For details see the README.md in the repo.
Edit: some additional observations have been added regarding handling of intercepted requests. Also the demo app has been tweaked to help show more clearly what the behaviour is
Current behavior
The first test using the global handler passes: request is intercepted and expected response is send & received. The test which defines the
server.use
fails, however, as the normal handler is executed instead of this particular one.Logging show the intercept log: