matchers.js 37 KB

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