Jest SecurityError:localStorage不适用于不透明的起源 [英] Jest SecurityError: localStorage is not available for opaque origins

查看:743
本文介绍了Jest SecurityError:localStorage不适用于不透明的起源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想使用命令 npm run test 运行我的项目时,我收到以下错误。造成这种情况的原因是什么?

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

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>)


推荐答案

如果您使用<正在访问您的应用程序 http:// localhost 前缀,您需要更新您的jest配置(在 jest.config.js 中),因为,

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/"
  }

如果您还没有任何jest配置,只需在 package.json 中包含配置。例如:

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/"
  }
}

jest.config.js

module.exports = {
  verbose: true,
  testURL: "http://localhost/",
  ...
}

或者如果你有项目已配置:

module.exports = {
  verbose: true,

  projects: [{
    runner: 'jest-runner',
    testURL: "http://localhost/",

    // ...
  }]
}

这篇关于Jest SecurityError:localStorage不适用于不透明的起源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆