admin管理员组文章数量:1406178
I have a simple code that resolves after 1 second:
const promiseWithTimeout = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({id: 3});
}, 1000);
});
};
I am trying to make a test using jest, however, using resolves or awaits makes it so the test passes even with the wrong expectation (unhandled rejection) . And If I try to make a failing test, it passes even if the expectation fails
What I tried
it("Should return the right ID", ()=>{
expect.assertions(1);
expect(promiseWithTimeout()).resolves.toEqual({id: 3}); // this one works fine
jest.advanceTimersByTime(1000)
})
it("Should return the right ID", ()=>{
expect.assertions(1);
expect(promiseWithTimeout()).resolves.toEqual({id: 4}); // this PASSES , but shows
// UnhandledPromiseRejectionWarning: Error: expect(received).resolves.toEqual(expected) // deep equality
jest.advanceTimersByTime(1000)
})
I expected it to fail and show that the objects are different.
I also tried using then instead, but still same problem
I have a simple code that resolves after 1 second:
const promiseWithTimeout = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({id: 3});
}, 1000);
});
};
I am trying to make a test using jest, however, using resolves or awaits makes it so the test passes even with the wrong expectation (unhandled rejection) . And If I try to make a failing test, it passes even if the expectation fails
What I tried
it("Should return the right ID", ()=>{
expect.assertions(1);
expect(promiseWithTimeout()).resolves.toEqual({id: 3}); // this one works fine
jest.advanceTimersByTime(1000)
})
it("Should return the right ID", ()=>{
expect.assertions(1);
expect(promiseWithTimeout()).resolves.toEqual({id: 4}); // this PASSES , but shows
// UnhandledPromiseRejectionWarning: Error: expect(received).resolves.toEqual(expected) // deep equality
jest.advanceTimersByTime(1000)
})
I expected it to fail and show that the objects are different.
I also tried using then instead, but still same problem
Share Improve this question edited Aug 16, 2022 at 14:05 Caio Oliveira asked Aug 16, 2022 at 13:47 Caio OliveiraCaio Oliveira 8446 silver badges13 bronze badges1 Answer
Reset to default 5You have a few options -
return a promise from your test
This will pass -
it("should return the id", () => {
return promiseWithTimeout().then(m => { // return
expect(m.id).toBe(3) // ✓
})
})
This will fail -
it("should return the id", () => {
return promiseWithTimeout().then(m => { // return
expect(m.id).toBe(4) // ✕ id 4
})
})
async..await
This will pass -
it("should return the id", async () => { // async
const m = await promiseWithTimeout() // await
expect(m.id).toBe(3) // ✓
})
This will fail -
it("should return the id", async () => {
const m = await promiseWithTimeout()
expect(m.id).toBe(4) // ✕ id 4
})
.resolves and .rejects
Notice you must return
then expectant promise. This will pass -
it("should return the id", ()=>{
return expect(promiseWithTimeout()).resolves.toEqual({id: 3}) // ✓
})
This will fail -
it("should return the id", ()=>{
return expect(promiseWithTimeout()).resolves.toEqual({id: 4}) // ✕ id 4
})
using jest.advanceTimersByTime
Jest allows you to "fake" the timers so test can run fast while still ensuring async code is behaving correctly. In your testing file you must include jest.useFakeTimers()
and your tests must use one of the techniques above. Here we return
expectant promises -
jest.useFakeTimers()
it("should return the id", () => {
const p = promiseWithTimeout()
jest.advanceTimersByTime(1000) // advance timers
return expect(p).resolves.toEqual({ id: 3 }) // ✓
})
This will fail -
jest.useFakeTimers()
it("should return the id", () => {
const p = promiseWithTimeout()
jest.advanceTimersByTime(1000)
return expect(p).resolves.toEqual({ id: 4 }) // ✕ id 4
})
using expect.assertions
In many cases you do not need to specify expect.assertions
. For example, when using the .resolves
expectation, we will get an assertion failure if the promise rejects -
const promiseWithTimeout = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject({ id: 3 }) // reject
}, 1000)
})
}
jest.useFakeTimers()
it("should return the id", () => {
const p = promiseWithTimeout()
jest.advanceTimersByTime(1000)
return expect(p).resolves.toEqual({ id: 3 }) // ✕ rejected
})
If you expect a promise to be rejected, use the .catch method
and then add expect.assertions
to verify that a certain number of assertions are called. Otherwise, a fulfilled promise would not fail the test -
test('the fetch fails with an error', () => {
expect.assertions(1) // expect assertions
return fetchData().catch(e => expect(e).toMatch('error')) // ✓
})
For more information, see Testing Asynchronous Code from the Jest docs.
本文标签: javascriptTest Promise with timeout JestStack Overflow
版权声明:本文标题:javascript - Test Promise with timeout Jest - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744973512a2635379.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论