Question: why I cannot see my request even cannot see it x-powered-by: msw
See original GitHub issueEnvironment
Name | Version |
---|---|
msw | 0.24.0 |
browser | chrome |
OS | MacOS 11.0.1 |
Request handlers
const allRecipes = {
"0": {
"id": "1",
"text": "burger"
},
"1": {
"id": "2",
"text": "fish"
},
"2": {
"id": "3",
"text": "pizza"
},
"3": {
"id": "4",
"text": "lazagne"
}
}
const server = setupServer(
rest.get('http://myurl/receipes', (_, res, ctx) => {
ctx.status(200)
return res(ctx.json({ allRecipes }))
})
)
beforeAll(() => server.listen())
afterAll(() => server.close())
test('fetches and displays all receipes', async () => {
render(<Recipes />)
const listItems = await screen.findAllByRole('listitem')
expect(listItems).toHaveLength(8)
expect(listItems[0]).toHaveTextContent('burger')
expect(listItems[1]).toHaveTextContent('fish')
expect(listItems[2]).toHaveTextContent('pizza')
})
Actual request: Recipes.tsx
export default function Recipes() {
const [recipes, setRecipes] = useState({})
const fetchAllRecipes = async () => {
const response = await fetch('http://myurl/receipes')
if (response.ok) {
const jsonResponse = await response.json()
setRecipes(jsonResponse)
}
}
useEffect(() => {
fetchAllRecipes()
}, [])
return (
<div>
<ul>
{Object.entries(recipes).map((element: any) => (
<li>{element[1].id}</li>
<li> {element[1].text}</li>
))}
</ul>
</div>
Current behavior
When I run the test it works for others but for the current one it fails to test it despite that I have added whatwg-fetch
dependency for using node to fetch the request but somehow it cannot render the element and it shows this result:
● fetches and displays all receipes
expect(received).toHaveLength(expected)
Expected length: 8
Received length: 2
Received array: [<li />, <li />]
46 |
47 | const listItems = await screen.findAllByRole('listitem')
> 48 | expect(listItems).toHaveLength(8)
also in the browser it’s not showing that it is x-powered-by: msw but instead it is showing x-powered-by: Express
Expected behavior
The expected behavior is to have a length of the recipes as 8 but its not showing them
So is there something wrong with it or it me who is configuring things incorrectly? PS: I wanted to give a thank about the package as it is super helpful and super powerful and I love it!
Issue Analytics
- State:
- Created 3 years ago
- Comments:15 (10 by maintainers)
Top Results From Across the Web
Debugging uncaught requests - Recipes - Mock Service Worker
If you've confirmed that the worker's scope cannot capture the requests your application is making, there are a few ways to address that....
Read more >Mocking response with msw - reactjs - Stack Overflow
I am working on an application in next.js. I have a server running that is responsible for tokenizing data, which is then displayed...
Read more >4. Finding and Adding Friends - Facebook - O'Reilly
If Facebook finds members who match the email addresses in your address book, it displays them; if it doesn't find any matches, it...
Read more >Cannot receive friend request - Meta Community Forums
Hello! We see you're having issues getting friend requests. It's no fun playing alone much, so let's try and find out what the...
Read more >20 Interview Questions Every Social Worker Needs to Know
Why have you selected a career in social work? How is your previous experience applicable to the work we do here? Have you...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I understand the issue, I didn’t see before. @timdeschryver has right you should do has he suggested or you can return directly the object. There is also another issue, the status should be inside
res
functionI do think there’s something wrong with the code, does it render correctly in the browser? The mocked response has the following structure:
I’m not fluent in react, but I suppose you have to “grab”
allRecipes
from the response body before invokingObject.entries
. E.g.: