Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'to' does not exist on type 'FunctionMatchers<any>'

I am using React, Jest and Typescript and following these docs to create a unit test using this code:

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

I have all types installed but I am getting the error Property 'to' does not exist on type 'FunctionMatchers<any>'. when I use to in the test.

It seems there maybe some confusion over whether to is coming form Jest or Jasmine? I'm not 100% sure but I am following the Enzyme docs exactly.

Update

I am now having problems structuring this test - following the docs and the comment below does not seem to allow the test to work. I am trying to test for props rendering properly:

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

Throws the error:

    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);
         |
like image 993
Mr. Robot Avatar asked Sep 18 '25 04:09

Mr. Robot


1 Answers

If you are using Jest you should use .toBe() instead of .to.equal() as described here:

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

If the parameter (dummyValue) is an object you should use .toMatchObject() as described here:

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

like image 101
Emanuele Scarabattoli Avatar answered Sep 20 '25 18:09

Emanuele Scarabattoli