-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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
Provide a way to specify test environment setup file for Jest #545
Comments
Is there any reason you can't do this manually by importing a file like this from your test? Is it because many tests rely on this? @cpojer What do you think is a reasonable solution? Should we come up with a conventional name for test setup file? |
I definitely don't want to add mocks for this in CRA because there are hundreds of browser APIs people might need to mock in different ways. This should be controlled by the user. |
Yeah, I think we should come up with a standard for CRA and if the file exists, it gets added to setupFiles, just like we talked about. For localStorage mocking, it probably needs a mock that uses ES proxies :) |
@gaearon I can indeed directly import the polyfill just before importing my file in the test. However, because Can we rely on the fact that |
Yes, order matters for ES imports. |
The thing is, tbh I'm happy to even support this directly in Jest. We could look for |
Is |
Yeah I don't think that will work well as an automatic default tbh. Especially since right now it would be run as a test itself. |
|
I don't want to make an assumption either about folder structure or whether to use camelcase or anything inside of their actual code. I'd be happy to support |
Yeah, I meant doing this in CRA config. Since CRA already asks |
Absolutely, feel free to make any call that you think is best for CRA users and decouple it from a similar setting in Jest we may want to add. |
Does Jest fail if passed file doesn’t exist? |
Yeah it'll fail. A silent error for a setup file wouldn't be very useful I think. |
This makes sense. I propose the following: if @gaelduplessix Would you like to submit a PR implementing this? |
Thanks for fixing this! |
My App is using
localStorage
but when I run tests, I get this error:ReferenceError: localStorage is not defined
Should we provide a polyfill to be able to mock it with Jest ?
Workaround
Add a
--setupTestFrameworkScriptFile ./localStoragePolyfill.js
to thetest
command inpackage.json
, where./localStoragePolyfill
looks like this:I guess it should be added to config/polyfills.js.
I can PR if that's fine. Not sure if we should use a simple inline polyfill or use something like
node-localstorage
though.The text was updated successfully, but these errors were encountered: