Intercepting requests from another package inside a monorepo #1296
Replies: 4 comments 12 replies
|
Hey, @alessandrojcm. I'm sorry that you're struggling to make this work. Let's solve this together. First, what puts me off is this sentence:
If you have
I lean towards the second being the case here. I don't think it's a Turborepo problem, as far as I can see it's not affecting the tests in any way. I'd have to clone the repo and debug it to provide you with additional detail. |
|
Ok so after much tinkering I finally hit something. If I override the global In my import { server } from './mocks/server';
import { afterAll, afterEach, beforeAll } from 'vitest';
import fetch from 'cross-fetch';
// This argument sends an object thus fetch fails for some reason, I need to explicitly call url
global.fetch = (req) => {
return fetch(req.url);
};
beforeAll(() => {
server.listen({onUnhandledRequest: 'error'});
});
afterAll(() => server.close());
afterEach(() => server.resetHandlers());Now this obviously does not feel right, but I am not sure what to do, should I open I ticket? |
|
I've been fighting this issue today too! Helper library in a monorepo which presents a unified service API client, and the calls are not being caught. Versions: |
|
It seems forcedly overwriting import fetch, { Headers, Request, Response } from "node-fetch";
import { afterAll, afterEach, beforeAll } from "vitest";
import { server } from "./mocks/server";
// Forcedly overwrite `globalThis.fetch` to enable msw
// https://github.com/node-fetch/node-fetch#providing-global-access
// https://github.com/mswjs/msw/discussions/1296
const globalThisAny: any = globalThis;
globalThisAny.fetch = fetch;
globalThisAny.Headers = Headers;
globalThisAny.Request = Request;
globalThisAny.Response = Response;
beforeAll(() => {
server.listen({
onUnhandledRequest: "error",
});
});
afterAll(() => server.close());
afterEach(() => server.resetHandlers()); |
Uh oh!
There was an error while loading. Please reload this page.
Hi there!
I've been banging my head against the keyboard for the past day or so trying to get this resolved, I'm pretty sure I am missing some configuration step but I cannot seem to find what it is. My problem is as follows:
I have a monorepo using Turbo and yarn workspaces, pretty much a carbon copy of this Turborepo example. Now, in my
appsfolder I have a React app that uses Vite and Vitest as per their example. Then, in mypackagesfolder I have a library (let's call itcommon) that is supposed to abstract certain common HTTP calls that are common to the react applications (we'll have around three of them). This package usesky(and thus usesfetch).My problem is that, when I try to write tests on my react app using
msw,mswfails to intercept the HTTP calls made by thiscommonpackage. There's no error in the console (I haveonUnhandledRequestset toerror), MSW just does not intercept the call. But, if I write the HTTP call directly in the app usingfetch(ie without importing fromcommon); the test passes, thus making me think I am missing some configuration steps. I have used MSW in the past on non-monorepo packages and I have been able to intercept requests from third-party dependencies, so I am not sure what would be different here.Please check my reproduction repo to see the problem.
I'm not sure if this is a MSW, Vite, or Turbo problem; so feel free to point me in the right direction if you think it is not.
Thanks.
All reactions