Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest configuration support for "setupFiles" #9706

Open
LordLalwani opened this issue Sep 26, 2020 · 2 comments
Open

Jest configuration support for "setupFiles" #9706

LordLalwani opened this issue Sep 26, 2020 · 2 comments

Comments

@LordLalwani
Copy link

Is your proposal related to a problem?

I'm trying to test localStorage with https://www.npmjs.com/package/jest-localstorage-mock which requires some minor configuration setup in the package.json however when I try to add the following to my package.json

  "jest": {
    "setupFiles": ["jest-localstorage-mock"]
  }

I get the following error message:

Out of the box, Create React App only supports overriding these Jest options:

  • clearMocks
  • collectCoverageFrom
  • coveragePathIgnorePatterns
  • coverageReporters
  • coverageThreshold
  • displayName
  • extraGlobals
  • globalSetup
  • globalTeardown
  • moduleNameMapper
  • resetMocks
  • resetModules
  • restoreMocks
  • snapshotSerializers
  • transform
  • transformIgnorePatterns
  • watchPathIgnorePatterns.

These options in your package.json Jest configuration are not currently supported by Create React App:

  • setupFiles

If you wish to override other Jest options, you need to eject from the default setup. You can do so by running npm run eject but remember that this is a one-way operation. You may also file an issue with Create React App to discuss supporting more options out of the box.

error Command failed with exit code 1.

Describe the solution you'd like

I'f possible, I'd like out of the box support for this as I don't want to run npm run eject

Describe alternatives you've considered

(Write your answer here.)

Additional context

https://www.npmjs.com/package/jest-localstorage-mock
https://jestjs.io/docs/en/configuration.html#content

(Write your answer here.)

@LordLalwani
Copy link
Author

If this is an easy fix, i'd like to contribute it myself to the create-react-app project if someone can just point me to previous config or relevant document =)

@giovannibenussi
Copy link

jest-localstorage-mock provides a solution for this:

For a create-react-app project you can replace the suggested mock
with this at the beginning of the existing src/setupTests.js file:

require('jest-localstorage-mock');

Why does this work?

create-react-app automatically executes the content in src/setupFiles.js if it’s created.

B8F1B58D-341D-44A2-8231-40162AB6D99D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants