Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在Jest中测试函数参数数据类型_Node.js_Unit Testing_Jestjs - Fatal编程技术网

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();
    })
});