当我想用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>)
发布于 2018-07-27 17:12:13
如果您使用http://localhost
前缀访问应用程序,则需要将jest.config.js
中的jest配置更新为,
"jest": {
"verbose": true,
"testURL": "http://localhost/"
}
如果您还没有任何jest配置,只需在package.json
中包含该配置即可。例如:
{
"name": "...",
"description": "...",
...
"jest": {
"verbose": true,
"testURL": "http://localhost/"
}
}
或者在jest.config.js
中:
module.exports = {
verbose: true,
testURL: "http://localhost/",
...
}
或者,如果您配置了projects
:
module.exports = {
verbose: true,
projects: [{
runner: 'jest-runner',
testURL: "http://localhost/",
// ...
}]
}
发布于 2018-07-27 18:05:24
我只是在一个很大的monorepo中突然出现了这个(在单元测试中,否则就不需要jsdom)。在我们的jest.config.js
(或package.json
等效项)中显式设置以下内容也可以缓解该问题:
module.exports = {
testEnvironment: 'node'
}
更新:正如Nicolas在下面提到的(谢谢!),如果你没有使用任何配置文件,你也可以添加以下标志:
jest --testEnvironment node
# or
jest --env=node
发布于 2018-07-27 20:05:52
您必须指定要使用的环境(--env
)。
在package.json
中运行jest
命令时,应该指定环境(jsdom
或node
)。例如:
"scripts": {
"jest": "jest --env=node --colors --coverage test",
"test": "npm run jest"
},
这对你应该是有效的!
https://stackoverflow.com/questions/51554366
复制相似问题