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

Function assertSpyCall

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

Source from the content-addressed store, hash-verified

1257 * @param expected The expected spy call.
1258 */
1259export function assertSpyCall<
1260 Self,
1261 Args extends unknown[],
1262 Return,
1263>(
1264 spy: SpyLike<Self, Args, Return>,
1265 callIndex: number,
1266 expected?: ExpectedSpyCall<Self, Args, Return>,
1267) {
1268 const call = getSpyCall(spy, callIndex);
1269 if (expected) {
1270 if (expected.args) {
1271 try {
1272 assertEquals(call.args, expected.args);
1273 } catch (e) {
1274 assertIsError(e);
1275 throw new AssertionError(
1276 "Spy not called with expected args:\n" +
1277 e.message.split("\n").slice(1).join("\n"),
1278 );
1279 }
1280 }
1281
1282 if ("self" in expected) {
1283 try {
1284 assertEquals(call.self, expected.self);
1285 } catch (e) {
1286 assertIsError(e);
1287 let message = expected.self
1288 ? "Spy not called as method on expected self:\n"
1289 : "Spy not expected to be called as method on object:\n";
1290 message += e.message.split("\n").slice(1).join("\n");
1291 throw new AssertionError(message);
1292 }
1293 }
1294
1295 if ("returned" in expected) {
1296 if ("error" in expected) {
1297 throw new TypeError(
1298 "Do not expect error and return, only one should be expected",
1299 );
1300 }
1301 if (call.error) {
1302 throw new AssertionError(
1303 "Spy call did not return expected value, an error was thrown.",
1304 );
1305 }
1306 try {
1307 assertEquals(call.returned, expected.returned);
1308 } catch (e) {
1309 assertIsError(e);
1310 throw new AssertionError(
1311 "Spy call did not return expected value:\n" +
1312 e.message.split("\n").slice(1).join("\n"),
1313 );
1314 }
1315 }
1316

Callers 14

parse_test.tsFile · 0.90
parse_test.tsFile · 0.90
time_test.tsFile · 0.90
bdd_test.tsFile · 0.90
assertOptionsFunction · 0.90
assertIgnoreOptionsFunction · 0.90
assertOnlyFunction · 0.90
assertHooksFunction · 0.90
assertDescribeOptionsFunction · 0.90
assertItOptionsFunction · 0.90
mock_test.tsFile · 0.90

Calls 4

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

Tested by

no test coverage detected