Jest SecurityError: localStorage is not available for opaque origins

前端 未结 12 516
礼貌的吻别
礼貌的吻别 2021-01-30 19:24

When I want to run my project with the command npm run test, I get the error below. What is causing this?

FAIL
● Test suite failed to run

SecurityE         


        
相关标签:
12条回答
  • 2021-01-30 19:30

    You do not need to do anything while working with React JS. It is default behavior of the create-react-app to place JEST and setting up testing environment. On running of below,it start showing the test success or fail,

    npm test

    In case you want test coverage, you simply need to add below to package.json file

    "test": "react-scripts test --coverage" Now your "script" of package.json look like,

    "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --coverage",
    "eject": "react-scripts eject"
    

    }

    0 讨论(0)
  • 2021-01-30 19:33

    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/",
    
        // ...
      }]
    }
    
    0 讨论(0)
  • 2021-01-30 19:33

    This may sound silly, but for me, the problem was caused because I had mistakenly installed random packages with npm update. I was running npm install and then npm update but I should have only ran npm install. I fixed the problem by deleting node_modules directory and running npm install again.

    0 讨论(0)
  • 2021-01-30 19:34

    If you are using jsdom, make sure you include url.

    Checkout jsdom repository simple options. https://github.com/jsdom/jsdom#simple-options

    const jsdom = require("jsdom");
    const { JSDOM } = jsdom;
    
    const dom = new JSDOM(`...`, { url: "https://example.org/" });
    
    0 讨论(0)
  • 2021-01-30 19:34

    This popped up with me when integrating enzyme with jest. The Github Issue Discussion suggests the same as noted above, namely adding

    "testURL": "http://localhost/"
    

    to the jest config. However, it's good to know that this is triggered also by enzyme. For me it was React v15 and the v15 adapter.

    0 讨论(0)
  • 2021-01-30 19:36

    I have the same issue and placing this in my package.json file worked for me:

    "jest": {
        "verbose": true,
        "testURL": "http://localhost/"
    } 
    
    0 讨论(0)
提交回复
热议问题