fix: add DOM cleanup to bun-setup.ts

Add afterEach hook to clear document.body.innerHTML between tests.
This prevents test pollution where elements from previous tests
interfere with subsequent test queries.
This commit is contained in:
2026-04-01 12:20:10 -07:00
parent 0d9707ea27
commit 09302fd911
+15 -7
View File
@@ -1,7 +1,7 @@
// Setup file for Bun test runner to provide DOM environment
import { JSDOM } from 'jsdom';
import '@testing-library/jest-dom';
import { act } from '@testing-library/react';
console.log('Loading bun-setup.ts...');
const dom = new JSDOM('<!DOCTYPE html><html><body></body></html>', {
url: 'http://localhost',
@@ -12,8 +12,16 @@ const dom = new JSDOM('<!DOCTYPE html><html><body></body></html>', {
(global as any).document = dom.window.document;
(global as any).navigator = dom.window.navigator;
// Extend global act to handle async operations
const originalAct = act;
(global as any).act = async (callback: () => Promise<void> | void) => {
return originalAct(callback);
};
console.log('bun-setup.ts loaded - document:', typeof (global as any).document);
console.log('document.body:', (global as any).document?.body);
// Import jest-dom matchers after setting up the DOM
import '@testing-library/jest-dom';
// Clear document body after each test
import { afterEach } from 'bun:test';
afterEach(() => {
if (global.document?.body) {
global.document.body.innerHTML = '';
}
});