MCPcopy
hub / github.com/jestjs/jest / createToHaveBeenCalledMatcher

Function createToHaveBeenCalledMatcher

packages/expect/src/spyMatchers.ts:359–412  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

357};
358
359const createToHaveBeenCalledMatcher = (): MatcherFunction<[unknown]> =>
360 function (received: any, expected: unknown): SyncExpectationResult {
361 const expectedArgument = '';
362 const options: MatcherHintOptions = {
363 isNot: this.isNot,
364 promise: this.promise,
365 };
366 ensureNoExpected(expected, 'toHaveBeenCalled', options);
367 ensureMockOrSpy(received, 'toHaveBeenCalled', expectedArgument, options);
368
369 const receivedIsSpy = isSpy(received);
370 const receivedName = receivedIsSpy ? 'spy' : received.getMockName();
371 const count = receivedIsSpy
372 ? received.calls.count()
373 : received.mock.calls.length;
374 const calls = receivedIsSpy
375 ? received.calls.all().map((x: any) => x.args)
376 : received.mock.calls;
377 const pass = count > 0;
378 const message = pass
379 ? () =>
380 // eslint-disable-next-line prefer-template
381 matcherHint(
382 'toHaveBeenCalled',
383 receivedName,
384 expectedArgument,
385 options,
386 ) +
387 '\n\n' +
388 `Expected number of calls: ${printExpected(0)}\n` +
389 `Received number of calls: ${printReceived(count)}\n\n` +
390 calls
391 .reduce((lines: Array<string>, args: any, i: number) => {
392 if (lines.length < PRINT_LIMIT) {
393 lines.push(`${i + 1}: ${printReceivedArgs(args)}`);
394 }
395
396 return lines;
397 }, [])
398 .join('\n')
399 : () =>
400 // eslint-disable-next-line prefer-template
401 matcherHint(
402 'toHaveBeenCalled',
403 receivedName,
404 expectedArgument,
405 options,
406 ) +
407 '\n\n' +
408 `Expected number of calls: >= ${printExpected(1)}\n` +
409 `Received number of calls: ${printReceived(count)}`;
410
411 return {message, pass};
412 };
413
414const createToHaveReturnedMatcher = (): MatcherFunction<[unknown]> =>
415 function (received: any, expected): SyncExpectationResult {

Callers 1

spyMatchers.tsFile · 0.85

Calls 9

ensureNoExpectedFunction · 0.90
matcherHintFunction · 0.90
printExpectedFunction · 0.90
printReceivedFunction · 0.90
ensureMockOrSpyFunction · 0.85
printReceivedArgsFunction · 0.85
getMockNameMethod · 0.80
isSpyFunction · 0.70
countMethod · 0.45

Tested by

no test coverage detected