React Testing Tutorial - 46 - Testing with MSW

preview_player
Показать описание

📱 Follow Codevolution

Testing with MSW
React Testing Tutorial
React Testing with Jest and React Testing Library
Рекомендации по теме
Комментарии
Автор

To Whoever facing issues with the mock response, please replace below in server.ts file inside mocks.
We should be using setUpServer, instead of setUpWorker.

The setUpServer is inside the msw/node.


import { setupServer } from "msw/node";
import { handlers } from "./handler";


export const server = setupServer(...handlers);

HariPrasad-qehd
Автор

Thank you very much for your great tutorials. I have a problem with "ReferenceError: fetch is not defined" . Does anyone have similar problem and know how to resolve it?

teodordimitrov
Автор

Hi,
How can I resolve this issue ? I am getting this error in my handlers.js file
Module '"msw"' has no exported member 'rest'.

user
Автор

hi Viswas, I am using React native here. But mock APIs are not calling when I run test cases. Can you please help me? I have done same setup as you. Please reply

srikanthracharla
Автор


@Vishwas or All,
I have one question with my scenario. I have User component where I need to derive/fetch data from fetch functions in my component in useEffect hook. I have separate file called UserAPIHelper where all rest calls are kept and functions are exposed as a pert of this component which are imported and called in useEffect hook wherever needed.
e.g fetchRoles is an function in UserAPIHelper which does API call here and in my User component I am importing this function and using it in useEffect to set roles state by calling fetchRoles function.
Now I am writing Jest test cases for it and struggling with mocking the function fetchRoles as its setting the state of user component hence I am not able to mock it. I have other functions too like fetchCountries and fetchUsers, and I cant change the current implementation/functionality.
Can someone help me here please ?

Thank you so much in Advance.

pradippokale
Автор

Hi Viswas, I have been following your tutorial, but the mock APIs are not getting called when I run test cases. I get the console warning message like: "[MSW] Expected response resolver to return a mocked response Object, but got undefined. The original response is going to be used instead."
Can you please help me? I have done same setup as you. Couldn't find any solution on the Internet. Please reply

kumarsaroj
Автор

but why my fail's reason was that not found the listItem before I imported the msw server in the setUpTests file?

xiaotingshao
Автор

I am facing error cannot read properties of undefined method

HabibUrRahman-muzf
Автор

I keep getting this error!! FAIL
● Users › renders a list of users

Unable to find role="listitem"

Ignored nodes: comments, script, style
<body>
<div>
<div>
<h1>
Users
</h1>
<p>
Error fetching users
</p>
<ul />
</div>
</div>
</body>

15 | test("renders a list of users", async () => {
16 | render(<Users />)
> 17 | const users = await
| ^
18 | expect(users).toHaveLength(3)
19 | })
20 |

at Object.<anonymous>

DaveJTake
Автор

Hi Guys, if anyone can help I am writing test cases with RTL in my office code but the coverage seena only 45% pf detail page where we have. Called all the other components. In the detail page component we have function which are helping us to convert API resoonse to the exact data that we want to pass to the component . It is giving msg error as these lines are uncovered. How can I write test cases for the functions ?

vaibhavshukla
Автор

i am getting error for axios i.e SyntaxError: Cannot use import statement outside a module. how can i resolve it?

swatijindal
Автор

My code is not working even in this same setup

satisfaction
join shbcf.ru