用酶反应Typescript面临的问题

时间:2020-10-22 08:12:10

标签: reactjs typescript jestjs enzyme

我有一个组成部分 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

我正在用玩笑和酵素测试它 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);
 });

每当我使用.to引发错误

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

我已尝试进行故障排除,但找不到解决方法

1 个答案:

答案 0 :(得分:2)

在添加chai library之前,没有.to.方法。笑话own matchers.toHaveLength()可以使用:

expect(app.find('div.head')).toHaveLength(1);