类型“ FunctionMatchers <any>”上不存在属性“ to”

时间:2019-11-27 12:00:05

标签: reactjs jestjs enzyme

我正在使用React,Jest和Typescript并遵循these文档使用以下代码创建单元测试:

expect(HorizontalNotIndented.props().includedProp).to.equal(dummyValue);

我已经安装了所有类型,但是在测试中使用Property 'to' does not exist on type 'FunctionMatchers<any>'.时出现错误to

似乎Jest或Jasmine即将对to造成困惑吗?我不确定100%,但是我确实在关注酶文档。

在此先感谢您的指点。

更新

我现在在构建此测试时遇到问题-按照文档和以下注释进行操作似乎无法正常工作。我正在尝试测试道具的渲染效果:

const dummyValue = {
  background: {
    color: 'rgba(38, 176, 17, 0.2)',
    value: 100,
  },
  foreground: {
    color: 'rgb(38, 176, 17)',
    value: 80,
  },
};

const HorizontalNotIndented = shallow(
  <Horizontal
    value={dummyValue}
  />,
);

HorizontalNotIndented.setProps({
  value
});

describe('HCGauge', () => {
  test('Test to see if props are in rendered component', () => {
expect(HorizontalNotIndented.props().value).toEqual(dummyValue);

引发错误:

    expect(received).toEqual(expected) // deep equality

    Expected: {"background": {"color": "rgba(38, 176, 17, 0.2)", "value": 100}, "foreground": {"color": "rgb(38, 176, 17)", "value": 80}}
    Received: undefined

      62 |   test('Test to see if props are in rendered component', () => {
      63 |     // expect('value' in HorizontalNotIndented.props()).toEqual(dummyValue);
    > 64 |     expect(HorizontalNotIndented.props().includedProp).toEqual(dummyValue);
         |

1 个答案:

答案 0 :(得分:0)

如果您使用Jest,则应使用.toBe()而不是.to.equal(),如此处所述:

https://jestjs.io/docs/en/expect.html#tobevalue

如果参数(dummyValue)是对象,则应按照以下说明使用.toMatchObject()

https://jestjs.io/docs/en/expect.html#tomatchobjectobject