gpt4 book ai didi

javascript - javascript测试中的异步断言

转载 作者:行者123 更新时间:2023-11-28 21:23:53 27 4
gpt4 key购买 nike

我正在尝试测试一个 React 项目,我有以下异步测试应该在这里失败:fail("Fail: "+ response.status)

我需要做什么才能做到这一点?

describe('Test dummy data endpoint', () => {
it('Test postman-echo for 2 element todo list', (done) => {
var options = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(["One", "Two"])
}
var promise = fetch('https://postman-echo.com/post', options);
promise.then(
function (response) {
it('Status should be 200', (stschk) => {
fail("Fail: " + response.status)
stschk()
})
return response.json()
},
function (error) {
fail("Failed", error)
}
)
done()
}
)
}
)

最佳答案

对于任何正在寻找的人,这里是如何做的:

describe('Test ajax', () => {
it('Test postman-echo for 2 element todo list', (asdf) => {
expect.assertions(2)
var options = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(["One", "Two"])
}
fetch('https://postman-echo.com/post', options).then(
function (data) {
expect(data.status).toEqual(200)
data.json().then((jd) => {
expect(jd.data.length).toEqual(2)
asdf()
})
}
)
})
})

关于javascript - javascript测试中的异步断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45804801/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com