【问题标题】:Jest, testing object contains correct function type开玩笑,测试对象包含正确的函数类型
【发布时间】:2020-02-17 11:15:24
【问题描述】:

我有这个方法:

export function getTableConfig(priceEntity: any) {
  const columns: any = { columns: [] };
  const keys: Array<string> = Object.keys(priceEntity);
  keys.forEach((key: string) => {
    columns.columns.push({
      key: key,
      label: key,
      values: (obj: any) => ({ value: obj[key] })
    });
  });

  return columns;
}

我正在尝试使用 Jest 测试返回。我写了这样的东西:

expect(tableConfig).toEqual({
  columns: [
    { key: 'id', label: 'id', values: expect.any(Function) },
  ]
});

但我想更具体地说明函数的类型,比如

expect(tableConfig).toEqual({
  columns: [
    { key: 'id', label: 'id', values: (obj: any) => ({ value: string }) }
  ]
});

但我不知道该怎么做。有什么想法吗?

【问题讨论】:

    标签: reactjs typescript jestjs


    【解决方案1】:

    你可以试试这样的

    expect(tableConfig).toEqual({
      columns: [
        { key: 'id', label: 'id', values: (obj: any) => ({ value: string }) }
      ]
    });
    expect(tableConfig.columns[0].values({id: 'testId'})).toBe('testId');
    

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 2021-08-19
      • 2020-08-24
      • 2020-03-10
      • 1970-01-01
      • 2018-05-25
      • 2019-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多