简体   繁体   English

使用 Jest 模拟 React 组件时出现 TypeScript 错误 TS2339

[英]TypeScript error TS2339 when React component mocked with Jest

I am mocking a React component using Jest, but TypeScript is not recognizing the mocked methods and giving a TS2339 error.我是 mocking 一个使用 Jest 的 React 组件,但 TypeScript 无法识别模拟方法并给出 TS2339 错误。

Here's my test:这是我的测试:

jest.mock('./features/News/News');

describe('<App />', () => {
  it('should render the News page on default route', () => {
    // The following line is giving an error:
    // TS2339: Property 'mockImplementation' does not exist on type '() => Element'.
    News.mockImplementation(() => <div>NewsPageMock</div>);

    render(
      <MemoryRouter>
        <App />
      </MemoryRouter>
    );

    expect(screen.getByText('NewsPageMock')).toBeInTheDocument();
  });
});

  • I can use @ts-ignore to ignore the error, but that's a hack.我可以使用@ts-ignore来忽略错误,但这是一个 hack。
  • I understand that ts-jest provides a mocked() function to workaround this.我知道 ts-jest 提供了一个mocked() function 来解决这个问题。 However this is a create-react-app which uses babel, so don't want to introduce ts-jest.然而这是一个使用 babel 的 create-react-app,所以不想引入 ts-jest。

Any other way to make TypeScript happy?还有其他方法可以让 TypeScript 开心吗?

I came up with a better hack than @ts-ignore .我想出了一个比@ts-ignore更好的技巧。 Here it is:这里是:

(News as jest.Mock).mockImplementation(() => <div>NewsPageMock</div>);

This is good enough for me, but happy to consider other answers.这对我来说已经足够了,但很高兴考虑其他答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React + TypeScript:将React组件作为道具传递,使用道具进行渲染[错误:TS2339] - React + TypeScript: Passing React Component as Props, rendering with props [Error: TS2339] 开玩笑+打字稿棉绒错误:TS2339:类型&#39;IntrinsicAttributes不存在属性&#39;store&#39; - Jest + Typescript lint error: TS2339: Property 'store' does not exist on type 'IntrinsicAttributes TS2339:如何为带有剩余参数的React组件的道具创建Typescript接口? - TS2339: How do I create a Typescript interface for props for a React component with rest params? TS2339:类型“ {}”上不存在属性“焦点”。 使用React打字稿 - TS2339: Property 'focus' does not exist on type '{}'. with React typescript TS2339:移植到Typescript时,类型'typeof React'上不存在属性'PropTypes' - TS2339: Property 'PropTypes' does not exist on type 'typeof React' when porting to Typescript TypeScript 类型与 JSX.Element 反应将不接受.map() 并给出错误 TS2339 - TypeScript types with JSX.Element on react will not accept .map()and gives error TS2339 为什么在键入Redux-saga调用参数时Typescript会引发错误-TS2339 - Why does Typescript throw an error when typing Redux-saga call parameter - TS2339 在解构时使用可选属性声明获取TS2339错误 - Getting a TS2339 error with optional property declaration when destructuring 由于错误组件内部错误“ TS2339:类型&#39;Y&#39;上不存在属性&#39;X&#39;”,因此无法设置参考 - Unable to set ref due to error “TS2339: Property 'X' does not exist on type 'Y' ” inside a react component Typescript TS2339:在Typescript + React + Redux应用程序中,类型&#39;IntrinsicAttributes&#39;上不存在属性&#39;queryType&#39;吗? - Typescript TS2339: Property 'queryType' does not exist on type 'IntrinsicAttributes' in Typescript + React + Redux app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM