Omortis
Omortis

Reputation: 1530

expect().to.have. returns undefined error

Per the enzyme docs here the following stanza should find a selector by ID:

  it('Should render the Select Vehicle entry', () => {
    let component = shallow(<VehicleMenu {...initialState} />);
    expect(component.find("#vehMenuSelect")).to.have.lengthOf(1);
  });

But the .to.have.[...] method never works for me anywhere in my code, and always returns:

TypeError: Cannot read property 'have' of undefined

no matter which selector type I use (in this case find).

This works, and is what I have been using:

  it('Should render the Select Vehicle entry', () => {
    let component = shallow(<VehicleMenu {...initialState} />);
    expect(component.find("#vehMenuSelect").length).toBe(1);
  });

.toBe() always works. Why do I get that error when using the methods described in the current enzyme docs? THis is with enzyme 3.9.0 and enzyme-adapter-react-16 1.12.1.

Upvotes: 0

Views: 153

Answers (1)

Rannie Aguilar Peralta
Rannie Aguilar Peralta

Reputation: 1752

The example from the docs of enzyme is not using jest. You should follow the docs of jest. Change it to toHaveLength instead of to.have.length

Upvotes: 1

Related Questions