admin管理员组文章数量:1321062
I have this function in my child ponent:
<div> {handTotal('dealersHand').total} </div>
This has been passed down from above
however when I run jest it says Cannot read property 'total' of undefined
when i console.log(handTotal('dealersHand')
it is logging the right thing and the function works so I know it's doing the correct thing
I've stubbed it out in jest like so:
const handTotalStub = jest.fn()
beforeEach(() => {
wrapper = mount(<Dealer
dealersHand={dealersHandStub}
containsAce={containsAceStub}
handTotal={handTotalStub}
/>);
})
How do I pass the parameter into this function so that jest understands what it is?
can add more code/explanation if doesn't make sense
stubbing:
const handTotalStub = jest.fn().mockReturnValue('dealersHand')
test:
it('expects dealers hand to equal', () => {
expect(handTotalStub('dealersHand').total).toEqual(1);
});
I have this function in my child ponent:
<div> {handTotal('dealersHand').total} </div>
This has been passed down from above
however when I run jest it says Cannot read property 'total' of undefined
when i console.log(handTotal('dealersHand')
it is logging the right thing and the function works so I know it's doing the correct thing
I've stubbed it out in jest like so:
const handTotalStub = jest.fn()
beforeEach(() => {
wrapper = mount(<Dealer
dealersHand={dealersHandStub}
containsAce={containsAceStub}
handTotal={handTotalStub}
/>);
})
How do I pass the parameter into this function so that jest understands what it is?
can add more code/explanation if doesn't make sense
stubbing:
const handTotalStub = jest.fn().mockReturnValue('dealersHand')
test:
it('expects dealers hand to equal', () => {
expect(handTotalStub('dealersHand').total).toEqual(1);
});
Share
Improve this question
edited Apr 8, 2018 at 7:36
The Walrus
asked Apr 8, 2018 at 7:08
The WalrusThe Walrus
1,2087 gold badges31 silver badges49 bronze badges
1 Answer
Reset to default 3Your jest spy for handleTotalStub
does not return anything, so it therefore returns undefined
. When your ponent tries to call handTotal('dealersHand').total
it is therefore calling undefined.total
, because handTotal(...)
is not defined.
Update your spy to return something (anything) by changing
handTotalStub = jest.fn();
to
handTotalStub = jest.fn().mockReturnValue(SOME_VALUE);
or
handTotalStub = jest.fn().mockImplementation(() => SOME_VALUE);
(where SOME_VALUE is any value that you can mock out to act as what the ponent expects to be there)
EDIT --
Ok, so you're misunderstanding what mockReturnValue does. You don't have to mock the parameters being passed in to that method. Because your ponent is already passing in that string. BUT, the actual handTotal
method is never going to be called (this is a good thing, because we're not testing how handTotal
works, we're testing how the ponent works).
So, whatever handTotal
would normally return is what you want to put in mockReturnValue()
. So if handTotal
returns an object like {total: 1}
, then you would say mockReturnValue({total: 1})
本文标签: javascriptPassing parameters to jest functionStack Overflow
版权声明:本文标题:javascript - Passing parameters to jest function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742092369a2420343.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论