codesandbox / codesandbox-client

An online IDE for rapid web development
https://codesandbox.io
Other
12.97k stars 2.26k forks source link

CRA - different test behaviour #5603

Open zaicevas opened 3 years ago

zaicevas commented 3 years ago

🐛 bug report

Preflight Checklist

Description of the problem

I've been coding with CRA locally. After copy-pasting the code to codesandbox, I've encountered different test behaviour.

How has this issue affected you? What are you trying to accomplish?

It is frustrating that there are environment differences and that you can't control how tests are being run in codesandbox.

To Reproduce

In my provided sandbox, you can see the following observations:

  1. jest.spyOn(window, 'fetch') or jest.spyOn(global, 'fetch') does not mock the fetch() call in App.jsx
  2. expect(fetchMock).toBeCalledTimes(1); assertion fails

Link to sandbox: link (optional)

Your Environment

Software Name/Version
Сodesandbox
Browser Chrome
Operating System Mac OS X 10.15.7
github-actions[bot] commented 2 years ago

This issue has automatically been marked stale because there has been no activity in a while. Please leave a comment if the issue has not been resolved, or if it is not stale for any other reason. After 2 weeks, this issue will automatically be closed, unless a comment is made or the stale label is removed.

andreashouben commented 2 years ago

Could it be that the code on your local machine simply runs faster than in codesandbox?

If you wait for your expectation the test succeeds.

await waitFor(() => expect(screen.getByText('Fetched')).toBeInTheDocument())

(rember to import waitfor from testing-library/react.)