( spy: SpyLike<Self, Args, Promise<Return>>, callIndex: number, expected?: ExpectedSpyCall<Self, Args, Promise<Return> | Return>, )
| 1356 | * @param expected The expected spy call. |
| 1357 | */ |
| 1358 | export async function assertSpyCallAsync< |
| 1359 | Self, |
| 1360 | Args extends unknown[], |
| 1361 | Return, |
| 1362 | >( |
| 1363 | spy: SpyLike<Self, Args, Promise<Return>>, |
| 1364 | callIndex: number, |
| 1365 | expected?: ExpectedSpyCall<Self, Args, Promise<Return> | Return>, |
| 1366 | ) { |
| 1367 | const expectedSync = expected && { ...expected }; |
| 1368 | if (expectedSync) { |
| 1369 | delete expectedSync.returned; |
| 1370 | delete expectedSync.error; |
| 1371 | } |
| 1372 | assertSpyCall(spy, callIndex, expectedSync); |
| 1373 | const call = getSpyCall(spy, callIndex); |
| 1374 | |
| 1375 | if (call.error) { |
| 1376 | throw new AssertionError( |
| 1377 | "Spy call did not return a promise, an error was thrown.", |
| 1378 | ); |
| 1379 | } |
| 1380 | if (call.returned !== Promise.resolve(call.returned)) { |
| 1381 | throw new AssertionError( |
| 1382 | "Spy call did not return a promise, a value was returned.", |
| 1383 | ); |
| 1384 | } |
| 1385 | |
| 1386 | if (expected) { |
| 1387 | if ("returned" in expected) { |
| 1388 | if ("error" in expected) { |
| 1389 | throw new TypeError( |
| 1390 | "Do not expect error and return, only one should be expected", |
| 1391 | ); |
| 1392 | } |
| 1393 | let expectedResolved; |
| 1394 | try { |
| 1395 | expectedResolved = await expected.returned; |
| 1396 | } catch { |
| 1397 | throw new TypeError( |
| 1398 | "Do not expect rejected promise, expect error instead", |
| 1399 | ); |
| 1400 | } |
| 1401 | |
| 1402 | let resolved; |
| 1403 | try { |
| 1404 | resolved = await call.returned; |
| 1405 | } catch { |
| 1406 | throw new AssertionError("Spy call returned promise was rejected"); |
| 1407 | } |
| 1408 | |
| 1409 | try { |
| 1410 | assertEquals(resolved, expectedResolved); |
| 1411 | } catch (e) { |
| 1412 | assertIsError(e); |
| 1413 | throw new AssertionError( |
| 1414 | "Spy call did not resolve to expected value:\n" + |
| 1415 | e.message.split("\n").slice(1).join("\n"), |
no test coverage detected