【发布时间】:2026-02-01 00:50:01
【问题描述】:
我刚刚下载了使用反应测试库的 Create-React-App。当我使用它的方法时,我没有得到智能感知(例如:toBeInTheDocument)。如何设置智能感知?我们有 RTL 的类型定义吗?
import React from 'react';
import { render } from '@testing-library/react';
import App from './App';
test('renders learn react link', () => {
const { getByText } = render(<App />);
const linkElement = getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
});
【问题讨论】:
-
你的意思是intellisense?这有帮助吗? code.visualstudio.com/docs/editor/…
-
不,这不是关于 VScode,而是关于与 jest 相关的类型,它们工作正常,但没有为它们显示智能感知。
-
是的,vscode 需要类型定义。抱歉,我的意思是提到 jest 的类型,并且还包括一个指向 jest 的链接以扩展匹配器,因为我找不到与更新 vscode 类型相关的任何内容。 jestjs.io/docs/en/expect#expectextendmatchers 扩展 jest 没有或没有更新 vscode 中的智能感知,所以也许也不是正确的解决方案。
-
@DrewReese ,那么如何使这项工作。有什么配置步骤吗?我希望显示智能感知,并且在“转到定义”上它必须跳转到定义。仅供参考:我在上面的问题中添加了一个新的屏幕截图。
-
纱线添加 -D @testing-library/jest-dom
标签: reactjs typescript visual-studio-code jestjs react-testing-library