diff options
author | 2023-06-20 18:57:37 -0700 | |
---|---|---|
committer | 2023-06-20 18:57:37 -0700 | |
commit | adb451eec6b8286a4ee18b16b5b87644d5ef3020 (patch) | |
tree | f1bca25150bf1c9ad9b1ac2d5703b71e5953fc51 /docs/test | |
parent | 62639081c1b8ffef0dbf8d729d9d21e10ed41734 (diff) | |
download | bun-adb451eec6b8286a4ee18b16b5b87644d5ef3020.tar.gz bun-adb451eec6b8286a4ee18b16b5b87644d5ef3020.tar.zst bun-adb451eec6b8286a4ee18b16b5b87644d5ef3020.zip |
Docs for DOM testing and FileSink (#3330)
* Update websocket docs & jsdoc
* Add info about user-specific data in ws
* Document FileSink
* Docs for happydom test
* Updates
Diffstat (limited to 'docs/test')
-rw-r--r-- | docs/test/dom.md | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/docs/test/dom.md b/docs/test/dom.md new file mode 100644 index 000000000..3eb897745 --- /dev/null +++ b/docs/test/dom.md @@ -0,0 +1,75 @@ +Bun's test runner plays well with existing component and DOM testing libraries, including React Testing Library and [`happy-dom`](https://github.com/capricorn86/happy-dom). + +## `happy-dom` + +For writing headless tests for your frontend code and components, we recommend [`happy-dom`](https://github.com/capricorn86/happy-dom). Happy DOM implements a complete set of HTML and DOM APIs in plain JavaScript, making it possible to simulate a browser environment with high fidelity. + +To get started install the `@happy-dom/global-registrator` package as a dev dependency. + +```bash +$ bun add -d @happy-dom/global-registrator +``` + +We'll be using Bun's _preload_ functionality to register the `happy-dom` globals before running our tests. This step will make browser APIs like `document` available in the global scope. Create a file called `happydom.ts` in the root of your project and add the following code: + +```ts +import { GlobalRegistrator } from "@happy-dom/global-registrator"; + +GlobalRegistrator.register(); +``` + +To preload this file before `bun test`, open or create a `bunfig.toml` file and add the following lines. + +```toml +[test] +preload = "./happydom.ts" +``` + +This will execute `happydom.ts` when you run `bun test`. Now you can write tests that use browser APIs like `document` and `window`. + +```ts#dom.test.ts +import {test, expect} from 'bun:test'; + +test('dom test', () => { + document.body.innerHTML = `<button>My button</button>`; + const button = document.querySelector('button'); + expect(button?.innerText).toEqual('My button'); +}); +``` + +Depending on your `tsconfig.json` setup, you may see a `"Cannot find name 'document'"` type error in the code above. To "inject" the types for `document` and other browser APIs, add the following [triple-slash directive](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html) to the top of any test file. + +```ts-diff#dom.test.ts ++ /// <reference lib="dom" /> + + import {test, expect} from 'bun:test'; + + test('dom test', () => { + document.body.innerHTML = `<button>My button</button>`; + const button = document.querySelector('button'); + expect(button?.innerText).toEqual('My button'); + }); +``` + +Let's run this test with `bun test`: + +```bash +$ bun test +bun test v0.x.y + +dom.test.ts: +✓ dom test [0.82ms] + + 1 pass + 0 fail + 1 expect() calls +Ran 1 tests across 1 files. 1 total [125.00ms] +``` + +<!-- ## React Testing Library + +Once you've set up `happy-dom` as described above, you can use it with React Testing Library. To get started, install the `@testing-library/react` package as a dev dependency. + +```bash +$ bun add -d @testing-library/react +``` --> |