npm run test fails with Jest - JSDom 11.12 causes SecurityError: localStorage is not available for opaque origin

(2500 Views)


Whenever you run your project with the command npm run test, You get the below error. So how to solve this Issue

FAIL Test suite failed to run SecurityError: localStorage is not available for opaque origins at Window.get localStorage [as localStorage] (node_modules/jsdom/lib/jsdom/browser/Window.js:257:15) at Array.forEach (<anonymous>)

Solution

If you are trying to access your application with just http://localhost prefix, you will have to update your jest configuration file i.e. jest.config.js as,

"jest": { "verbose": true, "testURL": "http://localhost/" }
If you don't have jest.config.js file, you can add below lines to your package.json file.
{ "name": "...", "description": "...", ... "jest": { "verbose": true, "testURL": "http://localhost/" } }
If you have projects configured in your jest.config.js file then,
module.exports = { verbose: true, projects: [{ runner: 'jest-runner', testURL: "http://localhost/", // ... }] }
If you are still facing the issue you can try adding below line to jest.config.js file,
module.exports = { testEnvironment: 'node' }

Update - 8th August, 2018

If you're not using any config files, then try below lines
jest --testEnvironment node # or jest --env=node
        

Solution Didn't Worked 0 DownvotesDownvote



Comments



Search
Play 2048 Game Online

Search Tags