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

查看:17
本文介绍了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/",
  ...
}

或者如果你有 projects 配置:

or if you have projects configured:

module.exports = {
  verbose: true,

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

    // ...
  }]
}

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

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