Having `afterEach(cleanup)` in the test setup file breaks exported `screen` module on Bun test
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 50/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 冷清
- 技术栈
- react, typescript
- 领域
- frontend, testing-qa
调研方向
首先按照所示,使用 test.setup.ts 和 Component.spec.tsx 运行 bun test --preload test.setup.ts。跟踪 afterEach(cleanup) 如何与导出的 screen 以及 render 使用的文档交互。完成标准是:复现能够使用导出的 screen,同时保留 setup 级别的 cleanup,并包含一个覆盖 Bun 的回归测试。
由索引模型根据 Issue 内容生成。
描述
@testing-library/reactversion:15.0.7- Testing Framework and version:
Bun test v1.1.21 - DOM Environment:
@happy-dom/[email protected]
Relevant code or config:
test.setup.ts
import { GlobalRegistrator } from '@happy-dom/global-registrator';
import { cleanup } from '@testing-library/react';
import { expect, afterEach } from 'bun:test';
import * as matchers from '@testing-library/jest-dom/matchers';
GlobalRegistrator.register();
declare module 'bun:test' {
interface Matchers<T> extends matchers.TestingLibraryMatchers<any, void> {}
interface AsymmetricMatchers extends matchers.TestingLibraryMatchers<any, void> {}
}
expect.extend(matchers);
afterEach(cleanup);
Component.spec.tsx
import { act, render, screen } from '@testing-library/react';
import { TextAreaInput } from './TextAreaInput.component';
import { describe, it } from 'bun:test';
describe('TextAreaInput', () => {
it('Should show in the DOM', async () => {
const { getByRole } = await act(() => render(<TextAreaInput />));
// Works fine!
getByRole('textbox')
// throws an error
screen.getByRole('textbox');
// TypeError: For queries bound to document.body a global document has to be available... Learn more: https://testing-library.com/s/screen-global-error
});
});
What you did:
I'm just trying to test a component using the screen exported by react testing library using Bun test.
What happened:
everything was smooth, until I tried to use the screen exported from the react testing library it threw an error about "global document has to be available",
upon inspecting, removing afterEach(cleanup) from the test setup file or writing afterEach(cleanup) in each test file will make it works,
Reproduction:
just run bun test --preload test.setup.ts
Problem description:
using the screen exported while having the cleanup code in the test setup will break the test
Suggested solution:
just use the returned screen from the render function.
- 主要语言
- JavaScript
- 星标
- 19.7k
- 派生
- 1.2k
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
testing-library/react-testing-library 的其他 Issue
-
fireEvent.select does not wrap its automatic native focus in act可能已有人在做 @sergioperezcheco 于 6 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 88/100
-
bug: calling configure() without reactStrictMode resets it to undefined, silently disabling strict mode可能重新可做 @suhailopensource 于 76 天前认领,目前没有进行中的 PR。 未关闭
难度 2/5 1-3 小时 新手友好度 35/100
testing-library/react-testing-library#1466 · 1 条评论 ·
-
难度 4/5 3-5 天 新手友好度 30/100
testing-library/react-testing-library#1459 · 2 条评论 ·
-
perf: optimize container lookup with early exit可能重新可做 @Ch-Abhinav-Chowdary 于 302 天前认领,目前没有进行中的 PR。 未关闭
难度 1/5 1 小时以内 新手友好度 35/100
testing-library/react-testing-library#1430 · 1 条评论 ·
-
`fireEvent.mouseEnter` does not forward `relatedTarget` (relatedTarget is the window instead)可能已有人在做 @swarnim02 于 320 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 55/100
查看 testing-library/react-testing-library 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 60/100
quarto-dev/quarto-cli#15042 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 62/100
extendr/extendr.github.io#134 ·
-
OpenCode install copies the Pi harness directory (`.pi/`) into the OpenCode config root可能已有人在做 @gaurav0107 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 75/100
维护者通常 3 天内回复
-
难度 2/5 1-3 小时 新手友好度 70/100
NaturalIntelligence/fast-xml-parser#890 · 1 条评论 ·
维护者通常 3 天内回复
-
Docs viewer: inverted grey colormap, Phantom CT window -20 to 35 HU, smoothing up to 40 mm可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭area: docs enhancement
难度 2/5 1-3 小时 新手友好度 64/100
PyTomography/PyTomography#331 ·
维护者通常 1 天内回复