Jest SecurityError:localStorage不适用于不透明的来源

JavaScript Node.js

JinJin

2020-04-03

当我想使用命令运行项目时npm run test,出现以下错误。是什么原因造成的?

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

第4012篇《Jest SecurityError:localStorage不适用于不透明的来源》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

6个回答
逆天Gil 2020.04.03

使用React JS时,您不需要做任何事情。放置JEST和设置测试环境是create-react-app的默认行为。在下面的运行中,它开始显示测试成功或失败,

npm测试

如果您想要测试覆盖率,只需将以下内容添加到package.json文件中

“ test”:“ react-scripts test --coverage”现在您的package.json的“脚本”看起来像,

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

}

小胖 2020.04.03

与整合时enzyme这点对我突然弹出jestGithub上的问题探讨表明同上面提到的,即增加

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

开玩笑的配置。但是,很高兴知道这也是由酶触发的。对我来说是React v15和v15适配器。

飞云 2020.04.03

这听起来可能很愚蠢,但是对我来说,问题是由于我错误地使用安装了随机软件包而引起的npm update我当时正在跑步npm installnpm update但我只应该跑步npm install我通过删除node_modules目录并npm install再次运行来解决该问题

老丝 2020.04.03

对我来说,这是通过升级到“ jest”:“ ^ 24.9.0”解决的,

Stafan 2020.04.03

如果使用的是jsdom,请确保包含url。

检出jsdom存储库简单选项。https://github.com/jsdom/jsdom#simple-options

const jsdom = require("jsdom");
const { JSDOM } = jsdom;

const dom = new JSDOM(`...`, { url: "https://example.org/" });
Itachi 2020.04.03

您必须指定--env要使用的环境()。

在中运行jest命令时,package.json应指定环境(jsdomnode)。例如:

  "scripts": {
    "jest": "jest --env=node --colors --coverage test",
    "test": "npm run jest"
  },

这应该为您工作!

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android