matchers.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. var _jestDiff = require('jest-diff');
  6. var _jestDiff2 = _interopRequireDefault(_jestDiff);
  7. var _jestGetType = require('jest-get-type');
  8. var _jestGetType2 = _interopRequireDefault(_jestGetType);
  9. var _jestRegexUtil = require('jest-regex-util');
  10. var _jestMatcherUtils = require('jest-matcher-utils');
  11. var _utils = require('./utils');
  12. var _jasmine_utils = require('./jasmine_utils');
  13. function _interopRequireDefault(obj) {
  14. return obj && obj.__esModule ? obj : {default: obj};
  15. }
  16. const matchers = {
  17. toBe: function(received, expected) {
  18. const comment = 'Object.is equality';
  19. const pass = Object.is(received, expected);
  20. const message = pass
  21. ? () =>
  22. (0, _jestMatcherUtils.matcherHint)('.toBe', undefined, undefined, {
  23. comment: comment,
  24. isNot: true
  25. }) +
  26. '\n\n' +
  27. `Expected: ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
  28. `Received: ${(0, _jestMatcherUtils.printReceived)(received)}`
  29. : () => {
  30. const suggestToEqual =
  31. (0, _jestGetType2.default)(received) ===
  32. (0, _jestGetType2.default)(expected) &&
  33. ((0, _jestGetType2.default)(received) === 'object' ||
  34. (0, _jestGetType2.default)(expected) === 'array') &&
  35. (0, _jasmine_utils.equals)(received, expected, [
  36. _utils.iterableEquality
  37. ]);
  38. const oneline = (0, _utils.isOneline)(expected, received);
  39. const diffString = (0, _jestDiff2.default)(expected, received, {
  40. expand: this.expand
  41. });
  42. return (
  43. (0, _jestMatcherUtils.matcherHint)('.toBe', undefined, undefined, {
  44. comment: comment,
  45. isNot: false
  46. }) +
  47. '\n\n' +
  48. `Expected: ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
  49. `Received: ${(0, _jestMatcherUtils.printReceived)(received)}` +
  50. (diffString && !oneline ? `\n\nDifference:\n\n${diffString}` : '') +
  51. (suggestToEqual ? ` ${_jestMatcherUtils.SUGGEST_TO_EQUAL}` : '')
  52. );
  53. };
  54. // Passing the the actual and expected objects so that a custom reporter
  55. // could access them, for example in order to display a custom visual diff,
  56. // or create a different error message
  57. return {
  58. actual: received,
  59. expected: expected,
  60. message: message,
  61. name: 'toBe',
  62. pass: pass
  63. };
  64. },
  65. toBeCloseTo: function(actual, expected) {
  66. let precision =
  67. arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 2;
  68. const secondArgument = arguments.length === 3 ? 'precision' : null;
  69. (0, _jestMatcherUtils.ensureNumbers)(actual, expected, '.toBeCloseTo');
  70. const pass = Math.abs(expected - actual) < Math.pow(10, -precision) / 2;
  71. const message = () =>
  72. (0, _jestMatcherUtils.matcherHint)('.toBeCloseTo', undefined, undefined, {
  73. isNot: this.isNot,
  74. secondArgument: secondArgument
  75. }) +
  76. '\n\n' +
  77. `Precision: ${(0, _jestMatcherUtils.printExpected)(precision)}-digit\n` +
  78. `Expected: ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
  79. `Received: ${(0, _jestMatcherUtils.printReceived)(actual)}`;
  80. return {message: message, pass: pass};
  81. },
  82. toBeDefined: function(actual, expected) {
  83. (0, _jestMatcherUtils.ensureNoExpected)(expected, '.toBeDefined');
  84. const pass = actual !== void 0;
  85. const message = () =>
  86. (0, _jestMatcherUtils.matcherHint)('.toBeDefined', 'received', '', {
  87. isNot: this.isNot
  88. }) +
  89. '\n\n' +
  90. `Received: ${(0, _jestMatcherUtils.printReceived)(actual)}`;
  91. return {message: message, pass: pass};
  92. },
  93. toBeFalsy: function(actual, expected) {
  94. (0, _jestMatcherUtils.ensureNoExpected)(expected, '.toBeFalsy');
  95. const pass = !actual;
  96. const message = () =>
  97. (0, _jestMatcherUtils.matcherHint)('.toBeFalsy', 'received', '', {
  98. isNot: this.isNot
  99. }) +
  100. '\n\n' +
  101. `Received: ${(0, _jestMatcherUtils.printReceived)(actual)}`;
  102. return {message: message, pass: pass};
  103. },
  104. toBeGreaterThan: function(actual, expected) {
  105. (0, _jestMatcherUtils.ensureNumbers)(actual, expected, '.toBeGreaterThan');
  106. const pass = actual > expected;
  107. const message = () =>
  108. (0, _jestMatcherUtils.matcherHint)(
  109. '.toBeGreaterThan',
  110. undefined,
  111. undefined,
  112. {
  113. isNot: this.isNot
  114. }
  115. ) +
  116. '\n\n' +
  117. `Expected: ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
  118. `Received: ${(0, _jestMatcherUtils.printReceived)(actual)}`;
  119. return {message: message, pass: pass};
  120. },
  121. toBeGreaterThanOrEqual: function(actual, expected) {
  122. (0, _jestMatcherUtils.ensureNumbers)(
  123. actual,
  124. expected,
  125. '.toBeGreaterThanOrEqual'
  126. );
  127. const pass = actual >= expected;
  128. const message = () =>
  129. (0, _jestMatcherUtils.matcherHint)(
  130. '.toBeGreaterThanOrEqual',
  131. undefined,
  132. undefined,
  133. {
  134. isNot: this.isNot
  135. }
  136. ) +
  137. '\n\n' +
  138. `Expected: ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
  139. `Received: ${(0, _jestMatcherUtils.printReceived)(actual)}`;
  140. return {message: message, pass: pass};
  141. },
  142. toBeInstanceOf: function(received, constructor) {
  143. const constType = (0, _jestGetType2.default)(constructor);
  144. if (constType !== 'function') {
  145. throw new Error(
  146. (0, _jestMatcherUtils.matcherHint)(
  147. '.toBeInstanceOf',
  148. 'value',
  149. 'constructor',
  150. {
  151. isNot: this.isNot
  152. }
  153. ) +
  154. `\n\n` +
  155. `Expected constructor to be a function. Instead got:\n` +
  156. ` ${(0, _jestMatcherUtils.printExpected)(constType)}`
  157. );
  158. }
  159. const pass = received instanceof constructor;
  160. const message = pass
  161. ? () =>
  162. (0, _jestMatcherUtils.matcherHint)(
  163. '.toBeInstanceOf',
  164. 'value',
  165. 'constructor',
  166. {
  167. isNot: this.isNot
  168. }
  169. ) +
  170. '\n\n' +
  171. `Expected constructor: ${(0, _jestMatcherUtils.EXPECTED_COLOR)(
  172. constructor.name || String(constructor)
  173. )}\n` +
  174. `Received value: ${(0, _jestMatcherUtils.printReceived)(received)}`
  175. : () =>
  176. (0, _jestMatcherUtils.matcherHint)(
  177. '.toBeInstanceOf',
  178. 'value',
  179. 'constructor',
  180. {
  181. isNot: this.isNot
  182. }
  183. ) +
  184. '\n\n' +
  185. `Expected constructor: ${(0, _jestMatcherUtils.EXPECTED_COLOR)(
  186. constructor.name || String(constructor)
  187. )}\n` +
  188. `Received constructor: ${(0, _jestMatcherUtils.RECEIVED_COLOR)(
  189. received != null
  190. ? received.constructor && received.constructor.name
  191. : ''
  192. )}\n` +
  193. `Received value: ${(0, _jestMatcherUtils.printReceived)(received)}`;
  194. return {message: message, pass: pass};
  195. },
  196. toBeLessThan: function(actual, expected) {
  197. (0, _jestMatcherUtils.ensureNumbers)(actual, expected, '.toBeLessThan');
  198. const pass = actual < expected;
  199. const message = () =>
  200. (0, _jestMatcherUtils.matcherHint)(
  201. '.toBeLessThan',
  202. undefined,
  203. undefined,
  204. {
  205. isNot: this.isNot
  206. }
  207. ) +
  208. '\n\n' +
  209. `Expected: ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
  210. `Received: ${(0, _jestMatcherUtils.printReceived)(actual)}`;
  211. return {message: message, pass: pass};
  212. },
  213. toBeLessThanOrEqual: function(actual, expected) {
  214. (0, _jestMatcherUtils.ensureNumbers)(
  215. actual,
  216. expected,
  217. '.toBeLessThanOrEqual'
  218. );
  219. const pass = actual <= expected;
  220. const message = () =>
  221. (0, _jestMatcherUtils.matcherHint)(
  222. '.toBeLessThanOrEqual',
  223. undefined,
  224. undefined,
  225. {
  226. isNot: this.isNot
  227. }
  228. ) +
  229. '\n\n' +
  230. `Expected: ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
  231. `Received: ${(0, _jestMatcherUtils.printReceived)(actual)}`;
  232. return {message: message, pass: pass};
  233. },
  234. toBeNaN: function(actual, expected) {
  235. (0, _jestMatcherUtils.ensureNoExpected)(expected, '.toBeNaN');
  236. const pass = Number.isNaN(actual);
  237. const message = () =>
  238. (0, _jestMatcherUtils.matcherHint)('.toBeNaN', 'received', '', {
  239. isNot: this.isNot
  240. }) +
  241. '\n\n' +
  242. `Received: ${(0, _jestMatcherUtils.printReceived)(actual)}`;
  243. return {message: message, pass: pass};
  244. },
  245. toBeNull: function(actual, expected) {
  246. (0, _jestMatcherUtils.ensureNoExpected)(expected, '.toBeNull');
  247. const pass = actual === null;
  248. const message = () =>
  249. (0, _jestMatcherUtils.matcherHint)('.toBeNull', 'received', '', {
  250. isNot: this.isNot
  251. }) +
  252. '\n\n' +
  253. `Received: ${(0, _jestMatcherUtils.printReceived)(actual)}`;
  254. return {message: message, pass: pass};
  255. },
  256. toBeTruthy: function(actual, expected) {
  257. (0, _jestMatcherUtils.ensureNoExpected)(expected, '.toBeTruthy');
  258. const pass = !!actual;
  259. const message = () =>
  260. (0, _jestMatcherUtils.matcherHint)('.toBeTruthy', 'received', '', {
  261. isNot: this.isNot
  262. }) +
  263. '\n\n' +
  264. `Received: ${(0, _jestMatcherUtils.printReceived)(actual)}`;
  265. return {message: message, pass: pass};
  266. },
  267. toBeUndefined: function(actual, expected) {
  268. (0, _jestMatcherUtils.ensureNoExpected)(expected, '.toBeUndefined');
  269. const pass = actual === void 0;
  270. const message = () =>
  271. (0, _jestMatcherUtils.matcherHint)('.toBeUndefined', 'received', '', {
  272. isNot: this.isNot
  273. }) +
  274. '\n\n' +
  275. `Received: ${(0, _jestMatcherUtils.printReceived)(actual)}`;
  276. return {message: message, pass: pass};
  277. },
  278. toContain: function(collection, value) {
  279. const collectionType = (0, _jestGetType2.default)(collection);
  280. let converted = null;
  281. if (Array.isArray(collection) || typeof collection === 'string') {
  282. // strings have `indexOf` so we don't need to convert
  283. // arrays have `indexOf` and we don't want to make a copy
  284. converted = collection;
  285. } else {
  286. try {
  287. converted = Array.from(collection);
  288. } catch (e) {
  289. throw new Error(
  290. (0, _jestMatcherUtils.matcherHint)(
  291. '[.not].toContainEqual',
  292. 'collection',
  293. 'value'
  294. ) +
  295. '\n\n' +
  296. `Expected ${(0, _jestMatcherUtils.RECEIVED_COLOR)(
  297. 'collection'
  298. )} to be an array-like structure.\n` +
  299. (0, _jestMatcherUtils.printWithType)(
  300. 'Received',
  301. collection,
  302. _jestMatcherUtils.printReceived
  303. )
  304. );
  305. }
  306. }
  307. // At this point, we're either a string or an Array,
  308. // which was converted from an array-like structure.
  309. const pass = converted.indexOf(value) != -1;
  310. const message = pass
  311. ? () =>
  312. (0, _jestMatcherUtils.matcherHint)(
  313. '.not.toContain',
  314. collectionType,
  315. 'value'
  316. ) +
  317. '\n\n' +
  318. `Expected ${collectionType}:\n` +
  319. ` ${(0, _jestMatcherUtils.printReceived)(collection)}\n` +
  320. `Not to contain value:\n` +
  321. ` ${(0, _jestMatcherUtils.printExpected)(value)}\n`
  322. : () => {
  323. const suggestToContainEqual =
  324. converted !== null &&
  325. typeof converted !== 'string' &&
  326. converted instanceof Array &&
  327. converted.findIndex(item =>
  328. (0, _jasmine_utils.equals)(item, value, [_utils.iterableEquality])
  329. ) !== -1;
  330. return (
  331. (0, _jestMatcherUtils.matcherHint)(
  332. '.toContain',
  333. collectionType,
  334. 'value'
  335. ) +
  336. '\n\n' +
  337. `Expected ${collectionType}:\n` +
  338. ` ${(0, _jestMatcherUtils.printReceived)(collection)}\n` +
  339. `To contain value:\n` +
  340. ` ${(0, _jestMatcherUtils.printExpected)(value)}` +
  341. (suggestToContainEqual
  342. ? `\n\n${_jestMatcherUtils.SUGGEST_TO_CONTAIN_EQUAL}`
  343. : '')
  344. );
  345. };
  346. return {message: message, pass: pass};
  347. },
  348. toContainEqual: function(collection, value) {
  349. const collectionType = (0, _jestGetType2.default)(collection);
  350. let converted = null;
  351. if (Array.isArray(collection)) {
  352. converted = collection;
  353. } else {
  354. try {
  355. converted = Array.from(collection);
  356. } catch (e) {
  357. throw new Error(
  358. (0, _jestMatcherUtils.matcherHint)(
  359. '[.not].toContainEqual',
  360. 'collection',
  361. 'value'
  362. ) +
  363. '\n\n' +
  364. `Expected ${(0, _jestMatcherUtils.RECEIVED_COLOR)(
  365. 'collection'
  366. )} to be an array-like structure.\n` +
  367. (0, _jestMatcherUtils.printWithType)(
  368. 'Received',
  369. collection,
  370. _jestMatcherUtils.printReceived
  371. )
  372. );
  373. }
  374. }
  375. const pass =
  376. converted.findIndex(item =>
  377. (0, _jasmine_utils.equals)(item, value, [_utils.iterableEquality])
  378. ) !== -1;
  379. const message = pass
  380. ? () =>
  381. (0, _jestMatcherUtils.matcherHint)(
  382. '.not.toContainEqual',
  383. collectionType,
  384. 'value'
  385. ) +
  386. '\n\n' +
  387. `Expected ${collectionType}:\n` +
  388. ` ${(0, _jestMatcherUtils.printReceived)(collection)}\n` +
  389. `Not to contain a value equal to:\n` +
  390. ` ${(0, _jestMatcherUtils.printExpected)(value)}\n`
  391. : () =>
  392. (0, _jestMatcherUtils.matcherHint)(
  393. '.toContainEqual',
  394. collectionType,
  395. 'value'
  396. ) +
  397. '\n\n' +
  398. `Expected ${collectionType}:\n` +
  399. ` ${(0, _jestMatcherUtils.printReceived)(collection)}\n` +
  400. `To contain a value equal to:\n` +
  401. ` ${(0, _jestMatcherUtils.printExpected)(value)}`;
  402. return {message: message, pass: pass};
  403. },
  404. toEqual: function(received, expected) {
  405. const pass = (0, _jasmine_utils.equals)(received, expected, [
  406. _utils.iterableEquality
  407. ]);
  408. const message = pass
  409. ? () =>
  410. (0, _jestMatcherUtils.matcherHint)('.not.toEqual') +
  411. '\n\n' +
  412. `Expected value to not equal:\n` +
  413. ` ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
  414. `Received:\n` +
  415. ` ${(0, _jestMatcherUtils.printReceived)(received)}`
  416. : () => {
  417. const oneline = (0, _utils.isOneline)(expected, received);
  418. const diffString = (0, _jestDiff2.default)(expected, received, {
  419. expand: this.expand
  420. });
  421. return (
  422. (0, _jestMatcherUtils.matcherHint)('.toEqual') +
  423. '\n\n' +
  424. `Expected value to equal:\n` +
  425. ` ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
  426. `Received:\n` +
  427. ` ${(0, _jestMatcherUtils.printReceived)(received)}` +
  428. (diffString && !oneline ? `\n\nDifference:\n\n${diffString}` : '')
  429. );
  430. };
  431. // Passing the the actual and expected objects so that a custom reporter
  432. // could access them, for example in order to display a custom visual diff,
  433. // or create a different error message
  434. return {
  435. actual: received,
  436. expected: expected,
  437. message: message,
  438. name: 'toEqual',
  439. pass: pass
  440. };
  441. },
  442. toHaveLength: function(received, length) {
  443. if (
  444. typeof received !== 'string' &&
  445. (!received || typeof received.length !== 'number')
  446. ) {
  447. throw new Error(
  448. (0, _jestMatcherUtils.matcherHint)(
  449. '[.not].toHaveLength',
  450. 'received',
  451. 'length'
  452. ) +
  453. '\n\n' +
  454. `Expected value to have a 'length' property that is a number. ` +
  455. `Received:\n` +
  456. ` ${(0, _jestMatcherUtils.printReceived)(received)}\n` +
  457. (received
  458. ? `received.length:\n ${(0, _jestMatcherUtils.printReceived)(
  459. received.length
  460. )}`
  461. : '')
  462. );
  463. }
  464. const pass = received.length === length;
  465. const message = pass
  466. ? () =>
  467. (0, _jestMatcherUtils.matcherHint)(
  468. '.not.toHaveLength',
  469. 'received',
  470. 'length'
  471. ) +
  472. '\n\n' +
  473. `Expected value to not have length:\n` +
  474. ` ${(0, _jestMatcherUtils.printExpected)(length)}\n` +
  475. `Received:\n` +
  476. ` ${(0, _jestMatcherUtils.printReceived)(received)}\n` +
  477. `received.length:\n` +
  478. ` ${(0, _jestMatcherUtils.printReceived)(received.length)}`
  479. : () =>
  480. (0, _jestMatcherUtils.matcherHint)(
  481. '.toHaveLength',
  482. 'received',
  483. 'length'
  484. ) +
  485. '\n\n' +
  486. `Expected value to have length:\n` +
  487. ` ${(0, _jestMatcherUtils.printExpected)(length)}\n` +
  488. `Received:\n` +
  489. ` ${(0, _jestMatcherUtils.printReceived)(received)}\n` +
  490. `received.length:\n` +
  491. ` ${(0, _jestMatcherUtils.printReceived)(received.length)}`;
  492. return {message: message, pass: pass};
  493. },
  494. toHaveProperty: function(object, keyPath, value) {
  495. const valuePassed = arguments.length === 3;
  496. const secondArgument = valuePassed ? 'value' : null;
  497. if (!object && typeof object !== 'string' && typeof object !== 'number') {
  498. throw new Error(
  499. (0, _jestMatcherUtils.matcherHint)(
  500. '[.not].toHaveProperty',
  501. 'object',
  502. 'path',
  503. {
  504. secondArgument: secondArgument
  505. }
  506. ) +
  507. '\n\n' +
  508. `Expected ${(0, _jestMatcherUtils.RECEIVED_COLOR)(
  509. 'object'
  510. )} to be an object. Received:\n` +
  511. ` ${(0, _jestGetType2.default)(object)}: ${(0,
  512. _jestMatcherUtils.printReceived)(object)}`
  513. );
  514. }
  515. const keyPathType = (0, _jestGetType2.default)(keyPath);
  516. if (keyPathType !== 'string' && keyPathType !== 'array') {
  517. throw new Error(
  518. (0, _jestMatcherUtils.matcherHint)(
  519. '[.not].toHaveProperty',
  520. 'object',
  521. 'path',
  522. {
  523. secondArgument: secondArgument
  524. }
  525. ) +
  526. '\n\n' +
  527. `Expected ${(0, _jestMatcherUtils.EXPECTED_COLOR)(
  528. 'path'
  529. )} to be a string or an array. Received:\n` +
  530. ` ${(0, _jestGetType2.default)(keyPath)}: ${(0,
  531. _jestMatcherUtils.printReceived)(keyPath)}`
  532. );
  533. }
  534. const result = (0, _utils.getPath)(object, keyPath);
  535. const lastTraversedObject = result.lastTraversedObject,
  536. hasEndProp = result.hasEndProp;
  537. const pass = valuePassed
  538. ? (0, _jasmine_utils.equals)(result.value, value, [
  539. _utils.iterableEquality
  540. ])
  541. : hasEndProp;
  542. const traversedPath = result.traversedPath.join('.');
  543. const message = pass
  544. ? () =>
  545. (0, _jestMatcherUtils.matcherHint)(
  546. '.not.toHaveProperty',
  547. 'object',
  548. 'path',
  549. {
  550. secondArgument: secondArgument
  551. }
  552. ) +
  553. '\n\n' +
  554. `Expected the object:\n` +
  555. ` ${(0, _jestMatcherUtils.printReceived)(object)}\n` +
  556. `Not to have a nested property:\n` +
  557. ` ${(0, _jestMatcherUtils.printExpected)(keyPath)}\n` +
  558. (valuePassed
  559. ? `With a value of:\n ${(0, _jestMatcherUtils.printExpected)(
  560. value
  561. )}\n`
  562. : '')
  563. : () => {
  564. const diffString =
  565. valuePassed && hasEndProp
  566. ? (0, _jestDiff2.default)(value, result.value, {
  567. expand: this.expand
  568. })
  569. : '';
  570. return (
  571. (0, _jestMatcherUtils.matcherHint)(
  572. '.toHaveProperty',
  573. 'object',
  574. 'path',
  575. {
  576. secondArgument: secondArgument
  577. }
  578. ) +
  579. '\n\n' +
  580. `Expected the object:\n` +
  581. ` ${(0, _jestMatcherUtils.printReceived)(object)}\n` +
  582. `To have a nested property:\n` +
  583. ` ${(0, _jestMatcherUtils.printExpected)(keyPath)}\n` +
  584. (valuePassed
  585. ? `With a value of:\n ${(0, _jestMatcherUtils.printExpected)(
  586. value
  587. )}\n`
  588. : '') +
  589. (hasEndProp
  590. ? `Received:\n` +
  591. ` ${(0, _jestMatcherUtils.printReceived)(result.value)}` +
  592. (diffString ? `\n\nDifference:\n\n${diffString}` : '')
  593. : traversedPath
  594. ? `Received:\n ${(0, _jestMatcherUtils.RECEIVED_COLOR)(
  595. 'object'
  596. )}.${traversedPath}: ${(0, _jestMatcherUtils.printReceived)(
  597. lastTraversedObject
  598. )}`
  599. : '')
  600. );
  601. };
  602. if (pass === undefined) {
  603. throw new Error('pass must be initialized');
  604. }
  605. return {message: message, pass: pass};
  606. },
  607. toMatch: function(received, expected) {
  608. if (typeof received !== 'string') {
  609. throw new Error(
  610. (0, _jestMatcherUtils.matcherHint)(
  611. '[.not].toMatch',
  612. 'string',
  613. 'expected'
  614. ) +
  615. '\n\n' +
  616. `${(0, _jestMatcherUtils.RECEIVED_COLOR)(
  617. 'string'
  618. )} value must be a string.\n` +
  619. (0, _jestMatcherUtils.printWithType)(
  620. 'Received',
  621. received,
  622. _jestMatcherUtils.printReceived
  623. )
  624. );
  625. }
  626. if (
  627. !(expected && typeof expected.test === 'function') &&
  628. !(typeof expected === 'string')
  629. ) {
  630. throw new Error(
  631. (0, _jestMatcherUtils.matcherHint)(
  632. '[.not].toMatch',
  633. 'string',
  634. 'expected'
  635. ) +
  636. '\n\n' +
  637. `${(0, _jestMatcherUtils.EXPECTED_COLOR)(
  638. 'expected'
  639. )} value must be a string or a regular expression.\n` +
  640. (0, _jestMatcherUtils.printWithType)(
  641. 'Expected',
  642. expected,
  643. _jestMatcherUtils.printExpected
  644. )
  645. );
  646. }
  647. const pass = new RegExp(
  648. typeof expected === 'string'
  649. ? (0, _jestRegexUtil.escapeStrForRegex)(expected)
  650. : expected
  651. ).test(received);
  652. const message = pass
  653. ? () =>
  654. (0, _jestMatcherUtils.matcherHint)('.not.toMatch') +
  655. `\n\nExpected value not to match:\n` +
  656. ` ${(0, _jestMatcherUtils.printExpected)(expected)}` +
  657. `\nReceived:\n` +
  658. ` ${(0, _jestMatcherUtils.printReceived)(received)}`
  659. : () =>
  660. (0, _jestMatcherUtils.matcherHint)('.toMatch') +
  661. `\n\nExpected value to match:\n` +
  662. ` ${(0, _jestMatcherUtils.printExpected)(expected)}` +
  663. `\nReceived:\n` +
  664. ` ${(0, _jestMatcherUtils.printReceived)(received)}`;
  665. return {message: message, pass: pass};
  666. },
  667. toMatchObject: function(receivedObject, expectedObject) {
  668. if (typeof receivedObject !== 'object' || receivedObject === null) {
  669. throw new Error(
  670. (0, _jestMatcherUtils.matcherHint)(
  671. '[.not].toMatchObject',
  672. 'object',
  673. 'expected'
  674. ) +
  675. '\n\n' +
  676. `${(0, _jestMatcherUtils.RECEIVED_COLOR)(
  677. 'received'
  678. )} value must be an object.\n` +
  679. (0, _jestMatcherUtils.printWithType)(
  680. 'Received',
  681. receivedObject,
  682. _jestMatcherUtils.printReceived
  683. )
  684. );
  685. }
  686. if (typeof expectedObject !== 'object' || expectedObject === null) {
  687. throw new Error(
  688. (0, _jestMatcherUtils.matcherHint)(
  689. '[.not].toMatchObject',
  690. 'object',
  691. 'expected'
  692. ) +
  693. '\n\n' +
  694. `${(0, _jestMatcherUtils.EXPECTED_COLOR)(
  695. 'expected'
  696. )} value must be an object.\n` +
  697. (0, _jestMatcherUtils.printWithType)(
  698. 'Expected',
  699. expectedObject,
  700. _jestMatcherUtils.printExpected
  701. )
  702. );
  703. }
  704. const pass = (0, _jasmine_utils.equals)(receivedObject, expectedObject, [
  705. _utils.iterableEquality,
  706. _utils.subsetEquality
  707. ]);
  708. const message = pass
  709. ? () =>
  710. (0, _jestMatcherUtils.matcherHint)('.not.toMatchObject') +
  711. `\n\nExpected value not to match object:\n` +
  712. ` ${(0, _jestMatcherUtils.printExpected)(expectedObject)}` +
  713. `\nReceived:\n` +
  714. ` ${(0, _jestMatcherUtils.printReceived)(receivedObject)}`
  715. : () => {
  716. const diffString = (0, _jestDiff2.default)(
  717. expectedObject,
  718. (0, _utils.getObjectSubset)(receivedObject, expectedObject),
  719. {
  720. expand: this.expand
  721. }
  722. );
  723. return (
  724. (0, _jestMatcherUtils.matcherHint)('.toMatchObject') +
  725. `\n\nExpected value to match object:\n` +
  726. ` ${(0, _jestMatcherUtils.printExpected)(expectedObject)}` +
  727. `\nReceived:\n` +
  728. ` ${(0, _jestMatcherUtils.printReceived)(receivedObject)}` +
  729. (diffString ? `\nDifference:\n${diffString}` : '')
  730. );
  731. };
  732. return {message: message, pass: pass};
  733. },
  734. toStrictEqual: function(received, expected) {
  735. const pass = (0, _jasmine_utils.equals)(
  736. received,
  737. expected,
  738. [_utils.iterableEquality, _utils.typeEquality],
  739. true
  740. );
  741. const message = pass
  742. ? () =>
  743. (0, _jestMatcherUtils.matcherHint)('.not.toStrictEqual') +
  744. '\n\n' +
  745. `Expected value to not equal:\n` +
  746. ` ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
  747. `Received:\n` +
  748. ` ${(0, _jestMatcherUtils.printReceived)(received)}`
  749. : () => {
  750. const diffString = (0, _jestDiff2.default)(expected, received, {
  751. expand: this.expand
  752. });
  753. return (
  754. (0, _jestMatcherUtils.matcherHint)('.toStrictEqual') +
  755. '\n\n' +
  756. `Expected value to equal:\n` +
  757. ` ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
  758. `Received:\n` +
  759. ` ${(0, _jestMatcherUtils.printReceived)(received)}` +
  760. (diffString ? `\n\nDifference:\n\n${diffString}` : '')
  761. );
  762. };
  763. // Passing the the actual and expected objects so that a custom reporter
  764. // could access them, for example in order to display a custom visual diff,
  765. // or create a different error message
  766. return {
  767. actual: received,
  768. expected: expected,
  769. message: message,
  770. name: 'toStrictEqual',
  771. pass: pass
  772. };
  773. }
  774. };
  775. /**
  776. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  777. *
  778. * This source code is licensed under the MIT license found in the
  779. * LICENSE file in the root directory of this source tree.
  780. *
  781. *
  782. */
  783. exports.default = matchers;