Node.js 在Jest中测试函数参数数据类型
我有以下功能:Node.js 在Jest中测试函数参数数据类型,node.js,unit-testing,jestjs,Node.js,Unit Testing,Jestjs,我有以下功能: export const getRotation = (elementId, position) => { if (typeof elementId !== 'string') { throw new TypeError('Argument "elementId" is not a string!'); } if (typeof position !== 'number') { throw new
export const getRotation = (elementId, position) => {
if (typeof elementId !== 'string') {
throw new TypeError('Argument "elementId" is not a string!');
}
if (typeof position !== 'number') {
throw new TypeError('Argument "position" is not a number!');
}
// ...
};
是否有一种方法可以在不必检查每种数据类型的情况下正确测试此函数的参数?像这样:
it('should throw if argument "elementId" is an object', () => {
const elementId = {};
expect(() => {
getRotation(elementId);
}).toThrow();
});
it('should throw if argument "elementId" is boolean', () => {
const elementId = true;
expect(() => {
getRotation(elementId);
}).toThrow();
});
// ...
像这样的
it('should throw if argument "elementId" is not string or number', () => {
[{}, true].forEach(elementId => {
expect(() => {
getRotation(elementId);
}).toThrow();
})
});