12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343 |
- 'use strict';
- Object.defineProperty(exports, '__esModule', {
- value: true
- });
- exports.default = void 0;
- var _jestGetType = _interopRequireDefault(require('jest-get-type'));
- var _jestMatcherUtils = require('jest-matcher-utils');
- var _jasmineUtils = require('./jasmineUtils');
- var _utils = require('./utils');
- function _interopRequireDefault(obj) {
- return obj && obj.__esModule ? obj : {default: obj};
- }
- /**
- * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
- *
- * This source code is licensed under the MIT license found in the
- * LICENSE file in the root directory of this source tree.
- */
- // The optional property of matcher context is true if undefined.
- const isExpand = expand => expand !== false;
- const PRINT_LIMIT = 3;
- const NO_ARGUMENTS = 'called with 0 arguments';
- const printExpectedArgs = expected =>
- expected.length === 0
- ? NO_ARGUMENTS
- : expected.map(arg => (0, _jestMatcherUtils.printExpected)(arg)).join(', ');
- const printReceivedArgs = (received, expected) =>
- received.length === 0
- ? NO_ARGUMENTS
- : received
- .map((arg, i) =>
- Array.isArray(expected) &&
- i < expected.length &&
- isEqualValue(expected[i], arg)
- ? printCommon(arg)
- : (0, _jestMatcherUtils.printReceived)(arg)
- )
- .join(', ');
- const printCommon = val =>
- (0, _jestMatcherUtils.DIM_COLOR)((0, _jestMatcherUtils.stringify)(val));
- const isEqualValue = (expected, received) =>
- (0, _jasmineUtils.equals)(expected, received, [_utils.iterableEquality]);
- const isEqualCall = (expected, received) => isEqualValue(expected, received);
- const isEqualReturn = (expected, result) =>
- result.type === 'return' && isEqualValue(expected, result.value);
- const countReturns = results =>
- results.reduce((n, result) => (result.type === 'return' ? n + 1 : n), 0);
- const printNumberOfReturns = (countReturns, countCalls) =>
- `\nNumber of returns: ${(0, _jestMatcherUtils.printReceived)(countReturns)}` +
- (countCalls !== countReturns
- ? `\nNumber of calls: ${(0, _jestMatcherUtils.printReceived)(countCalls)}`
- : '');
- // Given a label, return a function which given a string,
- // right-aligns it preceding the colon in the label.
- const getRightAlignedPrinter = label => {
- // Assume that the label contains a colon.
- const index = label.indexOf(':');
- const suffix = label.slice(index);
- return (string, isExpectedCall) =>
- (isExpectedCall
- ? '->' + ' '.repeat(Math.max(0, index - 2 - string.length))
- : ' '.repeat(Math.max(index - string.length))) +
- string +
- suffix;
- };
- const printReceivedCallsNegative = (
- expected,
- indexedCalls,
- isOnlyCall,
- iExpectedCall
- ) => {
- if (indexedCalls.length === 0) {
- return '';
- }
- const label = 'Received: ';
- if (isOnlyCall) {
- return label + printReceivedArgs(indexedCalls[0], expected) + '\n';
- }
- const printAligned = getRightAlignedPrinter(label);
- return (
- 'Received\n' +
- indexedCalls.reduce(
- (printed, [i, args]) =>
- printed +
- printAligned(String(i + 1), i === iExpectedCall) +
- printReceivedArgs(args, expected) +
- '\n',
- ''
- )
- );
- };
- const printExpectedReceivedCallsPositive = (
- expected,
- indexedCalls,
- expand,
- isOnlyCall,
- iExpectedCall
- ) => {
- const expectedLine = `Expected: ${printExpectedArgs(expected)}\n`;
- if (indexedCalls.length === 0) {
- return expectedLine;
- }
- const label = 'Received: ';
- if (isOnlyCall && (iExpectedCall === 0 || iExpectedCall === undefined)) {
- const received = indexedCalls[0][1];
- if (isLineDiffableCall(expected, received)) {
- // Display diff without indentation.
- const lines = [
- (0, _jestMatcherUtils.EXPECTED_COLOR)('- Expected'),
- (0, _jestMatcherUtils.RECEIVED_COLOR)('+ Received'),
- ''
- ];
- const length = Math.max(expected.length, received.length);
- for (let i = 0; i < length; i += 1) {
- if (i < expected.length && i < received.length) {
- if (isEqualValue(expected[i], received[i])) {
- lines.push(` ${printCommon(received[i])},`);
- continue;
- }
- if (isLineDiffableArg(expected[i], received[i])) {
- const difference = (0, _jestMatcherUtils.diff)(
- expected[i],
- received[i],
- {
- expand
- }
- );
- if (
- typeof difference === 'string' &&
- difference.includes('- Expected') &&
- difference.includes('+ Received')
- ) {
- // Omit annotation in case multiple args have diff.
- lines.push(difference.split('\n').slice(3).join('\n') + ',');
- continue;
- }
- }
- }
- if (i < expected.length) {
- lines.push(
- (0, _jestMatcherUtils.EXPECTED_COLOR)(
- '- ' + (0, _jestMatcherUtils.stringify)(expected[i])
- ) + ','
- );
- }
- if (i < received.length) {
- lines.push(
- (0, _jestMatcherUtils.RECEIVED_COLOR)(
- '+ ' + (0, _jestMatcherUtils.stringify)(received[i])
- ) + ','
- );
- }
- }
- return lines.join('\n') + '\n';
- }
- return expectedLine + label + printReceivedArgs(received, expected) + '\n';
- }
- const printAligned = getRightAlignedPrinter(label);
- return (
- expectedLine +
- 'Received\n' +
- indexedCalls.reduce((printed, [i, received]) => {
- const aligned = printAligned(String(i + 1), i === iExpectedCall);
- return (
- printed +
- ((i === iExpectedCall || iExpectedCall === undefined) &&
- isLineDiffableCall(expected, received)
- ? aligned.replace(': ', '\n') +
- printDiffCall(expected, received, expand)
- : aligned + printReceivedArgs(received, expected)) +
- '\n'
- );
- }, '')
- );
- };
- const indentation = 'Received'.replace(/\w/g, ' ');
- const printDiffCall = (expected, received, expand) =>
- received
- .map((arg, i) => {
- if (i < expected.length) {
- if (isEqualValue(expected[i], arg)) {
- return indentation + ' ' + printCommon(arg) + ',';
- }
- if (isLineDiffableArg(expected[i], arg)) {
- const difference = (0, _jestMatcherUtils.diff)(expected[i], arg, {
- expand
- });
- if (
- typeof difference === 'string' &&
- difference.includes('- Expected') &&
- difference.includes('+ Received')
- ) {
- // Display diff with indentation.
- // Omit annotation in case multiple args have diff.
- return (
- difference
- .split('\n')
- .slice(3)
- .map(line => indentation + line)
- .join('\n') + ','
- );
- }
- }
- } // Display + only if received arg has no corresponding expected arg.
- return (
- indentation +
- (i < expected.length
- ? ' ' + (0, _jestMatcherUtils.printReceived)(arg)
- : (0, _jestMatcherUtils.RECEIVED_COLOR)(
- '+ ' + (0, _jestMatcherUtils.stringify)(arg)
- )) +
- ','
- );
- })
- .join('\n');
- const isLineDiffableCall = (expected, received) =>
- expected.some(
- (arg, i) => i < received.length && isLineDiffableArg(arg, received[i])
- ); // Almost redundant with function in jest-matcher-utils,
- // except no line diff for any strings.
- const isLineDiffableArg = (expected, received) => {
- const expectedType = (0, _jestGetType.default)(expected);
- const receivedType = (0, _jestGetType.default)(received);
- if (expectedType !== receivedType) {
- return false;
- }
- if (_jestGetType.default.isPrimitive(expected)) {
- return false;
- }
- if (
- expectedType === 'date' ||
- expectedType === 'function' ||
- expectedType === 'regexp'
- ) {
- return false;
- }
- if (expected instanceof Error && received instanceof Error) {
- return false;
- }
- if (
- expectedType === 'object' &&
- typeof expected.asymmetricMatch === 'function'
- ) {
- return false;
- }
- if (
- receivedType === 'object' &&
- typeof received.asymmetricMatch === 'function'
- ) {
- return false;
- }
- return true;
- };
- const printResult = (result, expected) =>
- result.type === 'throw'
- ? 'function call threw an error'
- : result.type === 'incomplete'
- ? 'function call has not returned yet'
- : isEqualValue(expected, result.value)
- ? printCommon(result.value)
- : (0, _jestMatcherUtils.printReceived)(result.value);
- // Return either empty string or one line per indexed result,
- // so additional empty line can separate from `Number of returns` which follows.
- const printReceivedResults = (
- label,
- expected,
- indexedResults,
- isOnlyCall,
- iExpectedCall
- ) => {
- if (indexedResults.length === 0) {
- return '';
- }
- if (isOnlyCall && (iExpectedCall === 0 || iExpectedCall === undefined)) {
- return label + printResult(indexedResults[0][1], expected) + '\n';
- }
- const printAligned = getRightAlignedPrinter(label);
- return (
- label.replace(':', '').trim() +
- '\n' +
- indexedResults.reduce(
- (printed, [i, result]) =>
- printed +
- printAligned(String(i + 1), i === iExpectedCall) +
- printResult(result, expected) +
- '\n',
- ''
- )
- );
- };
- const createToBeCalledMatcher = matcherName =>
- function (received, expected) {
- const expectedArgument = '';
- const options = {
- isNot: this.isNot,
- promise: this.promise
- };
- (0, _jestMatcherUtils.ensureNoExpected)(expected, matcherName, options);
- ensureMockOrSpy(received, matcherName, expectedArgument, options);
- const receivedIsSpy = isSpy(received);
- const receivedName = receivedIsSpy ? 'spy' : received.getMockName();
- const count = receivedIsSpy
- ? received.calls.count()
- : received.mock.calls.length;
- const calls = receivedIsSpy
- ? received.calls.all().map(x => x.args)
- : received.mock.calls;
- const pass = count > 0;
- const message = pass
- ? () =>
- (0, _jestMatcherUtils.matcherHint)(
- matcherName,
- receivedName,
- expectedArgument,
- options
- ) +
- '\n\n' +
- `Expected number of calls: ${(0, _jestMatcherUtils.printExpected)(
- 0
- )}\n` +
- `Received number of calls: ${(0, _jestMatcherUtils.printReceived)(
- count
- )}\n\n` +
- calls
- .reduce((lines, args, i) => {
- if (lines.length < PRINT_LIMIT) {
- lines.push(`${i + 1}: ${printReceivedArgs(args)}`);
- }
- return lines;
- }, [])
- .join('\n')
- : () =>
- (0, _jestMatcherUtils.matcherHint)(
- matcherName,
- receivedName,
- expectedArgument,
- options
- ) +
- '\n\n' +
- `Expected number of calls: >= ${(0, _jestMatcherUtils.printExpected)(
- 1
- )}\n` +
- `Received number of calls: ${(0, _jestMatcherUtils.printReceived)(
- count
- )}`;
- return {
- message,
- pass
- };
- };
- const createToReturnMatcher = matcherName =>
- function (received, expected) {
- const expectedArgument = '';
- const options = {
- isNot: this.isNot,
- promise: this.promise
- };
- (0, _jestMatcherUtils.ensureNoExpected)(expected, matcherName, options);
- ensureMock(received, matcherName, expectedArgument, options);
- const receivedName = received.getMockName(); // Count return values that correspond only to calls that returned
- const count = received.mock.results.reduce(
- (n, result) => (result.type === 'return' ? n + 1 : n),
- 0
- );
- const pass = count > 0;
- const message = pass
- ? () =>
- (0, _jestMatcherUtils.matcherHint)(
- matcherName,
- receivedName,
- expectedArgument,
- options
- ) +
- '\n\n' +
- `Expected number of returns: ${(0, _jestMatcherUtils.printExpected)(
- 0
- )}\n` +
- `Received number of returns: ${(0, _jestMatcherUtils.printReceived)(
- count
- )}\n\n` +
- received.mock.results
- .reduce((lines, result, i) => {
- if (result.type === 'return' && lines.length < PRINT_LIMIT) {
- lines.push(
- `${i + 1}: ${(0, _jestMatcherUtils.printReceived)(
- result.value
- )}`
- );
- }
- return lines;
- }, [])
- .join('\n') +
- (received.mock.calls.length !== count
- ? `\n\nReceived number of calls: ${(0,
- _jestMatcherUtils.printReceived)(received.mock.calls.length)}`
- : '')
- : () =>
- (0, _jestMatcherUtils.matcherHint)(
- matcherName,
- receivedName,
- expectedArgument,
- options
- ) +
- '\n\n' +
- `Expected number of returns: >= ${(0,
- _jestMatcherUtils.printExpected)(1)}\n` +
- `Received number of returns: ${(0,
- _jestMatcherUtils.printReceived)(count)}` +
- (received.mock.calls.length !== count
- ? `\nReceived number of calls: ${(0,
- _jestMatcherUtils.printReceived)(received.mock.calls.length)}`
- : '');
- return {
- message,
- pass
- };
- };
- const createToBeCalledTimesMatcher = matcherName =>
- function (received, expected) {
- const expectedArgument = 'expected';
- const options = {
- isNot: this.isNot,
- promise: this.promise
- };
- (0, _jestMatcherUtils.ensureExpectedIsNonNegativeInteger)(
- expected,
- matcherName,
- options
- );
- ensureMockOrSpy(received, matcherName, expectedArgument, options);
- const receivedIsSpy = isSpy(received);
- const receivedName = receivedIsSpy ? 'spy' : received.getMockName();
- const count = receivedIsSpy
- ? received.calls.count()
- : received.mock.calls.length;
- const pass = count === expected;
- const message = pass
- ? () =>
- (0, _jestMatcherUtils.matcherHint)(
- matcherName,
- receivedName,
- expectedArgument,
- options
- ) +
- `\n\n` +
- `Expected number of calls: not ${(0, _jestMatcherUtils.printExpected)(
- expected
- )}`
- : () =>
- (0, _jestMatcherUtils.matcherHint)(
- matcherName,
- receivedName,
- expectedArgument,
- options
- ) +
- '\n\n' +
- `Expected number of calls: ${(0, _jestMatcherUtils.printExpected)(
- expected
- )}\n` +
- `Received number of calls: ${(0, _jestMatcherUtils.printReceived)(
- count
- )}`;
- return {
- message,
- pass
- };
- };
- const createToReturnTimesMatcher = matcherName =>
- function (received, expected) {
- const expectedArgument = 'expected';
- const options = {
- isNot: this.isNot,
- promise: this.promise
- };
- (0, _jestMatcherUtils.ensureExpectedIsNonNegativeInteger)(
- expected,
- matcherName,
- options
- );
- ensureMock(received, matcherName, expectedArgument, options);
- const receivedName = received.getMockName(); // Count return values that correspond only to calls that returned
- const count = received.mock.results.reduce(
- (n, result) => (result.type === 'return' ? n + 1 : n),
- 0
- );
- const pass = count === expected;
- const message = pass
- ? () =>
- (0, _jestMatcherUtils.matcherHint)(
- matcherName,
- receivedName,
- expectedArgument,
- options
- ) +
- `\n\n` +
- `Expected number of returns: not ${(0,
- _jestMatcherUtils.printExpected)(expected)}` +
- (received.mock.calls.length !== count
- ? `\n\nReceived number of calls: ${(0,
- _jestMatcherUtils.printReceived)(received.mock.calls.length)}`
- : '')
- : () =>
- (0, _jestMatcherUtils.matcherHint)(
- matcherName,
- receivedName,
- expectedArgument,
- options
- ) +
- '\n\n' +
- `Expected number of returns: ${(0, _jestMatcherUtils.printExpected)(
- expected
- )}\n` +
- `Received number of returns: ${(0, _jestMatcherUtils.printReceived)(
- count
- )}` +
- (received.mock.calls.length !== count
- ? `\nReceived number of calls: ${(0,
- _jestMatcherUtils.printReceived)(received.mock.calls.length)}`
- : '');
- return {
- message,
- pass
- };
- };
- const createToBeCalledWithMatcher = matcherName =>
- function (received, ...expected) {
- const expectedArgument = '...expected';
- const options = {
- isNot: this.isNot,
- promise: this.promise
- };
- ensureMockOrSpy(received, matcherName, expectedArgument, options);
- const receivedIsSpy = isSpy(received);
- const receivedName = receivedIsSpy ? 'spy' : received.getMockName();
- const calls = receivedIsSpy
- ? received.calls.all().map(x => x.args)
- : received.mock.calls;
- const pass = calls.some(call => isEqualCall(expected, call));
- const message = pass
- ? () => {
- // Some examples of calls that are equal to expected value.
- const indexedCalls = [];
- let i = 0;
- while (i < calls.length && indexedCalls.length < PRINT_LIMIT) {
- if (isEqualCall(expected, calls[i])) {
- indexedCalls.push([i, calls[i]]);
- }
- i += 1;
- }
- return (
- (0, _jestMatcherUtils.matcherHint)(
- matcherName,
- receivedName,
- expectedArgument,
- options
- ) +
- '\n\n' +
- `Expected: not ${printExpectedArgs(expected)}\n` +
- (calls.length === 1 &&
- (0, _jestMatcherUtils.stringify)(calls[0]) ===
- (0, _jestMatcherUtils.stringify)(expected)
- ? ''
- : printReceivedCallsNegative(
- expected,
- indexedCalls,
- calls.length === 1
- )) +
- `\nNumber of calls: ${(0, _jestMatcherUtils.printReceived)(
- calls.length
- )}`
- );
- }
- : () => {
- // Some examples of calls that are not equal to expected value.
- const indexedCalls = [];
- let i = 0;
- while (i < calls.length && indexedCalls.length < PRINT_LIMIT) {
- indexedCalls.push([i, calls[i]]);
- i += 1;
- }
- return (
- (0, _jestMatcherUtils.matcherHint)(
- matcherName,
- receivedName,
- expectedArgument,
- options
- ) +
- '\n\n' +
- printExpectedReceivedCallsPositive(
- expected,
- indexedCalls,
- isExpand(this.expand),
- calls.length === 1
- ) +
- `\nNumber of calls: ${(0, _jestMatcherUtils.printReceived)(
- calls.length
- )}`
- );
- };
- return {
- message,
- pass
- };
- };
- const createToReturnWithMatcher = matcherName =>
- function (received, expected) {
- const expectedArgument = 'expected';
- const options = {
- isNot: this.isNot,
- promise: this.promise
- };
- ensureMock(received, matcherName, expectedArgument, options);
- const receivedName = received.getMockName();
- const {calls, results} = received.mock;
- const pass = results.some(result => isEqualReturn(expected, result));
- const message = pass
- ? () => {
- // Some examples of results that are equal to expected value.
- const indexedResults = [];
- let i = 0;
- while (i < results.length && indexedResults.length < PRINT_LIMIT) {
- if (isEqualReturn(expected, results[i])) {
- indexedResults.push([i, results[i]]);
- }
- i += 1;
- }
- return (
- (0, _jestMatcherUtils.matcherHint)(
- matcherName,
- receivedName,
- expectedArgument,
- options
- ) +
- '\n\n' +
- `Expected: not ${(0, _jestMatcherUtils.printExpected)(
- expected
- )}\n` +
- (results.length === 1 &&
- results[0].type === 'return' &&
- (0, _jestMatcherUtils.stringify)(results[0].value) ===
- (0, _jestMatcherUtils.stringify)(expected)
- ? ''
- : printReceivedResults(
- 'Received: ',
- expected,
- indexedResults,
- results.length === 1
- )) +
- printNumberOfReturns(countReturns(results), calls.length)
- );
- }
- : () => {
- // Some examples of results that are not equal to expected value.
- const indexedResults = [];
- let i = 0;
- while (i < results.length && indexedResults.length < PRINT_LIMIT) {
- indexedResults.push([i, results[i]]);
- i += 1;
- }
- return (
- (0, _jestMatcherUtils.matcherHint)(
- matcherName,
- receivedName,
- expectedArgument,
- options
- ) +
- '\n\n' +
- `Expected: ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
- printReceivedResults(
- 'Received: ',
- expected,
- indexedResults,
- results.length === 1
- ) +
- printNumberOfReturns(countReturns(results), calls.length)
- );
- };
- return {
- message,
- pass
- };
- };
- const createLastCalledWithMatcher = matcherName =>
- function (received, ...expected) {
- const expectedArgument = '...expected';
- const options = {
- isNot: this.isNot,
- promise: this.promise
- };
- ensureMockOrSpy(received, matcherName, expectedArgument, options);
- const receivedIsSpy = isSpy(received);
- const receivedName = receivedIsSpy ? 'spy' : received.getMockName();
- const calls = receivedIsSpy
- ? received.calls.all().map(x => x.args)
- : received.mock.calls;
- const iLast = calls.length - 1;
- const pass = iLast >= 0 && isEqualCall(expected, calls[iLast]);
- const message = pass
- ? () => {
- const indexedCalls = [];
- if (iLast > 0) {
- // Display preceding call as context.
- indexedCalls.push([iLast - 1, calls[iLast - 1]]);
- }
- indexedCalls.push([iLast, calls[iLast]]);
- return (
- (0, _jestMatcherUtils.matcherHint)(
- matcherName,
- receivedName,
- expectedArgument,
- options
- ) +
- '\n\n' +
- `Expected: not ${printExpectedArgs(expected)}\n` +
- (calls.length === 1 &&
- (0, _jestMatcherUtils.stringify)(calls[0]) ===
- (0, _jestMatcherUtils.stringify)(expected)
- ? ''
- : printReceivedCallsNegative(
- expected,
- indexedCalls,
- calls.length === 1,
- iLast
- )) +
- `\nNumber of calls: ${(0, _jestMatcherUtils.printReceived)(
- calls.length
- )}`
- );
- }
- : () => {
- const indexedCalls = [];
- if (iLast >= 0) {
- if (iLast > 0) {
- let i = iLast - 1; // Is there a preceding call that is equal to expected args?
- while (i >= 0 && !isEqualCall(expected, calls[i])) {
- i -= 1;
- }
- if (i < 0) {
- i = iLast - 1; // otherwise, preceding call
- }
- indexedCalls.push([i, calls[i]]);
- }
- indexedCalls.push([iLast, calls[iLast]]);
- }
- return (
- (0, _jestMatcherUtils.matcherHint)(
- matcherName,
- receivedName,
- expectedArgument,
- options
- ) +
- '\n\n' +
- printExpectedReceivedCallsPositive(
- expected,
- indexedCalls,
- isExpand(this.expand),
- calls.length === 1,
- iLast
- ) +
- `\nNumber of calls: ${(0, _jestMatcherUtils.printReceived)(
- calls.length
- )}`
- );
- };
- return {
- message,
- pass
- };
- };
- const createLastReturnedMatcher = matcherName =>
- function (received, expected) {
- const expectedArgument = 'expected';
- const options = {
- isNot: this.isNot,
- promise: this.promise
- };
- ensureMock(received, matcherName, expectedArgument, options);
- const receivedName = received.getMockName();
- const {calls, results} = received.mock;
- const iLast = results.length - 1;
- const pass = iLast >= 0 && isEqualReturn(expected, results[iLast]);
- const message = pass
- ? () => {
- const indexedResults = [];
- if (iLast > 0) {
- // Display preceding result as context.
- indexedResults.push([iLast - 1, results[iLast - 1]]);
- }
- indexedResults.push([iLast, results[iLast]]);
- return (
- (0, _jestMatcherUtils.matcherHint)(
- matcherName,
- receivedName,
- expectedArgument,
- options
- ) +
- '\n\n' +
- `Expected: not ${(0, _jestMatcherUtils.printExpected)(
- expected
- )}\n` +
- (results.length === 1 &&
- results[0].type === 'return' &&
- (0, _jestMatcherUtils.stringify)(results[0].value) ===
- (0, _jestMatcherUtils.stringify)(expected)
- ? ''
- : printReceivedResults(
- 'Received: ',
- expected,
- indexedResults,
- results.length === 1,
- iLast
- )) +
- printNumberOfReturns(countReturns(results), calls.length)
- );
- }
- : () => {
- const indexedResults = [];
- if (iLast >= 0) {
- if (iLast > 0) {
- let i = iLast - 1; // Is there a preceding result that is equal to expected value?
- while (i >= 0 && !isEqualReturn(expected, results[i])) {
- i -= 1;
- }
- if (i < 0) {
- i = iLast - 1; // otherwise, preceding result
- }
- indexedResults.push([i, results[i]]);
- }
- indexedResults.push([iLast, results[iLast]]);
- }
- return (
- (0, _jestMatcherUtils.matcherHint)(
- matcherName,
- receivedName,
- expectedArgument,
- options
- ) +
- '\n\n' +
- `Expected: ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
- printReceivedResults(
- 'Received: ',
- expected,
- indexedResults,
- results.length === 1,
- iLast
- ) +
- printNumberOfReturns(countReturns(results), calls.length)
- );
- };
- return {
- message,
- pass
- };
- };
- const createNthCalledWithMatcher = matcherName =>
- function (received, nth, ...expected) {
- const expectedArgument = 'n';
- const options = {
- expectedColor: arg => arg,
- isNot: this.isNot,
- promise: this.promise,
- secondArgument: '...expected'
- };
- ensureMockOrSpy(received, matcherName, expectedArgument, options);
- if (!Number.isSafeInteger(nth) || nth < 1) {
- throw new Error(
- (0, _jestMatcherUtils.matcherErrorMessage)(
- (0, _jestMatcherUtils.matcherHint)(
- matcherName,
- undefined,
- expectedArgument,
- options
- ),
- `${expectedArgument} must be a positive integer`,
- (0, _jestMatcherUtils.printWithType)(
- expectedArgument,
- nth,
- _jestMatcherUtils.stringify
- )
- )
- );
- }
- const receivedIsSpy = isSpy(received);
- const receivedName = receivedIsSpy ? 'spy' : received.getMockName();
- const calls = receivedIsSpy
- ? received.calls.all().map(x => x.args)
- : received.mock.calls;
- const length = calls.length;
- const iNth = nth - 1;
- const pass = iNth < length && isEqualCall(expected, calls[iNth]);
- const message = pass
- ? () => {
- // Display preceding and following calls,
- // in case assertions fails because index is off by one.
- const indexedCalls = [];
- if (iNth - 1 >= 0) {
- indexedCalls.push([iNth - 1, calls[iNth - 1]]);
- }
- indexedCalls.push([iNth, calls[iNth]]);
- if (iNth + 1 < length) {
- indexedCalls.push([iNth + 1, calls[iNth + 1]]);
- }
- return (
- (0, _jestMatcherUtils.matcherHint)(
- matcherName,
- receivedName,
- expectedArgument,
- options
- ) +
- '\n\n' +
- `n: ${nth}\n` +
- `Expected: not ${printExpectedArgs(expected)}\n` +
- (calls.length === 1 &&
- (0, _jestMatcherUtils.stringify)(calls[0]) ===
- (0, _jestMatcherUtils.stringify)(expected)
- ? ''
- : printReceivedCallsNegative(
- expected,
- indexedCalls,
- calls.length === 1,
- iNth
- )) +
- `\nNumber of calls: ${(0, _jestMatcherUtils.printReceived)(
- calls.length
- )}`
- );
- }
- : () => {
- // Display preceding and following calls:
- // * nearest call that is equal to expected args
- // * otherwise, adjacent call
- // in case assertions fails because of index, especially off by one.
- const indexedCalls = [];
- if (iNth < length) {
- if (iNth - 1 >= 0) {
- let i = iNth - 1; // Is there a preceding call that is equal to expected args?
- while (i >= 0 && !isEqualCall(expected, calls[i])) {
- i -= 1;
- }
- if (i < 0) {
- i = iNth - 1; // otherwise, adjacent call
- }
- indexedCalls.push([i, calls[i]]);
- }
- indexedCalls.push([iNth, calls[iNth]]);
- if (iNth + 1 < length) {
- let i = iNth + 1; // Is there a following call that is equal to expected args?
- while (i < length && !isEqualCall(expected, calls[i])) {
- i += 1;
- }
- if (i >= length) {
- i = iNth + 1; // otherwise, adjacent call
- }
- indexedCalls.push([i, calls[i]]);
- }
- } else if (length > 0) {
- // The number of received calls is fewer than the expected number.
- let i = length - 1; // Is there a call that is equal to expected args?
- while (i >= 0 && !isEqualCall(expected, calls[i])) {
- i -= 1;
- }
- if (i < 0) {
- i = length - 1; // otherwise, last call
- }
- indexedCalls.push([i, calls[i]]);
- }
- return (
- (0, _jestMatcherUtils.matcherHint)(
- matcherName,
- receivedName,
- expectedArgument,
- options
- ) +
- '\n\n' +
- `n: ${nth}\n` +
- printExpectedReceivedCallsPositive(
- expected,
- indexedCalls,
- isExpand(this.expand),
- calls.length === 1,
- iNth
- ) +
- `\nNumber of calls: ${(0, _jestMatcherUtils.printReceived)(
- calls.length
- )}`
- );
- };
- return {
- message,
- pass
- };
- };
- const createNthReturnedWithMatcher = matcherName =>
- function (received, nth, expected) {
- const expectedArgument = 'n';
- const options = {
- expectedColor: arg => arg,
- isNot: this.isNot,
- promise: this.promise,
- secondArgument: 'expected'
- };
- ensureMock(received, matcherName, expectedArgument, options);
- if (!Number.isSafeInteger(nth) || nth < 1) {
- throw new Error(
- (0, _jestMatcherUtils.matcherErrorMessage)(
- (0, _jestMatcherUtils.matcherHint)(
- matcherName,
- undefined,
- expectedArgument,
- options
- ),
- `${expectedArgument} must be a positive integer`,
- (0, _jestMatcherUtils.printWithType)(
- expectedArgument,
- nth,
- _jestMatcherUtils.stringify
- )
- )
- );
- }
- const receivedName = received.getMockName();
- const {calls, results} = received.mock;
- const length = results.length;
- const iNth = nth - 1;
- const pass = iNth < length && isEqualReturn(expected, results[iNth]);
- const message = pass
- ? () => {
- // Display preceding and following results,
- // in case assertions fails because index is off by one.
- const indexedResults = [];
- if (iNth - 1 >= 0) {
- indexedResults.push([iNth - 1, results[iNth - 1]]);
- }
- indexedResults.push([iNth, results[iNth]]);
- if (iNth + 1 < length) {
- indexedResults.push([iNth + 1, results[iNth + 1]]);
- }
- return (
- (0, _jestMatcherUtils.matcherHint)(
- matcherName,
- receivedName,
- expectedArgument,
- options
- ) +
- '\n\n' +
- `n: ${nth}\n` +
- `Expected: not ${(0, _jestMatcherUtils.printExpected)(
- expected
- )}\n` +
- (results.length === 1 &&
- results[0].type === 'return' &&
- (0, _jestMatcherUtils.stringify)(results[0].value) ===
- (0, _jestMatcherUtils.stringify)(expected)
- ? ''
- : printReceivedResults(
- 'Received: ',
- expected,
- indexedResults,
- results.length === 1,
- iNth
- )) +
- printNumberOfReturns(countReturns(results), calls.length)
- );
- }
- : () => {
- // Display preceding and following results:
- // * nearest result that is equal to expected value
- // * otherwise, adjacent result
- // in case assertions fails because of index, especially off by one.
- const indexedResults = [];
- if (iNth < length) {
- if (iNth - 1 >= 0) {
- let i = iNth - 1; // Is there a preceding result that is equal to expected value?
- while (i >= 0 && !isEqualReturn(expected, results[i])) {
- i -= 1;
- }
- if (i < 0) {
- i = iNth - 1; // otherwise, adjacent result
- }
- indexedResults.push([i, results[i]]);
- }
- indexedResults.push([iNth, results[iNth]]);
- if (iNth + 1 < length) {
- let i = iNth + 1; // Is there a following result that is equal to expected value?
- while (i < length && !isEqualReturn(expected, results[i])) {
- i += 1;
- }
- if (i >= length) {
- i = iNth + 1; // otherwise, adjacent result
- }
- indexedResults.push([i, results[i]]);
- }
- } else if (length > 0) {
- // The number of received calls is fewer than the expected number.
- let i = length - 1; // Is there a result that is equal to expected value?
- while (i >= 0 && !isEqualReturn(expected, results[i])) {
- i -= 1;
- }
- if (i < 0) {
- i = length - 1; // otherwise, last result
- }
- indexedResults.push([i, results[i]]);
- }
- return (
- (0, _jestMatcherUtils.matcherHint)(
- matcherName,
- receivedName,
- expectedArgument,
- options
- ) +
- '\n\n' +
- `n: ${nth}\n` +
- `Expected: ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
- printReceivedResults(
- 'Received: ',
- expected,
- indexedResults,
- results.length === 1,
- iNth
- ) +
- printNumberOfReturns(countReturns(results), calls.length)
- );
- };
- return {
- message,
- pass
- };
- };
- const spyMatchers = {
- lastCalledWith: createLastCalledWithMatcher('lastCalledWith'),
- lastReturnedWith: createLastReturnedMatcher('lastReturnedWith'),
- nthCalledWith: createNthCalledWithMatcher('nthCalledWith'),
- nthReturnedWith: createNthReturnedWithMatcher('nthReturnedWith'),
- toBeCalled: createToBeCalledMatcher('toBeCalled'),
- toBeCalledTimes: createToBeCalledTimesMatcher('toBeCalledTimes'),
- toBeCalledWith: createToBeCalledWithMatcher('toBeCalledWith'),
- toHaveBeenCalled: createToBeCalledMatcher('toHaveBeenCalled'),
- toHaveBeenCalledTimes: createToBeCalledTimesMatcher('toHaveBeenCalledTimes'),
- toHaveBeenCalledWith: createToBeCalledWithMatcher('toHaveBeenCalledWith'),
- toHaveBeenLastCalledWith: createLastCalledWithMatcher(
- 'toHaveBeenLastCalledWith'
- ),
- toHaveBeenNthCalledWith: createNthCalledWithMatcher(
- 'toHaveBeenNthCalledWith'
- ),
- toHaveLastReturnedWith: createLastReturnedMatcher('toHaveLastReturnedWith'),
- toHaveNthReturnedWith: createNthReturnedWithMatcher('toHaveNthReturnedWith'),
- toHaveReturned: createToReturnMatcher('toHaveReturned'),
- toHaveReturnedTimes: createToReturnTimesMatcher('toHaveReturnedTimes'),
- toHaveReturnedWith: createToReturnWithMatcher('toHaveReturnedWith'),
- toReturn: createToReturnMatcher('toReturn'),
- toReturnTimes: createToReturnTimesMatcher('toReturnTimes'),
- toReturnWith: createToReturnWithMatcher('toReturnWith')
- };
- const isMock = received =>
- received != null && received._isMockFunction === true;
- const isSpy = received =>
- received != null &&
- received.calls != null &&
- typeof received.calls.all === 'function' &&
- typeof received.calls.count === 'function';
- const ensureMockOrSpy = (received, matcherName, expectedArgument, options) => {
- if (!isMock(received) && !isSpy(received)) {
- throw new Error(
- (0, _jestMatcherUtils.matcherErrorMessage)(
- (0, _jestMatcherUtils.matcherHint)(
- matcherName,
- undefined,
- expectedArgument,
- options
- ),
- `${(0, _jestMatcherUtils.RECEIVED_COLOR)(
- 'received'
- )} value must be a mock or spy function`,
- (0, _jestMatcherUtils.printWithType)(
- 'Received',
- received,
- _jestMatcherUtils.printReceived
- )
- )
- );
- }
- };
- const ensureMock = (received, matcherName, expectedArgument, options) => {
- if (!isMock(received)) {
- throw new Error(
- (0, _jestMatcherUtils.matcherErrorMessage)(
- (0, _jestMatcherUtils.matcherHint)(
- matcherName,
- undefined,
- expectedArgument,
- options
- ),
- `${(0, _jestMatcherUtils.RECEIVED_COLOR)(
- 'received'
- )} value must be a mock function`,
- (0, _jestMatcherUtils.printWithType)(
- 'Received',
- received,
- _jestMatcherUtils.printReceived
- )
- )
- );
- }
- };
- var _default = spyMatchers;
- exports.default = _default;
|