oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
72.05k stars 2.58k forks source link

V1: `bun test` throws `ReferenceError: Can't find variable: document` when using `@testing-library/svelte` #5113

Open MathiasWP opened 10 months ago

MathiasWP commented 10 months ago

What version of Bun is running?

1.0.0+822a00c4d508b54f650933a73ca5f4a3af9a7983

What platform is your computer?

Darwin 22.6.0 arm64 arm

What steps can reproduce the bug?

I've created a minimal repro here: https://github.com/MathiasWP/bun-testing-library-bug-repro/blob/main/test/Component.test.ts

Copied over code of test that should pass (but crashes):

import { render } from '@testing-library/svelte';
import Component from './Component.svelte';

describe('bun test', () => {
    it('should be able to render this component with @testing-library/svelte', () => {
        const {getByText} = render(Component);
        const div = getByText('Hello world!')
        expect(div).toBeDefined()
    });
});

What is the expected behavior?

The test should pass

What do you see instead?

Screenshot 2023-09-12 at 17 07 36

Additional information

I really hope this can be fixed, bun test is amazing. First time i've tried a testing library for JS/TS that is actually blazingly fast. It decreases our test time from ~10 minutes to seconds.

glenngijsberts commented 10 months ago

I think similar to https://github.com/oven-sh/bun/issues/198. Had the same problems with testing-library/react-hooks & testing-library/react.

glenngijsberts commented 10 months ago

This might help you out: https://bun.sh/docs/test/dom.

peacefulotter commented 6 months ago

Following the guide you mention, to me it appears the bunfig.toml file is not taken into account or the happydom.ts is not preloaded properly. It only works when I put the content of happydom.ts at the top of my test file. Are the docs up to date?

Rishab1207 commented 4 months ago

This might help you out: https://bun.sh/docs/test/dom.

Thanks! This resolved my issue

krel99 commented 4 months ago

Following the guide you mention, to me it appears the bunfig.toml file is not taken into account or the happydom.ts is not preloaded properly. It only works when I put the content of happydom.ts at the top of my test file. Are the docs up to date?

I don't know about Jan 12, but it is working now. I moved happydom.ts to tests folder and configured toml like this 'preload = "./tests/preloadDom.js"' but it did work from the root also.

DipakBiztech commented 3 months ago

I am still facing the same issue. I am try to setup react app using bun and when run test it's throw error.

image

aralroca commented 2 months ago

Probably you need to add happy-dom: https://github.com/oven-sh/bun/issues/198#issuecomment-1555565193

reza18s commented 5 days ago

bun test v1.1.15 (b23ba1fe)

tests/Home.test.tsx:

Unhandled error between tests


5 | import 'aria-query'; 6 | import 'chalk'; 7 | import 'lodash/isEqualWith.js'; 8 | import 'css.escape'; 9 | 10 | expect.extend(extensions); ^ ReferenceError: Can't find variable: expect at /home/reza/Desktop/next.JS/Board/node_modules/@testing-library/jest-dom/dist/index.mjs:10:1

0 pass 1 fail 1 error Ran 1 tests across 1 files. [191.00ms]

Screenshot from 2024-07-11 15-25-31 yarn test yarn run v1.22.22 $ jest PASS tests/Home.test.tsx Page ✓ renders a heading (60 ms)

Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 0 total Time: 0.701 s, estimated 1 s Ran all test suites. Done in 2.50s. ![Uploading Screenshot from 2024-07-11 15-26-29.png…]()

reza18s commented 5 days ago

image

reza18s commented 5 days ago

image