Open zaicevas opened 3 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.
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.)
🐛 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.
window.fetch
does not work (the call is not mocked). MockingglobalThis.fetch
works, but causes an error. Locally, mockingwindow.fetch
does workuseEffect
runs twice in the tests (probably due to strict mode?). For instance, if I have auseEffect(() => fetch(), [])
theexpect(fetch).toBeCalledTimes(2)
will return true, while locally is not the case.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:
jest.spyOn(window, 'fetch')
orjest.spyOn(global, 'fetch')
does not mock thefetch()
call inApp.jsx
expect(fetchMock).toBeCalledTimes(1);
assertion failsLink to sandbox: link (optional)
Your Environment