Provide a way to specify test environment setup file for Jest
See original GitHub issueMy 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 the test
command in package.json
, where ./localStoragePolyfill
looks like this:
const localStorageMock = (() => {
let store = {}
return {
getItem(key) {
return store[key]
},
setItem(key, value) {
store[key] = value.toString()
},
clear() {
store = {}
}
};
})()
global.localStorage = localStorageMock
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.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:4
- Comments:17 (16 by maintainers)
Top Results From Across the Web
Configuring Jest
Configuring Jest. The Jest philosophy is to work great by default, but sometimes you just need more configuration power.
Read more >Configuring package.json · Jest
Configuring package.json. Jest's configuration can be defined in the package.json file of your project or through the --config <path/to/json> option.
Read more >Using .env files for unit testing with jest - Stack Overflow
if you are like me trying to find a way to change the dotenv config path to use .env.test instead of .env ,...
Read more >Run/Debug Configuration: Jest | IntelliJ IDEA Documentation
In this area, specify the tests to be executed. The available options are: All tests: choose this option to run all the tests...
Read more >Unit and snapshot test initial setup and configuration - IBM
Use the provided files to easily configure a default Jest and Enzyme test environment that you can use to start writing your unit...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This makes sense. I propose the following: if
src/setupTests.js
exists 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?
Thanks for fixing this!