codesandbox / codesandbox/codesandbox-client

CRA - different test behaviour

Open
#5,603 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

🃏 Jest
Dominant language
JavaScript
Stars
13.6k
Forks
2.4k
Avg merge
6d 19h
Merged PRs (30d)
2

Description

🐛 bug report

Preflight Checklist

  • I have read the
    Contributing Guidelines
    for this project.
  • I agree to follow the
    Code of Conduct
    that this project adheres to.
  • I have searched the issue tracker for an issue that matches the one I want
    to file, without success.

Description of the problem

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

  • Mocking window.fetch does not work (the call is not mocked). Mocking globalThis.fetch works, but causes an error. Locally, mocking window.fetch does work
  • useEffect runs twice in the tests (probably due to strict mode?). For instance, if I have a useEffect(() => fetch(), []) the expect(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:

  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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked sandbox's src/App.jsx and src/App.test.js, focusing on the fetch mocks and the useEffect call count described in the report. Compare the sandbox test behavior with CRA's local behavior; done means fetch mocking works consistently and the effect is invoked the expected number of times.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.