Jest SecurityError: localStorage is not available for opaque origins
Solution 1:
In case, if you are accessing your application with a http://localhost
prefix, you need to update your jest configuration (in your jest.config.js
) as,
"jest": {
"verbose": true,
"testURL": "http://localhost/"
}
In case you do not already have any jest configuration, just include the configuration in your package.json
. For example:
{
"name": "...",
"description": "...",
...
"jest": {
"verbose": true,
"testURL": "http://localhost/"
}
}
or in jest.config.js
:
module.exports = {
verbose: true,
testURL: "http://localhost/",
...
}
or if you have projects
configured:
module.exports = {
verbose: true,
projects: [{
runner: 'jest-runner',
testURL: "http://localhost/",
// ...
}]
}
Solution 2:
I just had this cropping up in a large monorepo (in unit tests, that otherwise wouldn't have required jsdom). Explicitly setting the following in our jest.config.js
(or the package.json
equivalent) also alleviated that issue:
module.exports = {
testEnvironment: 'node'
}
Update: As Nicolas mentioned below (thanks!), you can also add the following flags if you're not using any config files:
jest --testEnvironment node
# or
jest --env=node