react-testing-library icon indicating copy to clipboard operation
react-testing-library copied to clipboard

Having `afterEach(cleanup)` in the test setup file breaks exported `screen` module on Bun test

Open hmidmrii opened this issue 1 year ago • 4 comments

  • @testing-library/react version: 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.

hmidmrii avatar Jul 30 '24 09:07 hmidmrii

This is most likely caused by https://github.com/oven-sh/bun/issues/4205

Jarred-Sumner avatar Jul 31 '24 06:07 Jarred-Sumner

@Jarred-Sumner so this is blocking the use of happy-dom? I think jsdom uses the same module so we need a fix from Bun team

hmidmrii avatar Aug 01 '24 06:08 hmidmrii

I found a workaround by splitting the matcher extension and the cleanup logic into two separate setup files and preloading both.

pkfln avatar Mar 19 '25 23:03 pkfln

I found a workaround by splitting the matcher extension and the cleanup logic into two separate setup files and preloading both.

@pkfln ... thanks. That worked. The test guide is misleading, it says you can also have everything in a single preload.ts script if you prefer. I had both in a test-setup.ts file.

kcsujeet avatar Jul 12 '25 17:07 kcsujeet