spy_matchers.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. var _slicedToArray = (function() {
  6. function sliceIterator(arr, i) {
  7. var _arr = [];
  8. var _n = true;
  9. var _d = false;
  10. var _e = undefined;
  11. try {
  12. for (
  13. var _i = arr[Symbol.iterator](), _s;
  14. !(_n = (_s = _i.next()).done);
  15. _n = true
  16. ) {
  17. _arr.push(_s.value);
  18. if (i && _arr.length === i) break;
  19. }
  20. } catch (err) {
  21. _d = true;
  22. _e = err;
  23. } finally {
  24. try {
  25. if (!_n && _i['return']) _i['return']();
  26. } finally {
  27. if (_d) throw _e;
  28. }
  29. }
  30. return _arr;
  31. }
  32. return function(arr, i) {
  33. if (Array.isArray(arr)) {
  34. return arr;
  35. } else if (Symbol.iterator in Object(arr)) {
  36. return sliceIterator(arr, i);
  37. } else {
  38. throw new TypeError(
  39. 'Invalid attempt to destructure non-iterable instance'
  40. );
  41. }
  42. };
  43. })();
  44. var _jestMatcherUtils = require('jest-matcher-utils');
  45. var _jasmine_utils = require('./jasmine_utils');
  46. var _utils = require('./utils');
  47. var _jestDiff = require('jest-diff');
  48. var _jestDiff2 = _interopRequireDefault(_jestDiff);
  49. function _interopRequireDefault(obj) {
  50. return obj && obj.__esModule ? obj : {default: obj};
  51. }
  52. const CALL_PRINT_LIMIT = 3;
  53. /**
  54. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  55. *
  56. * This source code is licensed under the MIT license found in the
  57. * LICENSE file in the root directory of this source tree.
  58. *
  59. *
  60. */
  61. const RETURN_PRINT_LIMIT = 5;
  62. const LAST_CALL_PRINT_LIMIT = 1;
  63. const createToBeCalledMatcher = matcherName => (received, expected) => {
  64. (0, _jestMatcherUtils.ensureNoExpected)(expected, matcherName);
  65. ensureMock(received, matcherName);
  66. const receivedIsSpy = isSpy(received);
  67. const type = receivedIsSpy ? 'spy' : 'mock function';
  68. const receivedName = receivedIsSpy ? 'spy' : received.getMockName();
  69. const identifier =
  70. receivedIsSpy || receivedName === 'jest.fn()'
  71. ? type
  72. : `${type} "${receivedName}"`;
  73. const count = receivedIsSpy
  74. ? received.calls.count()
  75. : received.mock.calls.length;
  76. const calls = receivedIsSpy
  77. ? received.calls.all().map(x => x.args)
  78. : received.mock.calls;
  79. const pass = count > 0;
  80. const message = pass
  81. ? () =>
  82. (0, _jestMatcherUtils.matcherHint)(
  83. '.not' + matcherName,
  84. receivedName,
  85. ''
  86. ) +
  87. '\n\n' +
  88. `Expected ${identifier} not to be called ` +
  89. formatReceivedCalls(calls, CALL_PRINT_LIMIT, {sameSentence: true})
  90. : () =>
  91. (0, _jestMatcherUtils.matcherHint)(matcherName, receivedName, '') +
  92. '\n\n' +
  93. `Expected ${identifier} to have been called, but it was not called.`;
  94. return {message: message, pass: pass};
  95. };
  96. const createToReturnMatcher = matcherName => (received, expected) => {
  97. (0, _jestMatcherUtils.ensureNoExpected)(expected, matcherName);
  98. ensureMock(received, matcherName);
  99. const receivedName = received.getMockName();
  100. const identifier =
  101. receivedName === 'jest.fn()'
  102. ? 'mock function'
  103. : `mock function "${receivedName}"`;
  104. // List of return values that correspond only to calls that did not throw
  105. // an error
  106. const returnValues = received.mock.results
  107. .filter(result => !result.isThrow)
  108. .map(result => result.value);
  109. const count = returnValues.length;
  110. const pass = count > 0;
  111. const message = pass
  112. ? () =>
  113. (0, _jestMatcherUtils.matcherHint)(
  114. '.not' + matcherName,
  115. receivedName,
  116. ''
  117. ) +
  118. '\n\n' +
  119. `Expected ${identifier} not to have returned, but it returned:\n` +
  120. ` ${getPrintedReturnValues(returnValues, RETURN_PRINT_LIMIT)}`
  121. : () =>
  122. (0, _jestMatcherUtils.matcherHint)(matcherName, receivedName, '') +
  123. '\n\n' +
  124. `Expected ${identifier} to have returned.`;
  125. return {message: message, pass: pass};
  126. };
  127. const createToBeCalledTimesMatcher = matcherName => (received, expected) => {
  128. (0, _jestMatcherUtils.ensureExpectedIsNumber)(expected, matcherName);
  129. ensureMock(received, matcherName);
  130. const receivedIsSpy = isSpy(received);
  131. const type = receivedIsSpy ? 'spy' : 'mock function';
  132. const receivedName = receivedIsSpy ? 'spy' : received.getMockName();
  133. const identifier =
  134. receivedIsSpy || receivedName === 'jest.fn()'
  135. ? type
  136. : `${type} "${receivedName}"`;
  137. const count = receivedIsSpy
  138. ? received.calls.count()
  139. : received.mock.calls.length;
  140. const pass = count === expected;
  141. const message = pass
  142. ? () =>
  143. (0, _jestMatcherUtils.matcherHint)(
  144. '.not' + matcherName,
  145. receivedName,
  146. String(expected)
  147. ) +
  148. `\n\n` +
  149. `Expected ${identifier} not to be called ` +
  150. `${(0, _jestMatcherUtils.EXPECTED_COLOR)(
  151. (0, _jestMatcherUtils.pluralize)('time', expected)
  152. )}, but it was` +
  153. ` called exactly ${(0, _jestMatcherUtils.RECEIVED_COLOR)(
  154. (0, _jestMatcherUtils.pluralize)('time', count)
  155. )}.`
  156. : () =>
  157. (0, _jestMatcherUtils.matcherHint)(
  158. matcherName,
  159. receivedName,
  160. String(expected)
  161. ) +
  162. '\n\n' +
  163. `Expected ${identifier} to have been called ` +
  164. `${(0, _jestMatcherUtils.EXPECTED_COLOR)(
  165. (0, _jestMatcherUtils.pluralize)('time', expected)
  166. )},` +
  167. ` but it was called ${(0, _jestMatcherUtils.RECEIVED_COLOR)(
  168. (0, _jestMatcherUtils.pluralize)('time', count)
  169. )}.`;
  170. return {message: message, pass: pass};
  171. };
  172. const createToReturnTimesMatcher = matcherName => (received, expected) => {
  173. (0, _jestMatcherUtils.ensureExpectedIsNumber)(expected, matcherName);
  174. ensureMock(received, matcherName);
  175. const receivedName = received.getMockName();
  176. const identifier =
  177. receivedName === 'jest.fn()'
  178. ? 'mock function'
  179. : `mock function "${receivedName}"`;
  180. // List of return results that correspond only to calls that did not throw
  181. // an error
  182. const returnResults = received.mock.results.filter(result => !result.isThrow);
  183. const count = returnResults.length;
  184. const pass = count === expected;
  185. const message = pass
  186. ? () =>
  187. (0, _jestMatcherUtils.matcherHint)(
  188. '.not' + matcherName,
  189. receivedName,
  190. String(expected)
  191. ) +
  192. `\n\n` +
  193. `Expected ${identifier} not to have returned ` +
  194. `${(0, _jestMatcherUtils.EXPECTED_COLOR)(
  195. (0, _jestMatcherUtils.pluralize)('time', expected)
  196. )}, but it` +
  197. ` returned exactly ${(0, _jestMatcherUtils.RECEIVED_COLOR)(
  198. (0, _jestMatcherUtils.pluralize)('time', count)
  199. )}.`
  200. : () =>
  201. (0, _jestMatcherUtils.matcherHint)(
  202. matcherName,
  203. receivedName,
  204. String(expected)
  205. ) +
  206. '\n\n' +
  207. `Expected ${identifier} to have returned ` +
  208. `${(0, _jestMatcherUtils.EXPECTED_COLOR)(
  209. (0, _jestMatcherUtils.pluralize)('time', expected)
  210. )},` +
  211. ` but it returned ${(0, _jestMatcherUtils.RECEIVED_COLOR)(
  212. (0, _jestMatcherUtils.pluralize)('time', count)
  213. )}.`;
  214. return {message: message, pass: pass};
  215. };
  216. const createToBeCalledWithMatcher = matcherName =>
  217. function(received) {
  218. for (
  219. var _len = arguments.length,
  220. expected = Array(_len > 1 ? _len - 1 : 0),
  221. _key = 1;
  222. _key < _len;
  223. _key++
  224. ) {
  225. expected[_key - 1] = arguments[_key];
  226. }
  227. ensureMock(received, matcherName);
  228. const receivedIsSpy = isSpy(received);
  229. const type = receivedIsSpy ? 'spy' : 'mock function';
  230. const receivedName = receivedIsSpy ? 'spy' : received.getMockName();
  231. const identifier =
  232. receivedIsSpy || receivedName === 'jest.fn()'
  233. ? type
  234. : `${type} "${receivedName}"`;
  235. const calls = receivedIsSpy
  236. ? received.calls.all().map(x => x.args)
  237. : received.mock.calls;
  238. var _partition = (0, _utils.partition)(calls, call =>
  239. (0, _jasmine_utils.equals)(call, expected, [_utils.iterableEquality])
  240. ),
  241. _partition2 = _slicedToArray(_partition, 2);
  242. const match = _partition2[0],
  243. fail = _partition2[1];
  244. const pass = match.length > 0;
  245. const message = pass
  246. ? () =>
  247. (0, _jestMatcherUtils.matcherHint)(
  248. '.not' + matcherName,
  249. receivedName
  250. ) +
  251. '\n\n' +
  252. `Expected ${identifier} not to have been called with:\n` +
  253. ` ${(0, _jestMatcherUtils.printExpected)(expected)}`
  254. : () =>
  255. (0, _jestMatcherUtils.matcherHint)(matcherName, receivedName) +
  256. '\n\n' +
  257. `Expected ${identifier} to have been called with:\n` +
  258. formatMismatchedCalls(fail, expected, CALL_PRINT_LIMIT);
  259. return {message: message, pass: pass};
  260. };
  261. const createToReturnWithMatcher = matcherName => (received, expected) => {
  262. ensureMock(received, matcherName);
  263. const receivedName = received.getMockName();
  264. const identifier =
  265. receivedName === 'jest.fn()'
  266. ? 'mock function'
  267. : `mock function "${receivedName}"`;
  268. // List of return values that correspond only to calls that did not throw
  269. // an error
  270. const returnValues = received.mock.results
  271. .filter(result => !result.isThrow)
  272. .map(result => result.value);
  273. var _partition3 = (0, _utils.partition)(returnValues, value =>
  274. (0, _jasmine_utils.equals)(expected, value, [_utils.iterableEquality])
  275. ),
  276. _partition4 = _slicedToArray(_partition3, 1);
  277. const match = _partition4[0];
  278. const pass = match.length > 0;
  279. const message = pass
  280. ? () =>
  281. (0, _jestMatcherUtils.matcherHint)('.not' + matcherName, receivedName) +
  282. '\n\n' +
  283. `Expected ${identifier} not to have returned:\n` +
  284. ` ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
  285. `But it returned exactly:\n` +
  286. ` ${(0, _jestMatcherUtils.printReceived)(expected)}`
  287. : () =>
  288. (0, _jestMatcherUtils.matcherHint)(matcherName, receivedName) +
  289. '\n\n' +
  290. `Expected ${identifier} to have returned:\n` +
  291. formatMismatchedReturnValues(
  292. returnValues,
  293. expected,
  294. RETURN_PRINT_LIMIT
  295. );
  296. return {message: message, pass: pass};
  297. };
  298. const createLastCalledWithMatcher = matcherName =>
  299. function(received) {
  300. for (
  301. var _len2 = arguments.length,
  302. expected = Array(_len2 > 1 ? _len2 - 1 : 0),
  303. _key2 = 1;
  304. _key2 < _len2;
  305. _key2++
  306. ) {
  307. expected[_key2 - 1] = arguments[_key2];
  308. }
  309. ensureMock(received, matcherName);
  310. const receivedIsSpy = isSpy(received);
  311. const type = receivedIsSpy ? 'spy' : 'mock function';
  312. const receivedName = receivedIsSpy ? 'spy' : received.getMockName();
  313. const identifier =
  314. receivedIsSpy || receivedName === 'jest.fn()'
  315. ? type
  316. : `${type} "${receivedName}"`;
  317. const calls = receivedIsSpy
  318. ? received.calls.all().map(x => x.args)
  319. : received.mock.calls;
  320. const pass = (0, _jasmine_utils.equals)(calls[calls.length - 1], expected, [
  321. _utils.iterableEquality
  322. ]);
  323. const message = pass
  324. ? () =>
  325. (0, _jestMatcherUtils.matcherHint)(
  326. '.not' + matcherName,
  327. receivedName
  328. ) +
  329. '\n\n' +
  330. `Expected ${identifier} to not have been last called with:\n` +
  331. ` ${(0, _jestMatcherUtils.printExpected)(expected)}`
  332. : () =>
  333. (0, _jestMatcherUtils.matcherHint)(matcherName, receivedName) +
  334. '\n\n' +
  335. `Expected ${identifier} to have been last called with:\n` +
  336. formatMismatchedCalls(calls, expected, LAST_CALL_PRINT_LIMIT);
  337. return {message: message, pass: pass};
  338. };
  339. const createLastReturnedMatcher = matcherName => (received, expected) => {
  340. ensureMock(received, matcherName);
  341. const receivedName = received.getMockName();
  342. const identifier =
  343. receivedName === 'jest.fn()'
  344. ? 'mock function'
  345. : `mock function "${receivedName}"`;
  346. const results = received.mock.results;
  347. const lastResult = results[results.length - 1];
  348. const pass =
  349. !!lastResult &&
  350. !lastResult.isThrow &&
  351. (0, _jasmine_utils.equals)(lastResult.value, expected, [
  352. _utils.iterableEquality
  353. ]);
  354. const message = pass
  355. ? () =>
  356. (0, _jestMatcherUtils.matcherHint)('.not' + matcherName, receivedName) +
  357. '\n\n' +
  358. `Expected ${identifier} to not have last returned:\n` +
  359. ` ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
  360. `But it last returned exactly:\n` +
  361. ` ${(0, _jestMatcherUtils.printReceived)(lastResult.value)}`
  362. : () =>
  363. (0, _jestMatcherUtils.matcherHint)(matcherName, receivedName) +
  364. '\n\n' +
  365. `Expected ${identifier} to have last returned:\n` +
  366. ` ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
  367. (!lastResult
  368. ? `But it was ${(0, _jestMatcherUtils.RECEIVED_COLOR)('not called')}`
  369. : lastResult.isThrow
  370. ? `But the last call ${(0, _jestMatcherUtils.RECEIVED_COLOR)(
  371. 'threw an error'
  372. )}`
  373. : `But the last call returned:\n ${(0,
  374. _jestMatcherUtils.printReceived)(lastResult.value)}`);
  375. return {message: message, pass: pass};
  376. };
  377. const createNthCalledWithMatcher = matcherName =>
  378. function(received, nth) {
  379. for (
  380. var _len3 = arguments.length,
  381. expected = Array(_len3 > 2 ? _len3 - 2 : 0),
  382. _key3 = 2;
  383. _key3 < _len3;
  384. _key3++
  385. ) {
  386. expected[_key3 - 2] = arguments[_key3];
  387. }
  388. ensureMock(received, matcherName);
  389. const receivedIsSpy = isSpy(received);
  390. const type = receivedIsSpy ? 'spy' : 'mock function';
  391. if (typeof nth !== 'number' || parseInt(nth, 10) !== nth || nth < 1) {
  392. const message = () =>
  393. `nth value ${(0, _jestMatcherUtils.printReceived)(
  394. nth
  395. )} must be a positive integer greater than ${(0,
  396. _jestMatcherUtils.printExpected)(0)}`;
  397. const pass = false;
  398. return {message: message, pass: pass};
  399. }
  400. const receivedName = receivedIsSpy ? 'spy' : received.getMockName();
  401. const identifier =
  402. receivedIsSpy || receivedName === 'jest.fn()'
  403. ? type
  404. : `${type} "${receivedName}"`;
  405. const calls = receivedIsSpy
  406. ? received.calls.all().map(x => x.args)
  407. : received.mock.calls;
  408. const pass = (0, _jasmine_utils.equals)(calls[nth - 1], expected, [
  409. _utils.iterableEquality
  410. ]);
  411. const message = pass
  412. ? () =>
  413. (0, _jestMatcherUtils.matcherHint)(
  414. '.not' + matcherName,
  415. receivedName
  416. ) +
  417. '\n\n' +
  418. `Expected ${identifier} ${nthToString(
  419. nth
  420. )} call to not have been called with:\n` +
  421. ` ${(0, _jestMatcherUtils.printExpected)(expected)}`
  422. : () =>
  423. (0, _jestMatcherUtils.matcherHint)(matcherName, receivedName) +
  424. '\n\n' +
  425. `Expected ${identifier} ${nthToString(
  426. nth
  427. )} call to have been called with:\n` +
  428. formatMismatchedCalls(
  429. calls[nth - 1] ? [calls[nth - 1]] : [],
  430. expected,
  431. LAST_CALL_PRINT_LIMIT
  432. );
  433. return {message: message, pass: pass};
  434. };
  435. const createNthReturnedWithMatcher = matcherName => (
  436. received,
  437. nth,
  438. expected
  439. ) => {
  440. ensureMock(received, matcherName);
  441. if (typeof nth !== 'number' || parseInt(nth, 10) !== nth || nth < 1) {
  442. const message = () =>
  443. `nth value ${(0, _jestMatcherUtils.printReceived)(
  444. nth
  445. )} must be a positive integer greater than ${(0,
  446. _jestMatcherUtils.printExpected)(0)}`;
  447. const pass = false;
  448. return {message: message, pass: pass};
  449. }
  450. const receivedName = received.getMockName();
  451. const identifier =
  452. receivedName === 'jest.fn()'
  453. ? 'mock function'
  454. : `mock function "${receivedName}"`;
  455. const results = received.mock.results;
  456. const nthResult = results[nth - 1];
  457. const pass =
  458. !!nthResult &&
  459. !nthResult.isThrow &&
  460. (0, _jasmine_utils.equals)(nthResult.value, expected, [
  461. _utils.iterableEquality
  462. ]);
  463. const nthString = nthToString(nth);
  464. const message = pass
  465. ? () =>
  466. (0, _jestMatcherUtils.matcherHint)('.not' + matcherName, receivedName) +
  467. '\n\n' +
  468. `Expected ${identifier} ${nthString} call to not have returned with:\n` +
  469. ` ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
  470. `But the ${nthString} call returned exactly:\n` +
  471. ` ${(0, _jestMatcherUtils.printReceived)(nthResult.value)}`
  472. : () =>
  473. (0, _jestMatcherUtils.matcherHint)(matcherName, receivedName) +
  474. '\n\n' +
  475. `Expected ${identifier} ${nthString} call to have returned with:\n` +
  476. ` ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
  477. (results.length === 0
  478. ? `But it was ${(0, _jestMatcherUtils.RECEIVED_COLOR)('not called')}`
  479. : nth > results.length
  480. ? `But it was only called ${(0, _jestMatcherUtils.printReceived)(
  481. results.length
  482. )} times`
  483. : nthResult.isThrow
  484. ? `But the ${nthString} call ${(0,
  485. _jestMatcherUtils.RECEIVED_COLOR)('threw an error')}`
  486. : `But the ${nthString} call returned with:\n ${(0,
  487. _jestMatcherUtils.printReceived)(nthResult.value)}`);
  488. return {message: message, pass: pass};
  489. };
  490. const spyMatchers = {
  491. lastCalledWith: createLastCalledWithMatcher('.lastCalledWith'),
  492. lastReturnedWith: createLastReturnedMatcher('.lastReturnedWith'),
  493. nthCalledWith: createNthCalledWithMatcher('.nthCalledWith'),
  494. nthReturnedWith: createNthReturnedWithMatcher('.nthReturnedWith'),
  495. toBeCalled: createToBeCalledMatcher('.toBeCalled'),
  496. toBeCalledTimes: createToBeCalledTimesMatcher('.toBeCalledTimes'),
  497. toBeCalledWith: createToBeCalledWithMatcher('.toBeCalledWith'),
  498. toHaveBeenCalled: createToBeCalledMatcher('.toHaveBeenCalled'),
  499. toHaveBeenCalledTimes: createToBeCalledTimesMatcher('.toHaveBeenCalledTimes'),
  500. toHaveBeenCalledWith: createToBeCalledWithMatcher('.toHaveBeenCalledWith'),
  501. toHaveBeenLastCalledWith: createLastCalledWithMatcher(
  502. '.toHaveBeenLastCalledWith'
  503. ),
  504. toHaveBeenNthCalledWith: createNthCalledWithMatcher(
  505. '.toHaveBeenNthCalledWith'
  506. ),
  507. toHaveLastReturnedWith: createLastReturnedMatcher('.toHaveLastReturnedWith'),
  508. toHaveNthReturnedWith: createNthReturnedWithMatcher('.toHaveNthReturnedWith'),
  509. toHaveReturned: createToReturnMatcher('.toHaveReturned'),
  510. toHaveReturnedTimes: createToReturnTimesMatcher('.toHaveReturnedTimes'),
  511. toHaveReturnedWith: createToReturnWithMatcher('.toHaveReturnedWith'),
  512. toReturn: createToReturnMatcher('.toReturn'),
  513. toReturnTimes: createToReturnTimesMatcher('.toReturnTimes'),
  514. toReturnWith: createToReturnWithMatcher('.toReturnWith')
  515. };
  516. const isSpy = spy => spy.calls && typeof spy.calls.count === 'function';
  517. const ensureMock = (mockOrSpy, matcherName) => {
  518. if (
  519. !mockOrSpy ||
  520. ((mockOrSpy.calls === undefined || mockOrSpy.calls.all === undefined) &&
  521. mockOrSpy._isMockFunction !== true)
  522. ) {
  523. throw new Error(
  524. (0, _jestMatcherUtils.matcherHint)(
  525. '[.not]' + matcherName,
  526. 'jest.fn()',
  527. ''
  528. ) +
  529. '\n\n' +
  530. `${(0, _jestMatcherUtils.RECEIVED_COLOR)(
  531. 'jest.fn()'
  532. )} value must be a mock function ` +
  533. `or spy.\n` +
  534. (0, _jestMatcherUtils.printWithType)(
  535. 'Received',
  536. mockOrSpy,
  537. _jestMatcherUtils.printReceived
  538. )
  539. );
  540. }
  541. };
  542. const getPrintedCalls = (calls, limit, sep, fn) => {
  543. const result = [];
  544. let i = calls.length;
  545. while (--i >= 0 && --limit >= 0) {
  546. result.push(fn(calls[i]));
  547. }
  548. return result.join(sep);
  549. };
  550. const getPrintedReturnValues = (calls, limit) => {
  551. const result = [];
  552. for (let i = 0; i < calls.length && i < limit; i += 1) {
  553. result.push((0, _jestMatcherUtils.printReceived)(calls[i]));
  554. }
  555. if (calls.length > limit) {
  556. result.push(
  557. `...and ${(0, _jestMatcherUtils.printReceived)(
  558. calls.length - limit
  559. )} more`
  560. );
  561. }
  562. return result.join('\n\n ');
  563. };
  564. const formatReceivedCalls = (calls, limit, options) => {
  565. if (calls.length) {
  566. const but = options && options.sameSentence ? 'but' : 'But';
  567. const count = calls.length - limit;
  568. const printedCalls = getPrintedCalls(
  569. calls,
  570. limit,
  571. ', ',
  572. _jestMatcherUtils.printReceived
  573. );
  574. return (
  575. `${but} it was called ` +
  576. `with:\n ` +
  577. printedCalls +
  578. (count > 0
  579. ? '\nand ' +
  580. (0, _jestMatcherUtils.RECEIVED_COLOR)(
  581. (0, _jestMatcherUtils.pluralize)('more call', count)
  582. ) +
  583. '.'
  584. : '')
  585. );
  586. } else {
  587. return `But it was ${(0, _jestMatcherUtils.RECEIVED_COLOR)('not called')}.`;
  588. }
  589. };
  590. const formatMismatchedCalls = (calls, expected, limit) => {
  591. if (calls.length) {
  592. return getPrintedCalls(
  593. calls,
  594. limit,
  595. '\n\n',
  596. formatMismatchedArgs.bind(null, expected)
  597. );
  598. } else {
  599. return (
  600. ` ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
  601. `But it was ${(0, _jestMatcherUtils.RECEIVED_COLOR)('not called')}.`
  602. );
  603. }
  604. };
  605. const formatMismatchedReturnValues = (returnValues, expected, limit) => {
  606. if (returnValues.length) {
  607. return (
  608. ` ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
  609. `But it returned:\n` +
  610. ` ${getPrintedReturnValues(returnValues, limit)}`
  611. );
  612. } else {
  613. return (
  614. ` ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
  615. `But it did ${(0, _jestMatcherUtils.RECEIVED_COLOR)('not return')}.`
  616. );
  617. }
  618. };
  619. const formatMismatchedArgs = (expected, received) => {
  620. const length = Math.max(expected.length, received.length);
  621. const printedArgs = [];
  622. for (let i = 0; i < length; i++) {
  623. if (
  624. !(0, _jasmine_utils.equals)(expected[i], received[i], [
  625. _utils.iterableEquality
  626. ])
  627. ) {
  628. const oneline = (0, _utils.isOneline)(expected[i], received[i]);
  629. const diffString = (0, _jestDiff2.default)(expected[i], received[i]);
  630. printedArgs.push(
  631. ` ${(0, _jestMatcherUtils.printExpected)(expected[i])}\n` +
  632. `as argument ${i + 1}, but it was called with\n` +
  633. ` ${(0, _jestMatcherUtils.printReceived)(received[i])}.` +
  634. (diffString && !oneline ? `\n\nDifference:\n\n${diffString}` : '')
  635. );
  636. } else if (i >= expected.length) {
  637. printedArgs.push(
  638. ` Did not expect argument ${i + 1} ` +
  639. `but it was called with ${(0, _jestMatcherUtils.printReceived)(
  640. received[i]
  641. )}.`
  642. );
  643. }
  644. }
  645. return printedArgs.join('\n');
  646. };
  647. const nthToString = nth => {
  648. switch (nth) {
  649. case 1:
  650. return 'first';
  651. case 2:
  652. return 'second';
  653. case 3:
  654. return 'third';
  655. }
  656. return `${nth}th`;
  657. };
  658. exports.default = spyMatchers;