使用 Create React App 和 TypeScript 创建 React 应用程序时出现 TypeScript 错误“找不到名称‘expect’’

问题描述 投票:0回答:0
TS2339: Property 'toBeInTheDocument' does not exist on type 'JestMatchers<HTMLElement>'.
     6 |   render(<App />);
     7 |   const linkElement = screen.getByText(/learn react/i);
  >  8 |   expect(linkElement).toBeInTheDocument();
       |                       ^^^^^^^^^^^^^^^^^
     9 | });

每当我使用

创建 React 应用程序时
yarn create react-app <app-name> --template typescript

我收到这些错误,可以通过删除

App.test.tsx
文件来删除它们,但我想要永久修复。

TS2339: Property 'toBeInTheDocument' does not exist on type 'JestMatchers<HTMLElement>'.
     6 |   render(<App />);
     7 |   const linkElement = screen.getByText(/learn react/i);
  >  8 |   expect(linkElement).toBeInTheDocument();
       |                       ^^^^^^^^^^^^^^^^^
     9 | });
javascript node.js reactjs typescript yarnpkg
© www.soinside.com 2019 - 2024. All rights reserved.