在反应烟雾测试期间引发“缺少 cookie 标头或对象”错误(Create-react-app)
Posted
技术标签:
【中文标题】在反应烟雾测试期间引发“缺少 cookie 标头或对象”错误(Create-react-app)【英文标题】:"Missing the cookie header or object" error thrown during react smoke tests (Create-react-app) 【发布时间】:2017-10-17 12:00:48 【问题描述】:我使用universal-cookie
存储在本地存储中,然后通过管道传输到存储中。
class App extends Component
componentDidMount()
// if a cookie is present, save the value in the store for use communication
// with the server. If the cookie is undefined, the user is redirected to the login page.
// Redirection is handled by router.
const userNameInCookie = cookies.get('userName');
if (userNameInCookie)
this.props.dispatch(actions.setUserNameFromCookie(userNameInCookie));
render()
return (
<div>
<div className="header">
<h2 className="header-title">Traveler</h2>
</div>
this.props.children
</div>
);
当我运行 Jest 测试套件时,每个测试都失败并出现此错误
FAIL src/test/UserCreateForm.test.js
● Test suite failed to run
Missing the cookie header or object
at new Cookies (node_modules/universal-cookie/lib/Cookies.js:35:15)
at Object.<anonymous> (src/actions/index.js:4:17)
at Object.<anonymous> (src/reducers/index.js:1:258)
at Object.<anonymous> (src/store.js:4:40)
at Object.<anonymous> (src/test/UserCreateForm.test.js:7:40)
at handle (node_modules/worker-farm/lib/child/index.js:41:8)
at process.<anonymous> (node_modules/worker-farm/lib/child/index.js:47:3)
at emitTwo (events.js:106:13)
at process.emit (events.js:194:7)
at process.nextTick (internal/child_process.js:766:12)
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)
我也尝试过使用 Enzyme 进行冒烟测试,但我得到了同样的错误。代码的行为完全符合我的要求,所以我相信universal-cookie
根本无法与测试相处。
有什么想法吗? 谢谢!
【问题讨论】:
【参考方案1】:您是否导入了universal-cookie,然后定义了类?
import Cookies from 'universal-cookie';
const cookies = new Cookies();
所以你的代码看起来是......
import Cookies from 'universal-cookie';
class App extends Component
componentDidMount()
const cookies = new Cookies();
const userNameInCookie = cookies.get('userName');
if (userNameInCookie)
this.props.dispatch(actions.setUserNameFromCookie(userNameInCookie));
render()
return (
<div>
<div className="header">
<h2 className="header-title">Traveler</h2>
</div>
this.props.children
</div>
);
【讨论】:
以上是关于在反应烟雾测试期间引发“缺少 cookie 标头或对象”错误(Create-react-app)的主要内容,如果未能解决你的问题,请参考以下文章