matchers.js 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _jestGetType = _interopRequireWildcard(require('jest-get-type'));
  7. var _jestMatcherUtils = require('jest-matcher-utils');
  8. var _print = require('./print');
  9. var _utils = require('./utils');
  10. var _jasmineUtils = require('./jasmineUtils');
  11. function _interopRequireWildcard(obj) {
  12. if (obj && obj.__esModule) {
  13. return obj;
  14. } else {
  15. var newObj = {};
  16. if (obj != null) {
  17. for (var key in obj) {
  18. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  19. var desc =
  20. Object.defineProperty && Object.getOwnPropertyDescriptor
  21. ? Object.getOwnPropertyDescriptor(obj, key)
  22. : {};
  23. if (desc.get || desc.set) {
  24. Object.defineProperty(newObj, key, desc);
  25. } else {
  26. newObj[key] = obj[key];
  27. }
  28. }
  29. }
  30. }
  31. newObj.default = obj;
  32. return newObj;
  33. }
  34. }
  35. /**
  36. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  37. *
  38. * This source code is licensed under the MIT license found in the
  39. * LICENSE file in the root directory of this source tree.
  40. *
  41. */
  42. // Omit colon and one or more spaces, so can call getLabelPrinter.
  43. const EXPECTED_LABEL = 'Expected';
  44. const RECEIVED_LABEL = 'Received';
  45. const EXPECTED_VALUE_LABEL = 'Expected value';
  46. const RECEIVED_VALUE_LABEL = 'Received value'; // The optional property of matcher context is true if undefined.
  47. const isExpand = expand => expand !== false;
  48. const toStrictEqualTesters = [
  49. _utils.iterableEquality,
  50. _utils.typeEquality,
  51. _utils.sparseArrayEquality
  52. ];
  53. const matchers = {
  54. toBe(received, expected) {
  55. const matcherName = 'toBe';
  56. const options = {
  57. comment: 'Object.is equality',
  58. isNot: this.isNot,
  59. promise: this.promise
  60. };
  61. const pass = Object.is(received, expected);
  62. const message = pass
  63. ? () =>
  64. (0, _jestMatcherUtils.matcherHint)(
  65. matcherName,
  66. undefined,
  67. undefined,
  68. options
  69. ) +
  70. '\n\n' +
  71. `Expected: not ${(0, _jestMatcherUtils.printExpected)(expected)}`
  72. : () => {
  73. const expectedType = (0, _jestGetType.default)(expected);
  74. let deepEqualityName = null;
  75. if (expectedType !== 'map' && expectedType !== 'set') {
  76. // If deep equality passes when referential identity fails,
  77. // but exclude map and set until review of their equality logic.
  78. if (
  79. (0, _jasmineUtils.equals)(
  80. received,
  81. expected,
  82. toStrictEqualTesters,
  83. true
  84. )
  85. ) {
  86. deepEqualityName = 'toStrictEqual';
  87. } else if (
  88. (0, _jasmineUtils.equals)(received, expected, [
  89. _utils.iterableEquality
  90. ])
  91. ) {
  92. deepEqualityName = 'toEqual';
  93. }
  94. }
  95. return (
  96. (0, _jestMatcherUtils.matcherHint)(
  97. matcherName,
  98. undefined,
  99. undefined,
  100. options
  101. ) +
  102. '\n\n' +
  103. (deepEqualityName !== null
  104. ? (0, _jestMatcherUtils.DIM_COLOR)(
  105. `If it should pass with deep equality, replace "${matcherName}" with "${deepEqualityName}"`
  106. ) + '\n\n'
  107. : '') +
  108. (0, _jestMatcherUtils.printDiffOrStringify)(
  109. expected,
  110. received,
  111. EXPECTED_LABEL,
  112. RECEIVED_LABEL,
  113. isExpand(this.expand)
  114. )
  115. );
  116. }; // Passing the actual and expected objects so that a custom reporter
  117. // could access them, for example in order to display a custom visual diff,
  118. // or create a different error message
  119. return {
  120. actual: received,
  121. expected,
  122. message,
  123. name: matcherName,
  124. pass
  125. };
  126. },
  127. toBeCloseTo(received, expected, precision = 2) {
  128. const matcherName = 'toBeCloseTo';
  129. const secondArgument = arguments.length === 3 ? 'precision' : undefined;
  130. const options = {
  131. isNot: this.isNot,
  132. promise: this.promise,
  133. secondArgument,
  134. secondArgumentColor: arg => arg
  135. };
  136. (0, _jestMatcherUtils.ensureNumbers)(
  137. received,
  138. expected,
  139. matcherName,
  140. options
  141. );
  142. let pass = false;
  143. let expectedDiff = 0;
  144. let receivedDiff = 0;
  145. if (received === Infinity && expected === Infinity) {
  146. pass = true; // Infinity - Infinity is NaN
  147. } else if (received === -Infinity && expected === -Infinity) {
  148. pass = true; // -Infinity - -Infinity is NaN
  149. } else {
  150. expectedDiff = Math.pow(10, -precision) / 2;
  151. receivedDiff = Math.abs(expected - received);
  152. pass = receivedDiff < expectedDiff;
  153. }
  154. const message = pass
  155. ? () =>
  156. (0, _jestMatcherUtils.matcherHint)(
  157. matcherName,
  158. undefined,
  159. undefined,
  160. options
  161. ) +
  162. '\n\n' +
  163. `Expected: not ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
  164. (receivedDiff === 0
  165. ? ''
  166. : `Received: ${(0, _jestMatcherUtils.printReceived)(
  167. received
  168. )}\n` +
  169. '\n' +
  170. `Expected precision: ${(0, _jestMatcherUtils.stringify)(
  171. precision
  172. )}\n` +
  173. `Expected difference: not < ${(0,
  174. _jestMatcherUtils.printExpected)(expectedDiff)}\n` +
  175. `Received difference: ${(0,
  176. _jestMatcherUtils.printReceived)(receivedDiff)}`)
  177. : () =>
  178. (0, _jestMatcherUtils.matcherHint)(
  179. matcherName,
  180. undefined,
  181. undefined,
  182. options
  183. ) +
  184. '\n\n' +
  185. `Expected: ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
  186. `Received: ${(0, _jestMatcherUtils.printReceived)(received)}\n` +
  187. '\n' +
  188. `Expected precision: ${(0, _jestMatcherUtils.stringify)(
  189. precision
  190. )}\n` +
  191. `Expected difference: < ${(0, _jestMatcherUtils.printExpected)(
  192. expectedDiff
  193. )}\n` +
  194. `Received difference: ${(0, _jestMatcherUtils.printReceived)(
  195. receivedDiff
  196. )}`;
  197. return {
  198. message,
  199. pass
  200. };
  201. },
  202. toBeDefined(received, expected) {
  203. const matcherName = 'toBeDefined';
  204. const options = {
  205. isNot: this.isNot,
  206. promise: this.promise
  207. };
  208. (0, _jestMatcherUtils.ensureNoExpected)(expected, matcherName, options);
  209. const pass = received !== void 0;
  210. const message = () =>
  211. (0, _jestMatcherUtils.matcherHint)(matcherName, undefined, '', options) +
  212. '\n\n' +
  213. `Received: ${(0, _jestMatcherUtils.printReceived)(received)}`;
  214. return {
  215. message,
  216. pass
  217. };
  218. },
  219. toBeFalsy(received, expected) {
  220. const matcherName = 'toBeFalsy';
  221. const options = {
  222. isNot: this.isNot,
  223. promise: this.promise
  224. };
  225. (0, _jestMatcherUtils.ensureNoExpected)(expected, matcherName, options);
  226. const pass = !received;
  227. const message = () =>
  228. (0, _jestMatcherUtils.matcherHint)(matcherName, undefined, '', options) +
  229. '\n\n' +
  230. `Received: ${(0, _jestMatcherUtils.printReceived)(received)}`;
  231. return {
  232. message,
  233. pass
  234. };
  235. },
  236. toBeGreaterThan(received, expected) {
  237. const matcherName = 'toBeGreaterThan';
  238. const isNot = this.isNot;
  239. const options = {
  240. isNot,
  241. promise: this.promise
  242. };
  243. (0, _jestMatcherUtils.ensureNumbers)(
  244. received,
  245. expected,
  246. matcherName,
  247. options
  248. );
  249. const pass = received > expected;
  250. const message = () =>
  251. (0, _jestMatcherUtils.matcherHint)(
  252. matcherName,
  253. undefined,
  254. undefined,
  255. options
  256. ) +
  257. '\n\n' +
  258. `Expected:${isNot ? ' not' : ''} > ${(0, _jestMatcherUtils.printExpected)(
  259. expected
  260. )}\n` +
  261. `Received:${isNot ? ' ' : ''} ${(0, _jestMatcherUtils.printReceived)(
  262. received
  263. )}`;
  264. return {
  265. message,
  266. pass
  267. };
  268. },
  269. toBeGreaterThanOrEqual(received, expected) {
  270. const matcherName = 'toBeGreaterThanOrEqual';
  271. const isNot = this.isNot;
  272. const options = {
  273. isNot,
  274. promise: this.promise
  275. };
  276. (0, _jestMatcherUtils.ensureNumbers)(
  277. received,
  278. expected,
  279. matcherName,
  280. options
  281. );
  282. const pass = received >= expected;
  283. const message = () =>
  284. (0, _jestMatcherUtils.matcherHint)(
  285. matcherName,
  286. undefined,
  287. undefined,
  288. options
  289. ) +
  290. '\n\n' +
  291. `Expected:${isNot ? ' not' : ''} >= ${(0,
  292. _jestMatcherUtils.printExpected)(expected)}\n` +
  293. `Received:${isNot ? ' ' : ''} ${(0,
  294. _jestMatcherUtils.printReceived)(received)}`;
  295. return {
  296. message,
  297. pass
  298. };
  299. },
  300. toBeInstanceOf(received, expected) {
  301. const matcherName = 'toBeInstanceOf';
  302. const options = {
  303. isNot: this.isNot,
  304. promise: this.promise
  305. };
  306. if (typeof expected !== 'function') {
  307. throw new Error(
  308. (0, _jestMatcherUtils.matcherErrorMessage)(
  309. (0, _jestMatcherUtils.matcherHint)(
  310. matcherName,
  311. undefined,
  312. undefined,
  313. options
  314. ),
  315. `${(0, _jestMatcherUtils.EXPECTED_COLOR)(
  316. 'expected'
  317. )} value must be a function`,
  318. (0, _jestMatcherUtils.printWithType)(
  319. 'Expected',
  320. expected,
  321. _jestMatcherUtils.printExpected
  322. )
  323. )
  324. );
  325. }
  326. const pass = received instanceof expected;
  327. const message = pass
  328. ? () =>
  329. (0, _jestMatcherUtils.matcherHint)(
  330. matcherName,
  331. undefined,
  332. undefined,
  333. options
  334. ) +
  335. '\n\n' +
  336. (0, _print.printExpectedConstructorNameNot)(
  337. 'Expected constructor',
  338. expected
  339. ) +
  340. (typeof received.constructor === 'function' &&
  341. received.constructor !== expected
  342. ? (0, _print.printReceivedConstructorNameNot)(
  343. 'Received constructor',
  344. received.constructor,
  345. expected
  346. )
  347. : '')
  348. : () =>
  349. (0, _jestMatcherUtils.matcherHint)(
  350. matcherName,
  351. undefined,
  352. undefined,
  353. options
  354. ) +
  355. '\n\n' +
  356. (0, _print.printExpectedConstructorName)(
  357. 'Expected constructor',
  358. expected
  359. ) +
  360. ((0, _jestGetType.isPrimitive)(received) ||
  361. Object.getPrototypeOf(received) === null
  362. ? `\nReceived value has no prototype\nReceived value: ${(0,
  363. _jestMatcherUtils.printReceived)(received)}`
  364. : typeof received.constructor !== 'function'
  365. ? `\nReceived value: ${(0, _jestMatcherUtils.printReceived)(
  366. received
  367. )}`
  368. : (0, _print.printReceivedConstructorName)(
  369. 'Received constructor',
  370. received.constructor
  371. ));
  372. return {
  373. message,
  374. pass
  375. };
  376. },
  377. toBeLessThan(received, expected) {
  378. const matcherName = 'toBeLessThan';
  379. const isNot = this.isNot;
  380. const options = {
  381. isNot,
  382. promise: this.promise
  383. };
  384. (0, _jestMatcherUtils.ensureNumbers)(
  385. received,
  386. expected,
  387. matcherName,
  388. options
  389. );
  390. const pass = received < expected;
  391. const message = () =>
  392. (0, _jestMatcherUtils.matcherHint)(
  393. matcherName,
  394. undefined,
  395. undefined,
  396. options
  397. ) +
  398. '\n\n' +
  399. `Expected:${isNot ? ' not' : ''} < ${(0, _jestMatcherUtils.printExpected)(
  400. expected
  401. )}\n` +
  402. `Received:${isNot ? ' ' : ''} ${(0, _jestMatcherUtils.printReceived)(
  403. received
  404. )}`;
  405. return {
  406. message,
  407. pass
  408. };
  409. },
  410. toBeLessThanOrEqual(received, expected) {
  411. const matcherName = 'toBeLessThanOrEqual';
  412. const isNot = this.isNot;
  413. const options = {
  414. isNot,
  415. promise: this.promise
  416. };
  417. (0, _jestMatcherUtils.ensureNumbers)(
  418. received,
  419. expected,
  420. matcherName,
  421. options
  422. );
  423. const pass = received <= expected;
  424. const message = () =>
  425. (0, _jestMatcherUtils.matcherHint)(
  426. matcherName,
  427. undefined,
  428. undefined,
  429. options
  430. ) +
  431. '\n\n' +
  432. `Expected:${isNot ? ' not' : ''} <= ${(0,
  433. _jestMatcherUtils.printExpected)(expected)}\n` +
  434. `Received:${isNot ? ' ' : ''} ${(0,
  435. _jestMatcherUtils.printReceived)(received)}`;
  436. return {
  437. message,
  438. pass
  439. };
  440. },
  441. toBeNaN(received, expected) {
  442. const matcherName = 'toBeNaN';
  443. const options = {
  444. isNot: this.isNot,
  445. promise: this.promise
  446. };
  447. (0, _jestMatcherUtils.ensureNoExpected)(expected, matcherName, options);
  448. const pass = Number.isNaN(received);
  449. const message = () =>
  450. (0, _jestMatcherUtils.matcherHint)(matcherName, undefined, '', options) +
  451. '\n\n' +
  452. `Received: ${(0, _jestMatcherUtils.printReceived)(received)}`;
  453. return {
  454. message,
  455. pass
  456. };
  457. },
  458. toBeNull(received, expected) {
  459. const matcherName = 'toBeNull';
  460. const options = {
  461. isNot: this.isNot,
  462. promise: this.promise
  463. };
  464. (0, _jestMatcherUtils.ensureNoExpected)(expected, matcherName, options);
  465. const pass = received === null;
  466. const message = () =>
  467. (0, _jestMatcherUtils.matcherHint)(matcherName, undefined, '', options) +
  468. '\n\n' +
  469. `Received: ${(0, _jestMatcherUtils.printReceived)(received)}`;
  470. return {
  471. message,
  472. pass
  473. };
  474. },
  475. toBeTruthy(received, expected) {
  476. const matcherName = 'toBeTruthy';
  477. const options = {
  478. isNot: this.isNot,
  479. promise: this.promise
  480. };
  481. (0, _jestMatcherUtils.ensureNoExpected)(expected, matcherName, options);
  482. const pass = !!received;
  483. const message = () =>
  484. (0, _jestMatcherUtils.matcherHint)(matcherName, undefined, '', options) +
  485. '\n\n' +
  486. `Received: ${(0, _jestMatcherUtils.printReceived)(received)}`;
  487. return {
  488. message,
  489. pass
  490. };
  491. },
  492. toBeUndefined(received, expected) {
  493. const matcherName = 'toBeUndefined';
  494. const options = {
  495. isNot: this.isNot,
  496. promise: this.promise
  497. };
  498. (0, _jestMatcherUtils.ensureNoExpected)(expected, matcherName, options);
  499. const pass = received === void 0;
  500. const message = () =>
  501. (0, _jestMatcherUtils.matcherHint)(matcherName, undefined, '', options) +
  502. '\n\n' +
  503. `Received: ${(0, _jestMatcherUtils.printReceived)(received)}`;
  504. return {
  505. message,
  506. pass
  507. };
  508. },
  509. toContain(received, expected) {
  510. const matcherName = 'toContain';
  511. const isNot = this.isNot;
  512. const options = {
  513. comment: 'indexOf',
  514. isNot,
  515. promise: this.promise
  516. };
  517. if (received == null) {
  518. throw new Error(
  519. (0, _jestMatcherUtils.matcherErrorMessage)(
  520. (0, _jestMatcherUtils.matcherHint)(
  521. matcherName,
  522. undefined,
  523. undefined,
  524. options
  525. ),
  526. `${(0, _jestMatcherUtils.RECEIVED_COLOR)(
  527. 'received'
  528. )} value must not be null nor undefined`,
  529. (0, _jestMatcherUtils.printWithType)(
  530. 'Received',
  531. received,
  532. _jestMatcherUtils.printReceived
  533. )
  534. )
  535. );
  536. }
  537. if (typeof received === 'string') {
  538. const index = received.indexOf(String(expected));
  539. const pass = index !== -1;
  540. const message = () => {
  541. const labelExpected = `Expected ${
  542. typeof expected === 'string' ? 'substring' : 'value'
  543. }`;
  544. const labelReceived = 'Received string';
  545. const printLabel = (0, _jestMatcherUtils.getLabelPrinter)(
  546. labelExpected,
  547. labelReceived
  548. );
  549. return (
  550. (0, _jestMatcherUtils.matcherHint)(
  551. matcherName,
  552. undefined,
  553. undefined,
  554. options
  555. ) +
  556. '\n\n' +
  557. `${printLabel(labelExpected)}${isNot ? 'not ' : ''}${(0,
  558. _jestMatcherUtils.printExpected)(expected)}\n` +
  559. `${printLabel(labelReceived)}${isNot ? ' ' : ''}${
  560. isNot
  561. ? (0, _print.printReceivedStringContainExpectedSubstring)(
  562. received,
  563. index,
  564. String(expected).length
  565. )
  566. : (0, _jestMatcherUtils.printReceived)(received)
  567. }`
  568. );
  569. };
  570. return {
  571. message,
  572. pass
  573. };
  574. }
  575. const indexable = Array.from(received);
  576. const index = indexable.indexOf(expected);
  577. const pass = index !== -1;
  578. const message = () => {
  579. const labelExpected = 'Expected value';
  580. const labelReceived = `Received ${(0, _jestGetType.default)(received)}`;
  581. const printLabel = (0, _jestMatcherUtils.getLabelPrinter)(
  582. labelExpected,
  583. labelReceived
  584. );
  585. return (
  586. (0, _jestMatcherUtils.matcherHint)(
  587. matcherName,
  588. undefined,
  589. undefined,
  590. options
  591. ) +
  592. '\n\n' +
  593. `${printLabel(labelExpected)}${isNot ? 'not ' : ''}${(0,
  594. _jestMatcherUtils.printExpected)(expected)}\n` +
  595. `${printLabel(labelReceived)}${isNot ? ' ' : ''}${
  596. isNot && Array.isArray(received)
  597. ? (0, _print.printReceivedArrayContainExpectedItem)(received, index)
  598. : (0, _jestMatcherUtils.printReceived)(received)
  599. }` +
  600. (!isNot &&
  601. indexable.findIndex(item =>
  602. (0, _jasmineUtils.equals)(item, expected, [_utils.iterableEquality])
  603. ) !== -1
  604. ? `\n\n${_jestMatcherUtils.SUGGEST_TO_CONTAIN_EQUAL}`
  605. : '')
  606. );
  607. };
  608. return {
  609. message,
  610. pass
  611. };
  612. },
  613. toContainEqual(received, expected) {
  614. const matcherName = 'toContainEqual';
  615. const isNot = this.isNot;
  616. const options = {
  617. comment: 'deep equality',
  618. isNot,
  619. promise: this.promise
  620. };
  621. if (received == null) {
  622. throw new Error(
  623. (0, _jestMatcherUtils.matcherErrorMessage)(
  624. (0, _jestMatcherUtils.matcherHint)(
  625. matcherName,
  626. undefined,
  627. undefined,
  628. options
  629. ),
  630. `${(0, _jestMatcherUtils.RECEIVED_COLOR)(
  631. 'received'
  632. )} value must not be null nor undefined`,
  633. (0, _jestMatcherUtils.printWithType)(
  634. 'Received',
  635. received,
  636. _jestMatcherUtils.printReceived
  637. )
  638. )
  639. );
  640. }
  641. const index = Array.from(received).findIndex(item =>
  642. (0, _jasmineUtils.equals)(item, expected, [_utils.iterableEquality])
  643. );
  644. const pass = index !== -1;
  645. const message = () => {
  646. const labelExpected = 'Expected value';
  647. const labelReceived = `Received ${(0, _jestGetType.default)(received)}`;
  648. const printLabel = (0, _jestMatcherUtils.getLabelPrinter)(
  649. labelExpected,
  650. labelReceived
  651. );
  652. return (
  653. (0, _jestMatcherUtils.matcherHint)(
  654. matcherName,
  655. undefined,
  656. undefined,
  657. options
  658. ) +
  659. '\n\n' +
  660. `${printLabel(labelExpected)}${isNot ? 'not ' : ''}${(0,
  661. _jestMatcherUtils.printExpected)(expected)}\n` +
  662. `${printLabel(labelReceived)}${isNot ? ' ' : ''}${
  663. isNot && Array.isArray(received)
  664. ? (0, _print.printReceivedArrayContainExpectedItem)(received, index)
  665. : (0, _jestMatcherUtils.printReceived)(received)
  666. }`
  667. );
  668. };
  669. return {
  670. message,
  671. pass
  672. };
  673. },
  674. toEqual(received, expected) {
  675. const matcherName = 'toEqual';
  676. const options = {
  677. comment: 'deep equality',
  678. isNot: this.isNot,
  679. promise: this.promise
  680. };
  681. const pass = (0, _jasmineUtils.equals)(received, expected, [
  682. _utils.iterableEquality
  683. ]);
  684. const message = pass
  685. ? () =>
  686. (0, _jestMatcherUtils.matcherHint)(
  687. matcherName,
  688. undefined,
  689. undefined,
  690. options
  691. ) +
  692. '\n\n' +
  693. `Expected: not ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
  694. ((0, _jestMatcherUtils.stringify)(expected) !==
  695. (0, _jestMatcherUtils.stringify)(received)
  696. ? `Received: ${(0, _jestMatcherUtils.printReceived)(received)}`
  697. : '')
  698. : () =>
  699. (0, _jestMatcherUtils.matcherHint)(
  700. matcherName,
  701. undefined,
  702. undefined,
  703. options
  704. ) +
  705. '\n\n' +
  706. (0, _jestMatcherUtils.printDiffOrStringify)(
  707. expected,
  708. received,
  709. EXPECTED_LABEL,
  710. RECEIVED_LABEL,
  711. isExpand(this.expand)
  712. ); // Passing the actual and expected objects so that a custom reporter
  713. // could access them, for example in order to display a custom visual diff,
  714. // or create a different error message
  715. return {
  716. actual: received,
  717. expected,
  718. message,
  719. name: matcherName,
  720. pass
  721. };
  722. },
  723. toHaveLength(received, expected) {
  724. const matcherName = 'toHaveLength';
  725. const isNot = this.isNot;
  726. const options = {
  727. isNot,
  728. promise: this.promise
  729. };
  730. if (
  731. typeof received !== 'string' &&
  732. (!received || typeof received.length !== 'number')
  733. ) {
  734. throw new Error(
  735. (0, _jestMatcherUtils.matcherErrorMessage)(
  736. (0, _jestMatcherUtils.matcherHint)(
  737. matcherName,
  738. undefined,
  739. undefined,
  740. options
  741. ),
  742. `${(0, _jestMatcherUtils.RECEIVED_COLOR)(
  743. 'received'
  744. )} value must have a length property whose value must be a number`,
  745. (0, _jestMatcherUtils.printWithType)(
  746. 'Received',
  747. received,
  748. _jestMatcherUtils.printReceived
  749. )
  750. )
  751. );
  752. }
  753. (0, _jestMatcherUtils.ensureExpectedIsNonNegativeInteger)(
  754. expected,
  755. matcherName,
  756. options
  757. );
  758. const pass = received.length === expected;
  759. const message = () => {
  760. const labelExpected = 'Expected length';
  761. const labelReceivedLength = 'Received length';
  762. const labelReceivedValue = `Received ${(0, _jestGetType.default)(
  763. received
  764. )}`;
  765. const printLabel = (0, _jestMatcherUtils.getLabelPrinter)(
  766. labelExpected,
  767. labelReceivedLength,
  768. labelReceivedValue
  769. );
  770. return (
  771. (0, _jestMatcherUtils.matcherHint)(
  772. matcherName,
  773. undefined,
  774. undefined,
  775. options
  776. ) +
  777. '\n\n' +
  778. `${printLabel(labelExpected)}${isNot ? 'not ' : ''}${(0,
  779. _jestMatcherUtils.printExpected)(expected)}\n` +
  780. (isNot
  781. ? ''
  782. : `${printLabel(labelReceivedLength)}${(0,
  783. _jestMatcherUtils.printReceived)(received.length)}\n`) +
  784. `${printLabel(labelReceivedValue)}${isNot ? ' ' : ''}${(0,
  785. _jestMatcherUtils.printReceived)(received)}`
  786. );
  787. };
  788. return {
  789. message,
  790. pass
  791. };
  792. },
  793. toHaveProperty(received, expectedPath, expectedValue) {
  794. const matcherName = 'toHaveProperty';
  795. const expectedArgument = 'path';
  796. const hasValue = arguments.length === 3;
  797. const options = {
  798. isNot: this.isNot,
  799. promise: this.promise,
  800. secondArgument: hasValue ? 'value' : ''
  801. };
  802. if (received === null || received === undefined) {
  803. throw new Error(
  804. (0, _jestMatcherUtils.matcherErrorMessage)(
  805. (0, _jestMatcherUtils.matcherHint)(
  806. matcherName,
  807. undefined,
  808. expectedArgument,
  809. options
  810. ),
  811. `${(0, _jestMatcherUtils.RECEIVED_COLOR)(
  812. 'received'
  813. )} value must not be null nor undefined`,
  814. (0, _jestMatcherUtils.printWithType)(
  815. 'Received',
  816. received,
  817. _jestMatcherUtils.printReceived
  818. )
  819. )
  820. );
  821. }
  822. const expectedPathType = (0, _jestGetType.default)(expectedPath);
  823. if (expectedPathType !== 'string' && expectedPathType !== 'array') {
  824. throw new Error(
  825. (0, _jestMatcherUtils.matcherErrorMessage)(
  826. (0, _jestMatcherUtils.matcherHint)(
  827. matcherName,
  828. undefined,
  829. expectedArgument,
  830. options
  831. ),
  832. `${(0, _jestMatcherUtils.EXPECTED_COLOR)(
  833. 'expected'
  834. )} path must be a string or array`,
  835. (0, _jestMatcherUtils.printWithType)(
  836. 'Expected',
  837. expectedPath,
  838. _jestMatcherUtils.printExpected
  839. )
  840. )
  841. );
  842. }
  843. const expectedPathLength =
  844. typeof expectedPath === 'string'
  845. ? expectedPath.split('.').length
  846. : expectedPath.length;
  847. if (expectedPathType === 'array' && expectedPathLength === 0) {
  848. throw new Error(
  849. (0, _jestMatcherUtils.matcherErrorMessage)(
  850. (0, _jestMatcherUtils.matcherHint)(
  851. matcherName,
  852. undefined,
  853. expectedArgument,
  854. options
  855. ),
  856. `${(0, _jestMatcherUtils.EXPECTED_COLOR)(
  857. 'expected'
  858. )} path must not be an empty array`,
  859. (0, _jestMatcherUtils.printWithType)(
  860. 'Expected',
  861. expectedPath,
  862. _jestMatcherUtils.printExpected
  863. )
  864. )
  865. );
  866. }
  867. const result = (0, _utils.getPath)(received, expectedPath);
  868. const lastTraversedObject = result.lastTraversedObject,
  869. hasEndProp = result.hasEndProp;
  870. const receivedPath = result.traversedPath;
  871. const hasCompletePath = receivedPath.length === expectedPathLength;
  872. const receivedValue = hasCompletePath ? result.value : lastTraversedObject;
  873. const pass = hasValue
  874. ? (0, _jasmineUtils.equals)(result.value, expectedValue, [
  875. _utils.iterableEquality
  876. ])
  877. : Boolean(hasEndProp); // theoretically undefined if empty path
  878. // Remove type cast if we rewrite getPath as iterative algorithm.
  879. // Delete this unique report if future breaking change
  880. // removes the edge case that expected value undefined
  881. // also matches absence of a property with the key path.
  882. if (pass && !hasCompletePath) {
  883. const message = () =>
  884. (0, _jestMatcherUtils.matcherHint)(
  885. matcherName,
  886. undefined,
  887. expectedArgument,
  888. options
  889. ) +
  890. '\n\n' +
  891. `Expected path: ${(0, _jestMatcherUtils.printExpected)(
  892. expectedPath
  893. )}\n` +
  894. `Received path: ${(0, _jestMatcherUtils.printReceived)(
  895. expectedPathType === 'array' || receivedPath.length === 0
  896. ? receivedPath
  897. : receivedPath.join('.')
  898. )}\n\n` +
  899. `Expected value: not ${(0, _jestMatcherUtils.printExpected)(
  900. expectedValue
  901. )}\n` +
  902. `Received value: ${(0, _jestMatcherUtils.printReceived)(
  903. receivedValue
  904. )}\n\n` +
  905. (0, _jestMatcherUtils.DIM_COLOR)(
  906. 'Because a positive assertion passes for expected value undefined if the property does not exist, this negative assertion fails unless the property does exist and has a defined value'
  907. );
  908. return {
  909. message,
  910. pass
  911. };
  912. }
  913. const message = pass
  914. ? () =>
  915. (0, _jestMatcherUtils.matcherHint)(
  916. matcherName,
  917. undefined,
  918. expectedArgument,
  919. options
  920. ) +
  921. '\n\n' +
  922. (hasValue
  923. ? `Expected path: ${(0, _jestMatcherUtils.printExpected)(
  924. expectedPath
  925. )}\n\n` +
  926. `Expected value: not ${(0, _jestMatcherUtils.printExpected)(
  927. expectedValue
  928. )}` +
  929. ((0, _jestMatcherUtils.stringify)(expectedValue) !==
  930. (0, _jestMatcherUtils.stringify)(receivedValue)
  931. ? `\nReceived value: ${(0, _jestMatcherUtils.printReceived)(
  932. receivedValue
  933. )}`
  934. : '')
  935. : `Expected path: not ${(0, _jestMatcherUtils.printExpected)(
  936. expectedPath
  937. )}\n\n` +
  938. `Received value: ${(0, _jestMatcherUtils.printReceived)(
  939. receivedValue
  940. )}`)
  941. : () =>
  942. (0, _jestMatcherUtils.matcherHint)(
  943. matcherName,
  944. undefined,
  945. expectedArgument,
  946. options
  947. ) +
  948. '\n\n' +
  949. `Expected path: ${(0, _jestMatcherUtils.printExpected)(
  950. expectedPath
  951. )}\n` +
  952. (hasCompletePath
  953. ? '\n' +
  954. (0, _jestMatcherUtils.printDiffOrStringify)(
  955. expectedValue,
  956. receivedValue,
  957. EXPECTED_VALUE_LABEL,
  958. RECEIVED_VALUE_LABEL,
  959. isExpand(this.expand)
  960. )
  961. : `Received path: ${(0, _jestMatcherUtils.printReceived)(
  962. expectedPathType === 'array' || receivedPath.length === 0
  963. ? receivedPath
  964. : receivedPath.join('.')
  965. )}\n\n` +
  966. (hasValue
  967. ? `Expected value: ${(0, _jestMatcherUtils.printExpected)(
  968. expectedValue
  969. )}\n`
  970. : '') +
  971. `Received value: ${(0, _jestMatcherUtils.printReceived)(
  972. receivedValue
  973. )}`);
  974. return {
  975. message,
  976. pass
  977. };
  978. },
  979. toMatch(received, expected) {
  980. const matcherName = 'toMatch';
  981. const options = {
  982. isNot: this.isNot,
  983. promise: this.promise
  984. };
  985. if (typeof received !== 'string') {
  986. throw new Error(
  987. (0, _jestMatcherUtils.matcherErrorMessage)(
  988. (0, _jestMatcherUtils.matcherHint)(
  989. matcherName,
  990. undefined,
  991. undefined,
  992. options
  993. ),
  994. `${(0, _jestMatcherUtils.RECEIVED_COLOR)(
  995. 'received'
  996. )} value must be a string`,
  997. (0, _jestMatcherUtils.printWithType)(
  998. 'Received',
  999. received,
  1000. _jestMatcherUtils.printReceived
  1001. )
  1002. )
  1003. );
  1004. }
  1005. if (
  1006. !(typeof expected === 'string') &&
  1007. !(expected && typeof expected.test === 'function')
  1008. ) {
  1009. throw new Error(
  1010. (0, _jestMatcherUtils.matcherErrorMessage)(
  1011. (0, _jestMatcherUtils.matcherHint)(
  1012. matcherName,
  1013. undefined,
  1014. undefined,
  1015. options
  1016. ),
  1017. `${(0, _jestMatcherUtils.EXPECTED_COLOR)(
  1018. 'expected'
  1019. )} value must be a string or regular expression`,
  1020. (0, _jestMatcherUtils.printWithType)(
  1021. 'Expected',
  1022. expected,
  1023. _jestMatcherUtils.printExpected
  1024. )
  1025. )
  1026. );
  1027. }
  1028. const pass =
  1029. typeof expected === 'string'
  1030. ? received.includes(expected)
  1031. : expected.test(received);
  1032. const message = pass
  1033. ? () =>
  1034. typeof expected === 'string'
  1035. ? (0, _jestMatcherUtils.matcherHint)(
  1036. matcherName,
  1037. undefined,
  1038. undefined,
  1039. options
  1040. ) +
  1041. '\n\n' +
  1042. `Expected substring: not ${(0, _jestMatcherUtils.printExpected)(
  1043. expected
  1044. )}\n` +
  1045. `Received string: ${(0,
  1046. _print.printReceivedStringContainExpectedSubstring)(
  1047. received,
  1048. received.indexOf(expected),
  1049. expected.length
  1050. )}`
  1051. : (0, _jestMatcherUtils.matcherHint)(
  1052. matcherName,
  1053. undefined,
  1054. undefined,
  1055. options
  1056. ) +
  1057. '\n\n' +
  1058. `Expected pattern: not ${(0, _jestMatcherUtils.printExpected)(
  1059. expected
  1060. )}\n` +
  1061. `Received string: ${(0,
  1062. _print.printReceivedStringContainExpectedResult)(
  1063. received,
  1064. typeof expected.exec === 'function'
  1065. ? expected.exec(received)
  1066. : null
  1067. )}`
  1068. : () => {
  1069. const labelExpected = `Expected ${
  1070. typeof expected === 'string' ? 'substring' : 'pattern'
  1071. }`;
  1072. const labelReceived = 'Received string';
  1073. const printLabel = (0, _jestMatcherUtils.getLabelPrinter)(
  1074. labelExpected,
  1075. labelReceived
  1076. );
  1077. return (
  1078. (0, _jestMatcherUtils.matcherHint)(
  1079. matcherName,
  1080. undefined,
  1081. undefined,
  1082. options
  1083. ) +
  1084. '\n\n' +
  1085. `${printLabel(labelExpected)}${(0, _jestMatcherUtils.printExpected)(
  1086. expected
  1087. )}\n` +
  1088. `${printLabel(labelReceived)}${(0, _jestMatcherUtils.printReceived)(
  1089. received
  1090. )}`
  1091. );
  1092. };
  1093. return {
  1094. message,
  1095. pass
  1096. };
  1097. },
  1098. toMatchObject(received, expected) {
  1099. const matcherName = 'toMatchObject';
  1100. const options = {
  1101. isNot: this.isNot,
  1102. promise: this.promise
  1103. };
  1104. if (typeof received !== 'object' || received === null) {
  1105. throw new Error(
  1106. (0, _jestMatcherUtils.matcherErrorMessage)(
  1107. (0, _jestMatcherUtils.matcherHint)(
  1108. matcherName,
  1109. undefined,
  1110. undefined,
  1111. options
  1112. ),
  1113. `${(0, _jestMatcherUtils.RECEIVED_COLOR)(
  1114. 'received'
  1115. )} value must be a non-null object`,
  1116. (0, _jestMatcherUtils.printWithType)(
  1117. 'Received',
  1118. received,
  1119. _jestMatcherUtils.printReceived
  1120. )
  1121. )
  1122. );
  1123. }
  1124. if (typeof expected !== 'object' || expected === null) {
  1125. throw new Error(
  1126. (0, _jestMatcherUtils.matcherErrorMessage)(
  1127. (0, _jestMatcherUtils.matcherHint)(
  1128. matcherName,
  1129. undefined,
  1130. undefined,
  1131. options
  1132. ),
  1133. `${(0, _jestMatcherUtils.EXPECTED_COLOR)(
  1134. 'expected'
  1135. )} value must be a non-null object`,
  1136. (0, _jestMatcherUtils.printWithType)(
  1137. 'Expected',
  1138. expected,
  1139. _jestMatcherUtils.printExpected
  1140. )
  1141. )
  1142. );
  1143. }
  1144. const pass = (0, _jasmineUtils.equals)(received, expected, [
  1145. _utils.iterableEquality,
  1146. _utils.subsetEquality
  1147. ]);
  1148. const message = pass
  1149. ? () =>
  1150. (0, _jestMatcherUtils.matcherHint)(
  1151. matcherName,
  1152. undefined,
  1153. undefined,
  1154. options
  1155. ) +
  1156. '\n\n' +
  1157. `Expected: not ${(0, _jestMatcherUtils.printExpected)(expected)}` +
  1158. ((0, _jestMatcherUtils.stringify)(expected) !==
  1159. (0, _jestMatcherUtils.stringify)(received)
  1160. ? `\nReceived: ${(0, _jestMatcherUtils.printReceived)(
  1161. received
  1162. )}`
  1163. : '')
  1164. : () =>
  1165. (0, _jestMatcherUtils.matcherHint)(
  1166. matcherName,
  1167. undefined,
  1168. undefined,
  1169. options
  1170. ) +
  1171. '\n\n' +
  1172. (0, _jestMatcherUtils.printDiffOrStringify)(
  1173. expected,
  1174. (0, _utils.getObjectSubset)(received, expected),
  1175. EXPECTED_LABEL,
  1176. RECEIVED_LABEL,
  1177. isExpand(this.expand)
  1178. );
  1179. return {
  1180. message,
  1181. pass
  1182. };
  1183. },
  1184. toStrictEqual(received, expected) {
  1185. const matcherName = 'toStrictEqual';
  1186. const options = {
  1187. comment: 'deep equality',
  1188. isNot: this.isNot,
  1189. promise: this.promise
  1190. };
  1191. const pass = (0, _jasmineUtils.equals)(
  1192. received,
  1193. expected,
  1194. toStrictEqualTesters,
  1195. true
  1196. );
  1197. const message = pass
  1198. ? () =>
  1199. (0, _jestMatcherUtils.matcherHint)(
  1200. matcherName,
  1201. undefined,
  1202. undefined,
  1203. options
  1204. ) +
  1205. '\n\n' +
  1206. `Expected: not ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
  1207. ((0, _jestMatcherUtils.stringify)(expected) !==
  1208. (0, _jestMatcherUtils.stringify)(received)
  1209. ? `Received: ${(0, _jestMatcherUtils.printReceived)(received)}`
  1210. : '')
  1211. : () =>
  1212. (0, _jestMatcherUtils.matcherHint)(
  1213. matcherName,
  1214. undefined,
  1215. undefined,
  1216. options
  1217. ) +
  1218. '\n\n' +
  1219. (0, _jestMatcherUtils.printDiffOrStringify)(
  1220. expected,
  1221. received,
  1222. EXPECTED_LABEL,
  1223. RECEIVED_LABEL,
  1224. isExpand(this.expand)
  1225. ); // Passing the actual and expected objects so that a custom reporter
  1226. // could access them, for example in order to display a custom visual diff,
  1227. // or create a different error message
  1228. return {
  1229. actual: received,
  1230. expected,
  1231. message,
  1232. name: matcherName,
  1233. pass
  1234. };
  1235. }
  1236. };
  1237. var _default = matchers;
  1238. exports.default = _default;