这对我来说似乎有点奇怪。我正在尝试使用 Jest 测试实际(即真实网络)请求。
这些是经过测试的场景:
- 测试没有标头的外部 API (fixer.io)
- 使用标头测试本地 API 服务器
- 使用以下标头测试相同的本地 API
node
终端
这种行为背后的原因可能是什么?解决办法是什么?
//This WORKS
test('testing no headers', () => {
return axios.get('http://api.fixer.io/latest')
.then( res => console.log(res) )
});
//This DOES NOT work
test('testing no headers', () => {
return axios.get('http://localhost:3000/users/4/profile',
{headers:{authorization:`Bearer ${mytoken}`}})
.then( res => console.log(res) )
});
//...
//Node Terminal
//This WORKS
> axios.get('http://localhost:3000/users/4/profile',
{headers:{authorization:`Bearer ${mytoken}`}})
.then( res => console.log(res) )
这可能是 Jest 配置问题。我解决了在 package.json 中强制“node”作为 jest 环境的问题:
“开玩笑”:{
“测试环境”:“节点”
}
请参阅文档:https://facebook.github.io/jest/docs/configuration.html#testenvironment-string https://facebook.github.io/jest/docs/configuration.html#testenvironment-string
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)