jestcustommatchers
Version:
A samll lib of jest custom matchers
104 lines (99 loc) • 3.96 kB
JavaScript
const toBeOfTypes = ({
received: received,
types: types,
strict: strict = false
} = {}) => {
const verifyType = (type) => {
const noStrictTypes = ['string', 'boolean', 'undefined', 'symbol', 'bigint', 'function'];
const strictAndNoStrictTypes = ['number', 'object'];
if ((noStrictTypes.indexOf(type) != -1 ||
(!strict && strictAndNoStrictTypes.indexOf(type) != -1)) &&
!typeof received === type
) return false;
if (type == 'number' &&
(!typeof received === 'number' || !isNaN(received))
) return false;
if (type == 'object' &&
(!typeof received === 'object' || Array.isArray(received) || received === null)
) return false;
if (type == 'float' && isNaN(parseFloat(received))) return false;
if (type == 'array' && !Array.isArray(received)) return false;
if (type == 'null' && received !== null) return false;
if ((type == 'nan' || type == 'NaN') && !isNaN(received)) return false;
return true;
}
if (!Array.isArray(types)) types = [types];
const allowedTypes = ['string', 'boolean', 'undefined', 'symbol', 'bigint', 'function', 'number', 'object', 'float', 'array', 'null', 'nan', 'NaN'];
for (const check of types) {
if (allowedTypes.indexOf(check) == -1) throw new Error(`The type ${check} cannot be asserted using this function`);
if (verifyType(check) == false) return false;
}
return true;
};
const toHaveRangedLengthAndType = ({
received: received,
min: min = 0,
max: max,
type: type = 'any'
} = {}) => {
if (min == 0 && typeof max == 'undefined') console.warning(`You are using toHaveRangedLengthAndType assertion with min == 0 and no max, maybe. It seems like you should use a different assertion, maybe toBeOfType`);
if (type != 'any') {
if ((type == 'array' && !toBeOfTypes({
received: received,
types: 'array'
})) ||
(type == 'object' && !toBeOfTypes({
received: received,
types: 'object',
strict: true
})) ||
(type == 'string' && !toBeOfTypes({
received: received,
types: 'string'
}))) return false;
if (type != 'object' && type != 'array' && type != 'string') throw new Error(`You are checking the length of a type without any length`);
}
if (toBeOfTypes({
received: received,
types: 'object',
strict: true
}) &&
(Object.keys(received).length < min || Object.keys(received).length > max)
) return false;
if ((toBeOfTypes({
received: received,
types: ['string', 'array']
})) &&
(received.length < min || received.length > max)
) return false;
return true;
};
expect.extend({
toHaveRangedLengthAndType(received, min, max, type) {
const pass = toHaveRangedLengthAndType({
received: received,
min: min = 0,
max: max,
type: type = 'any'
});
return {
message: () => `expected ${JSON.stringify(received)} to have length ${min != 0 ? `of at list ${min} and` : ''}${typeof max != 'undefined' ? `at most ${max}` : ''}${type != 'any' ? `and to be of type ${type}` : ''}`,
pass: pass
};
},
toBeOfTypes({
received: received,
types: types,
strict: strict = false
} = {}) {
const pass = toBeOfTypes({
received: received,
types: types,
strict: strict = false
} = {});
return {
message: () => `expected ${JSON.stringify(received)} to be of type ${type}`,
pass: pass
};
},
});