> ## Documentation Index
> Fetch the complete documentation index at: https://bun.zhcndoc.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 在 Bun 中使用 Testing Library

你可以在 Bun 的测试运行器中使用 [Testing Library](https://testing-library.com/)。

***

使用 Testing Library 的前提是你需要安装 [Happy Dom](https://github.com/capricorn86/happy-dom)。（[查看 Bun 的 Happy DOM 指南获取更多信息](/guides/test/happy-dom)）。

```sh terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
bun add -D @happy-dom/global-registrator
```

***

接下来你应当安装打算使用的 Testing Library 包。例如，如果你正在为 React 设置测试，你可以这样安装。你还需要安装 `@testing-library/jest-dom`，以便后续使用 matcher。

```sh terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
bun add -D @testing-library/react @testing-library/dom @testing-library/jest-dom
```

***

接下来你需要为 Happy DOM 和 Testing Library 创建预加载脚本。有关 Happy DOM 设置脚本的更多细节，请参阅 [Bun 的 Happy DOM 指南](/guides/test/happy-dom)。

```ts happydom.ts icon="https://mintcdn.com/bun-zhcndoc/cnUTwgMuf4cCrwC-/icons/typescript.svg?fit=max&auto=format&n=cnUTwgMuf4cCrwC-&q=85&s=e7767043c9e885c34f2d6c8fe2a95217" theme={"theme":{"light":"github-light","dark":"dracula"}}
import { GlobalRegistrator } from "@happy-dom/global-registrator";

GlobalRegistrator.register();
```

***

对于 Testing Library，你需要使用 Testing Library 的 matcher 来扩展 Bun 的 `expect` 函数。可选地，为了更好地匹配像 Jest 这样的测试运行器的行为，你可能想在每个测试后运行清理操作。

```ts testing-library.ts icon="https://mintcdn.com/bun-zhcndoc/cnUTwgMuf4cCrwC-/icons/typescript.svg?fit=max&auto=format&n=cnUTwgMuf4cCrwC-&q=85&s=e7767043c9e885c34f2d6c8fe2a95217" theme={"theme":{"light":"github-light","dark":"dracula"}}
import { afterEach, expect } from "bun:test";
import { cleanup } from "@testing-library/react";
import * as matchers from "@testing-library/jest-dom/matchers";

expect.extend(matchers);

// 可选：在每个测试后清理 `render`
afterEach(() => {
  cleanup();
});
```

***

然后，将这些预加载脚本添加到你的 `bunfig.toml` 中（如果你愿意，也可以将所有内容放在单个 `preload.ts` 脚本里）。

```toml bunfig.toml icon="settings" theme={"theme":{"light":"github-light","dark":"dracula"}}
[test]
preload = ["./happydom.ts", "./testing-library.ts"]
```

***

如果你在使用 TypeScript，你还需要使用声明合并，以便在编辑器中显示新的 matcher 类型。为此，你需要创建一个类型声明文件，像这样扩展 `Matchers`。

```ts matchers.d.ts icon="https://mintcdn.com/bun-zhcndoc/cnUTwgMuf4cCrwC-/icons/typescript.svg?fit=max&auto=format&n=cnUTwgMuf4cCrwC-&q=85&s=e7767043c9e885c34f2d6c8fe2a95217" theme={"theme":{"light":"github-light","dark":"dracula"}}
import { TestingLibraryMatchers } from "@testing-library/jest-dom/matchers";
import { Matchers, AsymmetricMatchers } from "bun:test";

declare module "bun:test" {
  interface Matchers<T> extends TestingLibraryMatchers<typeof expect.stringContaining, T> {}
  interface AsymmetricMatchers extends TestingLibraryMatchers {}
}
```

***

现在你应该可以在测试中使用 Testing Library 了。

```tsx myComponent.test.tsx icon="https://mintcdn.com/bun-zhcndoc/cnUTwgMuf4cCrwC-/icons/typescript.svg?fit=max&auto=format&n=cnUTwgMuf4cCrwC-&q=85&s=e7767043c9e885c34f2d6c8fe2a95217" theme={"theme":{"light":"github-light","dark":"dracula"}}
import { test, expect } from "bun:test";
import { screen, render } from "@testing-library/react";
import { MyComponent } from "./myComponent";

test("Can use Testing Library", () => {
  render(MyComponent);
  const myComponent = screen.getByTestId("my-component");
  expect(myComponent).toBeInTheDocument();
});
```

***

有关使用 Bun 编写浏览器测试的完整文档，请参阅 [Testing Library 文档](https://testing-library.com/)、[Happy DOM 仓库](https://github.com/capricorn86/happy-dom) 以及 [文档 > 测试运行器 > DOM](/test/dom)。
