asymmetricMatchers.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.stringNotMatching =
  6. exports.stringNotContaining =
  7. exports.stringMatching =
  8. exports.stringContaining =
  9. exports.objectNotContaining =
  10. exports.objectContaining =
  11. exports.notCloseTo =
  12. exports.closeTo =
  13. exports.arrayNotContaining =
  14. exports.arrayContaining =
  15. exports.anything =
  16. exports.any =
  17. exports.AsymmetricMatcher =
  18. void 0;
  19. var matcherUtils = _interopRequireWildcard(require('jest-matcher-utils'));
  20. var _jasmineUtils = require('./jasmineUtils');
  21. var _jestMatchersObject = require('./jestMatchersObject');
  22. var _utils = require('./utils');
  23. function _getRequireWildcardCache(nodeInterop) {
  24. if (typeof WeakMap !== 'function') return null;
  25. var cacheBabelInterop = new WeakMap();
  26. var cacheNodeInterop = new WeakMap();
  27. return (_getRequireWildcardCache = function (nodeInterop) {
  28. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  29. })(nodeInterop);
  30. }
  31. function _interopRequireWildcard(obj, nodeInterop) {
  32. if (!nodeInterop && obj && obj.__esModule) {
  33. return obj;
  34. }
  35. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  36. return {default: obj};
  37. }
  38. var cache = _getRequireWildcardCache(nodeInterop);
  39. if (cache && cache.has(obj)) {
  40. return cache.get(obj);
  41. }
  42. var newObj = {};
  43. var hasPropertyDescriptor =
  44. Object.defineProperty && Object.getOwnPropertyDescriptor;
  45. for (var key in obj) {
  46. if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
  47. var desc = hasPropertyDescriptor
  48. ? Object.getOwnPropertyDescriptor(obj, key)
  49. : null;
  50. if (desc && (desc.get || desc.set)) {
  51. Object.defineProperty(newObj, key, desc);
  52. } else {
  53. newObj[key] = obj[key];
  54. }
  55. }
  56. }
  57. newObj.default = obj;
  58. if (cache) {
  59. cache.set(obj, newObj);
  60. }
  61. return newObj;
  62. }
  63. var global = (function () {
  64. if (typeof globalThis !== 'undefined') {
  65. return globalThis;
  66. } else if (typeof global !== 'undefined') {
  67. return global;
  68. } else if (typeof self !== 'undefined') {
  69. return self;
  70. } else if (typeof window !== 'undefined') {
  71. return window;
  72. } else {
  73. return Function('return this')();
  74. }
  75. })();
  76. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  77. function _defineProperty(obj, key, value) {
  78. if (key in obj) {
  79. Object.defineProperty(obj, key, {
  80. value: value,
  81. enumerable: true,
  82. configurable: true,
  83. writable: true
  84. });
  85. } else {
  86. obj[key] = value;
  87. }
  88. return obj;
  89. }
  90. const utils = Object.freeze({
  91. ...matcherUtils,
  92. iterableEquality: _utils.iterableEquality,
  93. subsetEquality: _utils.subsetEquality
  94. });
  95. class AsymmetricMatcher {
  96. constructor(sample, inverse = false) {
  97. _defineProperty(this, '$$typeof', Symbol.for('jest.asymmetricMatcher'));
  98. this.sample = sample;
  99. this.inverse = inverse;
  100. }
  101. getMatcherContext() {
  102. return {
  103. ...(0, _jestMatchersObject.getState)(),
  104. equals: _jasmineUtils.equals,
  105. isNot: this.inverse,
  106. utils
  107. };
  108. }
  109. }
  110. exports.AsymmetricMatcher = AsymmetricMatcher;
  111. class Any extends AsymmetricMatcher {
  112. constructor(sample) {
  113. if (typeof sample === 'undefined') {
  114. throw new TypeError(
  115. 'any() expects to be passed a constructor function. ' +
  116. 'Please pass one or use anything() to match any object.'
  117. );
  118. }
  119. super(sample);
  120. }
  121. asymmetricMatch(other) {
  122. if (this.sample == String) {
  123. return typeof other == 'string' || other instanceof String;
  124. }
  125. if (this.sample == Number) {
  126. return typeof other == 'number' || other instanceof Number;
  127. }
  128. if (this.sample == Function) {
  129. return typeof other == 'function' || other instanceof Function;
  130. }
  131. if (this.sample == Boolean) {
  132. return typeof other == 'boolean' || other instanceof Boolean;
  133. }
  134. if (this.sample == BigInt) {
  135. return typeof other == 'bigint' || other instanceof BigInt;
  136. }
  137. if (this.sample == Symbol) {
  138. return typeof other == 'symbol' || other instanceof Symbol;
  139. }
  140. if (this.sample == Object) {
  141. return typeof other == 'object';
  142. }
  143. return other instanceof this.sample;
  144. }
  145. toString() {
  146. return 'Any';
  147. }
  148. getExpectedType() {
  149. if (this.sample == String) {
  150. return 'string';
  151. }
  152. if (this.sample == Number) {
  153. return 'number';
  154. }
  155. if (this.sample == Function) {
  156. return 'function';
  157. }
  158. if (this.sample == Object) {
  159. return 'object';
  160. }
  161. if (this.sample == Boolean) {
  162. return 'boolean';
  163. }
  164. return (0, _jasmineUtils.fnNameFor)(this.sample);
  165. }
  166. toAsymmetricMatcher() {
  167. return 'Any<' + (0, _jasmineUtils.fnNameFor)(this.sample) + '>';
  168. }
  169. }
  170. class Anything extends AsymmetricMatcher {
  171. asymmetricMatch(other) {
  172. return !(0, _jasmineUtils.isUndefined)(other) && other !== null;
  173. }
  174. toString() {
  175. return 'Anything';
  176. } // No getExpectedType method, because it matches either null or undefined.
  177. toAsymmetricMatcher() {
  178. return 'Anything';
  179. }
  180. }
  181. class ArrayContaining extends AsymmetricMatcher {
  182. constructor(sample, inverse = false) {
  183. super(sample, inverse);
  184. }
  185. asymmetricMatch(other) {
  186. if (!Array.isArray(this.sample)) {
  187. throw new Error(
  188. `You must provide an array to ${this.toString()}, not '` +
  189. typeof this.sample +
  190. "'."
  191. );
  192. }
  193. const result =
  194. this.sample.length === 0 ||
  195. (Array.isArray(other) &&
  196. this.sample.every(item =>
  197. other.some(another => (0, _jasmineUtils.equals)(item, another))
  198. ));
  199. return this.inverse ? !result : result;
  200. }
  201. toString() {
  202. return `Array${this.inverse ? 'Not' : ''}Containing`;
  203. }
  204. getExpectedType() {
  205. return 'array';
  206. }
  207. }
  208. class ObjectContaining extends AsymmetricMatcher {
  209. constructor(sample, inverse = false) {
  210. super(sample, inverse);
  211. }
  212. asymmetricMatch(other) {
  213. if (typeof this.sample !== 'object') {
  214. throw new Error(
  215. `You must provide an object to ${this.toString()}, not '` +
  216. typeof this.sample +
  217. "'."
  218. );
  219. }
  220. let result = true;
  221. for (const property in this.sample) {
  222. if (
  223. !(0, _jasmineUtils.hasProperty)(other, property) ||
  224. !(0, _jasmineUtils.equals)(this.sample[property], other[property])
  225. ) {
  226. result = false;
  227. break;
  228. }
  229. }
  230. return this.inverse ? !result : result;
  231. }
  232. toString() {
  233. return `Object${this.inverse ? 'Not' : ''}Containing`;
  234. }
  235. getExpectedType() {
  236. return 'object';
  237. }
  238. }
  239. class StringContaining extends AsymmetricMatcher {
  240. constructor(sample, inverse = false) {
  241. if (!(0, _jasmineUtils.isA)('String', sample)) {
  242. throw new Error('Expected is not a string');
  243. }
  244. super(sample, inverse);
  245. }
  246. asymmetricMatch(other) {
  247. const result =
  248. (0, _jasmineUtils.isA)('String', other) && other.includes(this.sample);
  249. return this.inverse ? !result : result;
  250. }
  251. toString() {
  252. return `String${this.inverse ? 'Not' : ''}Containing`;
  253. }
  254. getExpectedType() {
  255. return 'string';
  256. }
  257. }
  258. class StringMatching extends AsymmetricMatcher {
  259. constructor(sample, inverse = false) {
  260. if (
  261. !(0, _jasmineUtils.isA)('String', sample) &&
  262. !(0, _jasmineUtils.isA)('RegExp', sample)
  263. ) {
  264. throw new Error('Expected is not a String or a RegExp');
  265. }
  266. super(new RegExp(sample), inverse);
  267. }
  268. asymmetricMatch(other) {
  269. const result =
  270. (0, _jasmineUtils.isA)('String', other) && this.sample.test(other);
  271. return this.inverse ? !result : result;
  272. }
  273. toString() {
  274. return `String${this.inverse ? 'Not' : ''}Matching`;
  275. }
  276. getExpectedType() {
  277. return 'string';
  278. }
  279. }
  280. class CloseTo extends AsymmetricMatcher {
  281. constructor(sample, precision = 2, inverse = false) {
  282. if (!(0, _jasmineUtils.isA)('Number', sample)) {
  283. throw new Error('Expected is not a Number');
  284. }
  285. if (!(0, _jasmineUtils.isA)('Number', precision)) {
  286. throw new Error('Precision is not a Number');
  287. }
  288. super(sample);
  289. _defineProperty(this, 'precision', void 0);
  290. this.inverse = inverse;
  291. this.precision = precision;
  292. }
  293. asymmetricMatch(other) {
  294. if (!(0, _jasmineUtils.isA)('Number', other)) {
  295. return false;
  296. }
  297. let result = false;
  298. if (other === Infinity && this.sample === Infinity) {
  299. result = true; // Infinity - Infinity is NaN
  300. } else if (other === -Infinity && this.sample === -Infinity) {
  301. result = true; // -Infinity - -Infinity is NaN
  302. } else {
  303. result =
  304. Math.abs(this.sample - other) < Math.pow(10, -this.precision) / 2;
  305. }
  306. return this.inverse ? !result : result;
  307. }
  308. toString() {
  309. return `Number${this.inverse ? 'Not' : ''}CloseTo`;
  310. }
  311. getExpectedType() {
  312. return 'number';
  313. }
  314. }
  315. const any = expectedObject => new Any(expectedObject);
  316. exports.any = any;
  317. const anything = () => new Anything();
  318. exports.anything = anything;
  319. const arrayContaining = sample => new ArrayContaining(sample);
  320. exports.arrayContaining = arrayContaining;
  321. const arrayNotContaining = sample => new ArrayContaining(sample, true);
  322. exports.arrayNotContaining = arrayNotContaining;
  323. const objectContaining = sample => new ObjectContaining(sample);
  324. exports.objectContaining = objectContaining;
  325. const objectNotContaining = sample => new ObjectContaining(sample, true);
  326. exports.objectNotContaining = objectNotContaining;
  327. const stringContaining = expected => new StringContaining(expected);
  328. exports.stringContaining = stringContaining;
  329. const stringNotContaining = expected => new StringContaining(expected, true);
  330. exports.stringNotContaining = stringNotContaining;
  331. const stringMatching = expected => new StringMatching(expected);
  332. exports.stringMatching = stringMatching;
  333. const stringNotMatching = expected => new StringMatching(expected, true);
  334. exports.stringNotMatching = stringNotMatching;
  335. const closeTo = (expected, precision) => new CloseTo(expected, precision);
  336. exports.closeTo = closeTo;
  337. const notCloseTo = (expected, precision) =>
  338. new CloseTo(expected, precision, true);
  339. exports.notCloseTo = notCloseTo;