matchers.js 38 KB

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