Javascript Jest SecurityError:localStorage 不适用于不透明来源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51554366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jest SecurityError: localStorage is not available for opaque origins
提问by amirdehghan
When I want to run my project with the command npm run test, I get the error below. What is causing this?
当我想使用命令运行我的项目时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>)
回答by David R
In case, if you are accessing your application with a http://localhostprefix, you need to update your jest configuration (in your jest.config.js) as,
如果您使用http://localhost前缀访问您的应用程序,则需要将您的 jest 配置(在您的 中jest.config.js)更新为,
"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:
如果您还没有任何 jest 配置,只需在您的package.json. 例如:
{
"name": "...",
"description": "...",
...
"jest": {
"verbose": true,
"testURL": "http://localhost/"
}
}
or in jest.config.js:
或在jest.config.js:
module.exports = {
verbose: true,
testURL: "http://localhost/",
...
}
or if you have projectsconfigured:
或者如果您已projects配置:
module.exports = {
verbose: true,
projects: [{
runner: 'jest-runner',
testURL: "http://localhost/",
// ...
}]
}
回答by Burgi
I just had this cropping up in a large monorepo (in unit tests, that otherwise wouldn't have required jsdom). Explicitly setting the following in our jest.config.js(or the package.jsonequivalent) also alleviated that issue:
我刚刚在一个大型 monorepo 中出现了这个(在单元测试中,否则就不需要 jsdom)。在我们的jest.config.js(或package.json等效的)中明确设置以下内容也缓解了这个问题:
module.exports = {
testEnvironment: 'node'
}
Update: As Nicolas mentioned below (thanks!), you can also add the following flags if you're not using any config files:
更新:正如下面提到的 Nicolas(谢谢!),如果您不使用任何配置文件,您还可以添加以下标志:
jest --testEnvironment node
# or
jest --env=node
回答by Manuel Morejón
You must specify what environment (--env) are you going to use.
您必须指定--env要使用的环境 ( )。
When you run jestcommand in the package.jsonyou should specify the environment (jsdomor node). For example:
当您在 中运行jest命令时,package.json您应该指定环境(jsdom或node)。例如:
"scripts": {
"jest": "jest --env=node --colors --coverage test",
"test": "npm run jest"
},
This should work for you!
这应该对你有用!
回答by p8ul
If you are using jsdom, make sure you include url.
如果您使用的是 jsdom,请确保包含 url。
Checkout jsdom repository simple options. https://github.com/jsdom/jsdom#simple-options
签出 jsdom 存储库简单选项。https://github.com/jsdom/jsdom#simple-options
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const dom = new JSDOM(`...`, { url: "https://example.org/" });
回答by Scott Martin
The suggestion in the top-rated answer of adding testURL: "http://localhost"to my Jest config didn't work for me. However, this suggestion from the jsdom GitHub discussion, of passing a URL in when creating the jsdom object, did.
添加testURL: "http://localhost"到我的 Jest 配置的最受好评的答案中的建议对我不起作用。然而,来自 jsdom GitHub 讨论的这个建议,即在创建 jsdom 对象时传入一个 URL,确实如此。
const url = 'http://localhost';
const dom = new JSDOM('<!DOCTYPE html><html><body></body></html>', { url });
回答by Mike Rodov
for me this was solved by upgrading to "jest": "^24.9.0",
对我来说,这是通过升级到“jest”解决的:“^24.9.0”,
回答by pascalwhoop
This popped up with me when integrating enzymewith jest. The Github Issue Discussionsuggests the same as noted above, namely adding
整合时,此弹出我enzyme用jest。在Github上的问题探讨表明同上面提到的,即增加
"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.
到开玩笑的配置。然而,很高兴知道这也是由酶触发的。对我来说是 React v15 和 v15 适配器。
回答by Rock Lee
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 installand then npm updatebut I should have only ran npm install. I fixed the problem by deleting node_modulesdirectory and running npm installagain.
这听起来可能很傻,但对我来说,问题是因为我错误地安装了带有npm update. 我正在跑步npm install,npm update但我应该只跑npm install。我通过删除node_modules目录并npm install再次运行来解决这个问题。
回答by Amjad Khan
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,
在使用 React JS 时你不需要做任何事情。放置 JEST 和设置测试环境是 create-react-app 的默认行为。在下面运行时,它开始显示测试成功或失败,
npm test
npm 测试
In case you want test coverage, you simply need to add below to package.json file
如果您需要测试覆盖率,您只需将以下内容添加到 package.json 文件中
"test": "react-scripts test --coverage" Now your "script" of package.json look like,
"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"
}
}

