question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: why I cannot see my request even cannot see it x-powered-by: msw

See original GitHub issue

Environment

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:closed
  • Created 3 years ago
  • Comments:15 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
marcosvega91commented, Dec 2, 2020

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 function

return res(ctx.status(200),ctx.json(allRecipes))
2reactions
timdeschryvercommented, Dec 2, 2020

I do think there’s something wrong with the code, does it render correctly in the browser? The mocked response has the following structure:

{ 
  "allRecipes": {...}
}

I’m not fluent in react, but I suppose you have to “grab” allRecipes from the response body before invoking Object.entries. E.g.:

Object.entries(recipes.allRecipes)
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found