You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import"@testing-library/jest-dom";import{expect,afterEach,beforeAll,afterAll}from"vitest";import*asmatchersfrom"@testing-library/jest-dom/matchers";import{cleanup}from"@testing-library/react";import{server}from"./mocks/server";expect.extend(matchers);afterEach(()=>{cleanup();});// Start the MSW server before all testsbeforeAll(()=>{console.log("listening****");server.listen({onUnhandledRequest: "error",});});// Reset handlers after each test to ensure no tests are affected by previous onesafterEach(()=>{console.log("resetting****");server.resetHandlers();});// Clean up and close the server after all testsafterAll(()=>server.close());Object.defineProperty(window,"scrollTo",{writable: true,value: vi.fn(),});
Component where I am not getting the data is being rendered from root using the useNavigate() hook from react router dom v6
exportconstMyComp=()=>{constnavigate=useNavigate();const{ isLoading, data }=useGetDataQuery({page: 1,pageSize: 50,});useEffect(()=>{console.log("myComp mounted");},[]);console.log("partnersList ****",data);console.log("isLoading ****",isLoading);if(isLoading){return<FullPageLoader/>;
}if(!isLoading&&!partnersList){toast.error("Something went wrong");returnnull;}return(<div>***</div>)
Test file,
importAppfrom"./App";vi.mock("jwt-decode");beforeEach(()=>{vi.clearAllMocks();});test("renders the app component - logged in user",async()=>{constdecodedToken={exp: Math.floor(Date.now()/1000)+1000};vi.mocked(jwtDecode).mockImplementation(()=>decodedToken);const{ debug }=renderWithProviders(<App/>, {
initialEntries: [`/?token=some-token`],// my home route});awaitwaitFor(()=>{console.log(debug());expect(true).toBe(true);});});
Now i can the logs from the response that it's being sent and also logs from useEffect that component is mounted, But I am unable to receive the mocked response in the component.
Please assist if I am missing anything in the setup or the configuration.
The text was updated successfully, but these errors were encountered:
Hi, @mohit-92. Could you please share a reproduction repository with your issue? I appreciate extensive code snippets, but unless I can run the code, there's little I can do.
As a rule of thumb, if something doesn't work as expected in MSW, follow the Debugging runbook. It's designed as a step-by-step guide to walk you through the most common mistakes people make when using MSW. Once you do that, please share your results. Thanks.
No sorry I haven't found the root cause for this issue. I will be creating a repo to reproduce this issue, but for now we have switched from msw to playwright for testing.
Hello,
I am trying to mock the API's using MSW v2 but somehow I am unable to retrieve the mocked response back in my component. Below are the details,
Redux toolkit query,
Taken from redux toolkit documentation
Store setup is like this,
Test renderer picked from Redux toolkit documentation
MSW setup,
Handler.ts file
Setup test file
Component where I am not getting the data is being rendered from root using the useNavigate() hook from react router dom v6
Test file,
Vite config
Now i can the logs from the response that it's being sent and also logs from useEffect that component is mounted, But I am unable to receive the mocked response in the component.
Please assist if I am missing anything in the setup or the configuration.
The text was updated successfully, but these errors were encountered: