【问题标题】:Testing function parameter data types in Jest在 Jest 中测试函数参数数据类型
【发布时间】:2018-03-16 11:30:26
【问题描述】:

我有以下功能:

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

// ...

【问题讨论】:

    标签: node.js unit-testing jestjs


    【解决方案1】:

    像这样?:

    it('should throw if argument "elementId" is not string or number', () => {
        [{}, true].forEach(elementId => {
            expect(() => {
                getRotation(elementId);
            }).toThrow();
        })
    });
    

    【讨论】:

      猜你喜欢
      • 2020-01-05
      • 2018-11-01
      • 2018-06-19
      • 2018-09-27
      • 2020-03-08
      • 1970-01-01
      • 2020-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多