admin管理员组文章数量:1431935
I am using Jest with ES2015, trying to test this ponent that makes an ajax call (ajax call done with jQuery) on form submit. I'm just trying to mock the value returned and test the value is updated in the ponent. But I receive this error:
- TypeError: Cannot read property 'then' of undefined
Below is the code for ajax call:
accountValidate(name, email).then(this.showMsg);
showMsg(response) {
if (!response.authenticated) {
this.setState({msg: response.msg})
}
}
accountValidate(name, email) {
return($.ajax({
type: 'POST',
url: `/users/authenticate`,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
dataType: 'json',
data: JSON.stringify({name: name, email: email})
}));
}
Here is the code for Jest:
let testMsg = {msg: 'testing msg'}
const msgResponse = Promise.resolve(testMsg);
accountValidate.mockClear();
accountValidate.mockReturnValue(msgResponse);
return msgResponse.then(() => {
console.log('test');
});
I am using Jest with ES2015, trying to test this ponent that makes an ajax call (ajax call done with jQuery) on form submit. I'm just trying to mock the value returned and test the value is updated in the ponent. But I receive this error:
- TypeError: Cannot read property 'then' of undefined
Below is the code for ajax call:
accountValidate(name, email).then(this.showMsg);
showMsg(response) {
if (!response.authenticated) {
this.setState({msg: response.msg})
}
}
accountValidate(name, email) {
return($.ajax({
type: 'POST',
url: `/users/authenticate`,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
dataType: 'json',
data: JSON.stringify({name: name, email: email})
}));
}
Here is the code for Jest:
let testMsg = {msg: 'testing msg'}
const msgResponse = Promise.resolve(testMsg);
accountValidate.mockClear();
accountValidate.mockReturnValue(msgResponse);
return msgResponse.then(() => {
console.log('test');
});
Share
Improve this question
edited Jun 8, 2016 at 10:01
BioGeek
23k23 gold badges90 silver badges155 bronze badges
asked Jun 3, 2016 at 3:17
sayayinsayayin
1,0416 gold badges25 silver badges43 bronze badges
6
-
1
The error implies either
accountValidate
returnsundefined
orerrorResponse
isundefined
– MinusFour Commented Jun 3, 2016 at 3:23 - but the ponent is working fine. I am able to make the ajax call, receive data in response and update the ponent. Only the test fails. – sayayin Commented Jun 3, 2016 at 3:25
-
1
Is
accountValidate
defined as a function somewhere in your code? It's kind of unclear where this code is being pulled from - thethis.setState()
calls make me think this is part of a React ponent? – Nick Zuber Commented Jun 3, 2016 at 3:25 - Yes it is defined, you can see above. And yes it is React.js with ES2015 – sayayin Commented Jun 3, 2016 at 3:27
-
@sayayin, what is
errorResponse
? – MinusFour Commented Jun 3, 2016 at 3:30
2 Answers
Reset to default 1It's really late but maybe it helps someone. I would try it like that:
let testMsg = {msg: 'testing msg'}
beforeEach(() => {
(accountValidate: any).mockClear();
(accountValidate: any).mockReturnValue(
new Promise(resolve => {
resolve(testMsg);
})
);
});
Hi we were getting the same error in the test env we are using the typescript , testing-library/react
I was using node v 14.X but other person was using node 16.X and above they were getting this error. So I updated my node version then able to replicate it on my local.
Then In test file, added the mockcall in the act, as it is affecting in updating the state also
act(() => {
mockAxios.post.mockImplementation(
async () => Promise.resolve(availabilitySuccessMockData)
);
});
本文标签: javascriptJESTTypeError Cannot read property 39then39 of undefinedStack Overflow
版权声明:本文标题:javascript - JEST - TypeError: Cannot read property 'then' of undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745585606a2664885.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论