create-react-app: Documentation: missing instructions if setupTests.js is added after an eject

initializing-test-environment doesn’t explain that "setupTestFrameworkScriptFile": "<rootDir>/src/setupTests.js", needs to be added to package.json if src/setupTests.js is created after the app has been “ejected” as createJestConfig only adds this configuration if the file is present (makes sense).

Standard template being:

{
  …
  "jest": {
    "setupFiles": [
      …
    ],
    "setupTestFrameworkScriptFile": "<rootDir>/src/setupTests.js",

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 5
  • Comments: 16 (3 by maintainers)

Most upvoted comments

I have the same problem as Doug. src/setupTests.js and it doesn’t work. Yet if I place the code in the test files themselves, it works.

Documented in https://github.com/facebookincubator/create-react-app/commit/1c8a7f3fecc60cd1516ed3a28f274945de90b3db.

@dougbacelar Unfortunately we can’t help without a reproducing project.

Just happened to me, and this question was my solution. So 👍 for adding it into docs.

I ended up just adding the --setupFiles flag to the package.json test command:

react-scripts-ts test --env=jsdom --setupFiles ./src/setupTests.js

@diligiant Not at all, I wish them all the luck of the universe. The problem is: I’m not sure if there is a category of post-eject situations that we should cover on docs, while ignoring all the rest. The solution adopted on this project (correct me if I’m wrong) is to simply don’t cover any of them.

But maybe we can solve this case without docs: on eject create an empty setupTests.js if there isn’t one already, then add this configuration on package.json. What you think? Does this solve the problem?