解决 toBeInTheDocument does not exist
问题描述
用pnpm
安装create-react-app
创建的typescript
模板应用依赖,执行pnpm run start
会出现错误Property 'toBeInTheDocument' does not exist on type 'Matchers<HTMLElement>'
。
1 | pnpm dlx create-react-app --template typescript |
问题原因
问题的原因是pnpm
V7以上的版本默认不提升@types
的依赖,导致typescript
编译时在node_modules/@types
目录下找不到testing-library/jest-dom
的类型定义,因此报错。参考链接:
- https://github.com/pnpm/pnpm/issues/4920#issuecomment-1225961351
- https://github.com/pnpm/pnpm/pull/4459
问题解决
修改.npmrc
配置,把types
的依赖提升至根目录,然后重新安装依赖
1 | ## ~/.npmrc |