【问题标题】:TS Error on JEST Testing - SelectOptions.find is not a functionJEST 测试中的 TS 错误 - SelectOptions.find 不是函数
【发布时间】:2019-11-28 13:36:55
【问题描述】:

我有一个简单的实用程序,我在其中返回一个布尔值,基于一些比较功能:

import ISelect from './interfaces/ISelect';

const checkCaseSensitiveSelectUtil = (inputValue: ISelect, selectOptions: ISelect[]): boolean => {
  const exactValueExists = selectOptions.find(input => input.value === inputValue.value);
  return !exactValueExists;
};

export default checkCaseSensitiveSelectUtil;

这是我的测试:

  it('should return TRUE if the values are equal', () => {
    const selectedOptions = {
      label: '*positions',
      value: '*positions'
    };
    expect(checkCaseSensitiveSelectUtil(caseInsensitiveValues, selectedOptions)).toEqual(true);
  });

这是我得到的错误:

    TypeError: selectOptions.find is not a function

      2 |
      3 | const checkCaseSensitiveSelectUtil = (inputValue: ISelect, selectOptions: ISelect[]): boolean => {
    > 4 |   const exactValueExists = selectOptions.find(input => input.value === inputValue.value);
        |                                          ^
      5 |   return !exactValueExists;
      6 | };
      7 |

知道该怎么做吗?我的意思是整个事情都在正常工作。但我有什么问题?

【问题讨论】:

    标签: javascript reactjs typescript jestjs enzyme


    【解决方案1】:

    find 函数存在于数组而非对象中。

    const selectedOptions = {
      label: '*positions',
      value: '*positions'
    };
    

    将上面的内容更改为有效的数组对象。

    试试这个

    const selectedOptions = [{
      label: '*positions',
      value: '*positions'
    }];
    

    【讨论】:

      猜你喜欢
      • 2020-01-05
      • 1970-01-01
      • 2020-10-22
      • 2020-07-31
      • 1970-01-01
      • 2020-02-28
      • 2019-07-31
      • 2019-10-01
      • 2022-11-18
      相关资源
      最近更新 更多