Javascript 开玩笑测试在一个函数中进行POST和fetch的异步函数
我需要使用jest来测试一个包含两个函数的函数。我尝试测试的函数中的第一个函数向其中包含API调用的路由发送POST,第二个函数从我设置的服务器上的端点获取API返回的数据。我对jest以及jest的哪些部分用于测试此函数知之甚少。我是否在主功能的一个测试中嵌套两个测试?以下是我需要测试的功能:Javascript 开玩笑测试在一个函数中进行POST和fetch的异步函数,javascript,node.js,express,webpack,jestjs,Javascript,Node.js,Express,Webpack,Jestjs,我需要使用jest来测试一个包含两个函数的函数。我尝试测试的函数中的第一个函数向其中包含API调用的路由发送POST,第二个函数从我设置的服务器上的端点获取API返回的数据。我对jest以及jest的哪些部分用于测试此函数知之甚少。我是否在主功能的一个测试中嵌套两个测试?以下是我需要测试的功能: document.getElementById('generate').addEventListener('click', handleSubmit); function handleSubmit(e
document.getElementById('generate').addEventListener('click', handleSubmit);
function handleSubmit(event) {
//event.preventDefault()
// check what text was put into the form field
let url = document.getElementById('URL').value
postURL('/postURL', url)
.then(updateUI())
.then(checkURL)
};
handleSubmit(我需要测试的主要功能):
postrl(构成POST的主函数中嵌套的第一个函数)
updateUI(获取数据的第二个函数):
let postURL = async(url = '', data = {})=>{
console.log(data);
let response = await fetch(url, {
method: 'POST',
credentials: 'same-origin',
headers: {
"Content-Type": 'application/json',
},
body: JSON.stringify( { data} ),
});
try {
updateUI()
}catch(error){
console.log("error", error);
}
}
const updateUI = async () =>{
const res = await fetch('/sentiment');
console.log(res);
try {
console.log(res.data)
const allData = await res.json();
console.log(allData)
document.getElementById("polarity").innerHTML = allData.polarity;
document.getElementById("polarityConfidence").innerHTML = allData.polarity_confidence;
document.getElementById("subjectivity").innerHTML = allData.polarity;
document.getElementById("subjectivityConfidence").innerHTML = allData.polarity_confidence;
return allData;
}catch(error){
console.log(error)
}
};