UNPKG

4.53 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.matches = exports.captor = exports.notEmpty = exports.notUndefined = exports.notNull = exports.objectContainsValue = exports.objectContainsKey = exports.mapHas = exports.setHas = exports.arrayIncludes = exports.isA = exports.anySet = exports.anyMap = exports.anyArray = exports.anyObject = exports.anySymbol = exports.anyFunction = exports.anyString = exports.anyNumber = exports.anyBoolean = exports.any = exports.CaptorMatcher = exports.Matcher = void 0;
4// needs to be a class so we can instanceof
5class Matcher {
6 constructor(asymmetricMatch, description) {
7 this.asymmetricMatch = asymmetricMatch;
8 this.description = description;
9 this.$$typeof = Symbol.for('jest.asymmetricMatcher');
10 }
11 toString() {
12 return this.description;
13 }
14 toAsymmetricMatcher() {
15 return this.description;
16 }
17 getExpectedType() {
18 return 'undefined';
19 }
20}
21exports.Matcher = Matcher;
22class CaptorMatcher {
23 constructor() {
24 this.values = [];
25 this.$$typeof = Symbol.for('jest.asymmetricMatcher');
26 this.asymmetricMatch = (actualValue) => {
27 // @ts-ignore
28 this.value = actualValue;
29 this.values.push(actualValue);
30 return true;
31 };
32 }
33 getExpectedType() {
34 return 'Object';
35 }
36 toString() {
37 return 'captor';
38 }
39 toAsymmetricMatcher() {
40 return 'captor';
41 }
42}
43exports.CaptorMatcher = CaptorMatcher;
44const any = () => new Matcher(() => true, 'any()');
45exports.any = any;
46const anyBoolean = () => new Matcher((actualValue) => typeof actualValue === 'boolean', 'anyBoolean()');
47exports.anyBoolean = anyBoolean;
48const anyNumber = () => new Matcher((actualValue) => typeof actualValue === 'number' && !isNaN(actualValue), 'anyNumber()');
49exports.anyNumber = anyNumber;
50const anyString = () => new Matcher((actualValue) => typeof actualValue === 'string', 'anyString()');
51exports.anyString = anyString;
52const anyFunction = () => new Matcher((actualValue) => typeof actualValue === 'function', 'anyFunction()');
53exports.anyFunction = anyFunction;
54const anySymbol = () => new Matcher((actualValue) => typeof actualValue === 'symbol', 'anySymbol()');
55exports.anySymbol = anySymbol;
56const anyObject = () => new Matcher((actualValue) => typeof actualValue === 'object' && actualValue !== null, 'anyObject()');
57exports.anyObject = anyObject;
58const anyArray = () => new Matcher((actualValue) => Array.isArray(actualValue), 'anyArray()');
59exports.anyArray = anyArray;
60const anyMap = () => new Matcher((actualValue) => actualValue instanceof Map, 'anyMap()');
61exports.anyMap = anyMap;
62const anySet = () => new Matcher((actualValue) => actualValue instanceof Set, 'anySet()');
63exports.anySet = anySet;
64const isA = (clazz) => new Matcher((actualValue) => actualValue instanceof clazz, 'isA()');
65exports.isA = isA;
66const arrayIncludes = (arrayVal) => new Matcher((actualValue) => Array.isArray(actualValue) && actualValue.includes(arrayVal), 'arrayIncludes()');
67exports.arrayIncludes = arrayIncludes;
68const setHas = (arrayVal) => new Matcher((actualValue) => (0, exports.anySet)().asymmetricMatch(actualValue) && actualValue.has(arrayVal), 'setHas()');
69exports.setHas = setHas;
70const mapHas = (mapVal) => new Matcher((actualValue) => (0, exports.anyMap)().asymmetricMatch(actualValue) && actualValue.has(mapVal), 'mapHas()');
71exports.mapHas = mapHas;
72const objectContainsKey = (key) => new Matcher((actualValue) => (0, exports.anyObject)().asymmetricMatch(actualValue) && actualValue[key] !== undefined, 'objectContainsKey()');
73exports.objectContainsKey = objectContainsKey;
74const objectContainsValue = (value) => new Matcher((actualValue) => (0, exports.anyObject)().asymmetricMatch(actualValue) && Object.values(actualValue).includes(value), 'objectContainsValue()');
75exports.objectContainsValue = objectContainsValue;
76const notNull = () => new Matcher((actualValue) => actualValue !== null, 'notNull()');
77exports.notNull = notNull;
78const notUndefined = () => new Matcher((actualValue) => actualValue !== undefined, 'notUndefined()');
79exports.notUndefined = notUndefined;
80const notEmpty = () => new Matcher((actualValue) => actualValue !== null && actualValue !== undefined && actualValue !== '', 'notEmpty()');
81exports.notEmpty = notEmpty;
82const captor = () => new CaptorMatcher();
83exports.captor = captor;
84const matches = (matcher) => new Matcher(matcher, 'matches()');
85exports.matches = matches;