Repository navigation
Provide a way to specify test environment setup file for Jest #545
Description
Activity
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
localStorageis in the global scope, I don't know if we can rely on the order in which the files are imported, I thought it was safer to import it using thesetupFilesoption.Can we rely on the fact that
importstatements are executed in order ? Is this a standard ?Yes, order matters for ES imports.
The thing is,
--setupTestFrameworkScriptFileshould really be deprecated or renamed and we should either expose--setupFile=<path>(which can be array arguments?) to add new setup files or do what @gaearon said and look for a setup file in a standard location.tbh I'm happy to even support this directly in Jest. We could look for
<rootDir>/.jest-setup.js.Is
src/index.test.jsgoing to be confusing? I’m asking because we already havesrc/index.jsas required file so it makes sense to “anchor” test setup around it.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.
src/setupTests.jsI 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
jest-setup.jsor.jest-setup.jsin the root automatically. I think anything else I'd recommend you to do in CRA on top :)Yeah, I meant doing this in CRA config. Since CRA already asks
src/index.jsto exist we can have more conventional names here, as this makes sense given nature of the project.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.
Reacted by danThis makes sense. I propose the following: if
src/setupTests.jsexists it should be used, otherwise we don’t pass it. This logic would live increateJestConfig.js. Path itself would be determined inpaths.js.@gaelduplessix Would you like to submit a PR implementing this?
Reacted by Gael Ollivier, Drew Dennison, HiiTea and Manas Bajaj2 remaining items
- changed the title
[-]localStorage polyfill[/-][+]Provide a way to specify test environment setup file for Jest[/+]on Sep 2, 2016 - added a commit that references this issue
on Sep 2, 2016 Thanks for fixing this!
Reacted by Gael Ollivier, Dinos Theodorou and Simon Taranto- added a commit that references this issue
on Sep 7, 2016 - added a commit that references this issue
on Oct 25, 2016 - locked and limited conversation to collaborators
on Jan 22, 2019
My App is using
localStoragebut when I run tests, I get this error:ReferenceError: localStorage is not definedShould we provide a polyfill to be able to mock it with Jest ?
Workaround
Add a
--setupTestFrameworkScriptFile ./localStoragePolyfill.jsto thetestcommand inpackage.json, where./localStoragePolyfilllooks 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-localstoragethough.