Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

matchers.js 38KB

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