|
1 | 1 | # bun-test-env-dom |
2 | | -bun-test-env-dom is a preload library that provides a ready-to-use DOM |
| 2 | + |
| 3 | +A preload library for Bun that provides a ready-to-use DOM environment for testing. It automatically sets up [happy-dom](https://github.com/capricorn86/happy-dom) and enables proper snapshot testing for React and HTML elements with beautifully formatted output. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +bun add -d bun-test-env-dom |
| 9 | +``` |
| 10 | + |
| 11 | +## Setup |
| 12 | + |
| 13 | +Add the following to your `bunfig.toml`: |
| 14 | + |
| 15 | +```toml |
| 16 | +[test] |
| 17 | +preload = ["bun-test-env-dom"] |
| 18 | +``` |
| 19 | + |
| 20 | +That's it! The DOM environment is automatically configured when tests run. |
| 21 | + |
| 22 | +## Features |
| 23 | + |
| 24 | +### Snapshot Testing for React & HTML Elements |
| 25 | + |
| 26 | +Snapshot testing works seamlessly with both React elements and HTML elements. The HTML output is automatically formatted for readable snapshots. |
| 27 | + |
| 28 | +```tsx |
| 29 | +import { expect, test } from 'bun:test' |
| 30 | +import { render } from 'bun-test-env-dom' |
| 31 | + |
| 32 | +test('React element snapshot', () => { |
| 33 | + expect(<Component />).toMatchSnapshot() |
| 34 | +}) |
| 35 | + |
| 36 | +test('HTML element snapshot', () => { |
| 37 | + const { container } = render(<Component />) |
| 38 | + expect(container).toMatchSnapshot() |
| 39 | +}) |
| 40 | +``` |
| 41 | + |
| 42 | +### Re-exported @testing-library/react |
| 43 | + |
| 44 | +All functions from `@testing-library/react` are re-exported, so you can import directly from `bun-test-env-dom`: |
| 45 | + |
| 46 | +```tsx |
| 47 | +import { render, screen, fireEvent } from 'bun-test-env-dom' |
| 48 | + |
| 49 | +describe('HomePage', () => { |
| 50 | + it('should render', () => { |
| 51 | + const { container } = render(<HomePage />) |
| 52 | + expect(container).toMatchSnapshot() |
| 53 | + }) |
| 54 | +}) |
| 55 | +``` |
| 56 | + |
| 57 | +### Full TypeScript Support for Custom Matchers |
| 58 | + |
| 59 | +All additional matchers from `@testing-library/jest-dom` are fully typed: |
| 60 | + |
| 61 | +```tsx |
| 62 | +import { expect, test } from 'bun:test' |
| 63 | +import { render } from 'bun-test-env-dom' |
| 64 | + |
| 65 | +test('custom matchers', () => { |
| 66 | + const { getByRole } = render(<Button pressed>Click me</Button>) |
| 67 | + |
| 68 | + expect(getByRole('button')).toBePressed() |
| 69 | + expect(getByRole('button')).toBeVisible() |
| 70 | + expect(getByRole('button')).toHaveTextContent('Click me') |
| 71 | +}) |
| 72 | +``` |
| 73 | + |
| 74 | +## License |
| 75 | + |
| 76 | +Apache-2.0 |
0 commit comments