在 Jest 中,当我使用拒绝的 Promise 时,我无法使用自己的代码看到堆栈跟踪...
async function throwReject() {
return Promise.reject("There is an asynchron error")
}
it("should display an asynchron error", async () => {
await expect(throwReject()).rejects.toBe(1)
})
对于这个测试,我有一条不太有用的消息:
expect(received).rejects.toBe(expected) // Object.is equality
Expected: 1
Received: "There is an asynchron error"
12 |
13 | it("should display an asynchron error", async () => {
> 14 | await expect(throwReject()).rejects.toBe(1)
| ^
15 | })
16 |
at Object.toBe (../../node_modules/expect/build/index.js:241:20)
at _callee$ (unit/promise.spec.js:14:39)
at tryCatch (../../node_modules/regenerator-runtime/runtime.js:63:40)
at Generator.invoke [as _invoke] (../../node_modules/regenerator-runtime/runtime.js:293:22)
at Generator.next (../../node_modules/regenerator-runtime/runtime.js:118:21)
at asyncGeneratorStep (../../node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
at _next (../../node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
at ../../node_modules/@babel/runtime/helpers/asyncToGenerator.js:32:7
at Object.<anonymous> (../../node_modules/@babel/runtime/helpers/asyncToGenerator.js:21:12)
另一方面,对于同步错误,堆栈跟踪要好得多,包括抛出错误的行:
function throwError() {
throw new Error("There is an error")
}
it("should display error", () => {
expect(throwError()).toBe(1)
})
● 应该显示错误
There is an error
1 | function throwError() {
> 2 | throw new Error("There is an error")
| ^
3 | }
4 |
5 | async function throwReject() {
at throwError (unit/promise.spec.js:2:9)
at Object.<anonymous> (unit/promise.spec.js:10:10)
我的测试写错了吗? 您有一些资源可以查看吗?