( spy: SpyLike<Self, Args, Return>, callIndex: number, expected?: ExpectedSpyCall<Self, Args, Return>, )
| 1257 | * @param expected The expected spy call. |
| 1258 | */ |
| 1259 | export 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 |
no test coverage detected