Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Typescript facing issue with enzyme [duplicate]

I have a component app.tsx


import React, { useState } from "react";

const TestCom = () => {
 return(
     <>
     <div className="head">hey there</div>
     <select name="xyz" id="uni">
         <option value="abc">abc</option>
     </select>
     </>
 )
}

export default TestCom

I am testing it with jest and enzyme app.test.tsx

import  TestCom  from "./foo";
import { shallow } from 'enzyme';
import React from "react";


 test("basic test", () => {
 const app= shallow(<TestCom />)
  expect(app.find('div.head')).to.have.lengthOf(1);
 });

whenever I am using .to its throwing error

Property 'to' does not exist on type 'JestMatchersShape<Matchers<void, ShallowWrapper<HTMLAttributes,
 any, Component<{}, {}, any>>>, Matchers<Promise<void>, ShallowWrapper<HTMLAttributes, any, 
Component<...>>>>'.ts(2339)

I have tried troubleshooting but I am not able to find a fix for it

like image 757
Mohammad Zubair Avatar asked Jul 18 '26 06:07

Mohammad Zubair


1 Answers

Until you add chai library, there is no .to. method. Jest own matchers have .toHaveLength() that you can use:

expect(app.find('div.head')).toHaveLength(1);
like image 122
skyboyer Avatar answered Jul 20 '26 22:07

skyboyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!