MCPcopy Create free account
hub / github.com/denoland/std / assertSpyCallAsync

Function assertSpyCallAsync

testing/mock.ts:1358–1428  ·  view source on GitHub ↗
(
  spy: SpyLike<Self, Args, Promise<Return>>,
  callIndex: number,
  expected?: ExpectedSpyCall<Self, Args, Promise<Return> | Return>,
)

Source from the content-addressed store, hash-verified

1356 * @param expected The expected spy call.
1357 */
1358export 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"),

Callers 1

mock_test.tsFile · 0.90

Calls 6

assertEqualsFunction · 0.90
assertRejectsFunction · 0.90
assertSpyCallFunction · 0.85
getSpyCallFunction · 0.85
assertIsErrorFunction · 0.50
sliceMethod · 0.45

Tested by

no test coverage detected