Skip to content

Commit d46d6f4

Browse files
committed
docs: update browser testing recipe
1 parent 52b2270 commit d46d6f4

File tree

1 file changed

+24
-65
lines changed

1 file changed

+24
-65
lines changed

docs/recipes/browser-testing.md

Lines changed: 24 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,41 @@
11
# Setting up AVA for browser testing
22

3-
Translations: [Español](https://github.com/avajs/ava-docs/blob/main/es_ES/docs/recipes/browser-testing.md), [Français](https://github.com/avajs/ava-docs/blob/main/fr_FR/docs/recipes/browser-testing.md), [Italiano](https://github.com/avajs/ava-docs/blob/main/it_IT/docs/recipes/browser-testing.md), [Русский](https://github.com/avajs/ava-docs/blob/main/ru_RU/docs/recipes/browser-testing.md), [简体中文](https://github.com/avajs/ava-docs/blob/main/zh_CN/docs/recipes/browser-testing.md)
3+
AVA is running in a __Node.js__ environment. JavaScript that runs in a browser are likely to expect the browser DOM globals to be in place.
4+
With help from a package called [jsdom](https://github.com/jsdom/jsdom),
5+
you can write unit tests with `ava` for JavaScript that is expecting browser specific globals such as `window`, `document` and `navigator`.
46

5-
AVA does not support running tests in browsers [yet](https://github.com/avajs/ava/issues/24). However JavaScript libraries that require browser specific globals (`window`, `document`, `navigator`, etc) can still be tested with AVA by mocking these globals.
7+
## Install jsdom
68

7-
This recipe works for any library that needs a mocked browser environment.
8-
9-
## Install browser-env
10-
11-
> **❗️ Important note**
12-
>
13-
>`browser-env` adds properties from the `jsdom` window namespace to the Node.js global namespace. This is explicitly [recommended against](https://github.com/tmpvar/jsdom/wiki/Don't-stuff-jsdom-globals-onto-the-Node-global) by `jsdom`. Please read through the linked wiki page and make sure you understand the caveats. If you don't have lots of dependencies that also require a browser environment then [`window`](https://github.com/lukechilds/window#universal-testing-pattern) may be a better solution.
14-
15-
Install [browser-env](https://github.com/lukechilds/browser-env).
16-
17-
> Simulates a global browser environment using jsdom.
18-
19-
```
20-
$ npm install --save-dev browser-env
9+
```bash
10+
npm install --save-dev jsdom
2111
```
2212

23-
## Setup browser-env
24-
25-
Create a helper file, prefixed with an underscore. This ensures AVA does not treat it as a test.
13+
## Writing unit tests
2614

27-
`test/_setup-browser-env.js`:
15+
Use `jsdom` to set the globals and DOM elements that the test target is expecting.
2816

29-
```js
30-
import browserEnv from 'browser-env';
31-
browserEnv();
32-
```
3317

34-
By default, `browser-env` will add all global browser variables to the Node.js global scope, creating a full browser environment. This should have good compatibility with most front-end libraries, however, it's generally not a good idea to create lots of global variables if you don't need to. If you know exactly which browser globals you need, you can pass an array of them.
18+
### An example Unit Test
19+
The JavaScript code is doing a DOM query in a function, such as: `document.querySelector('#my-element-id')`.
3520

36-
```js
37-
import browserEnv from 'browser-env';
38-
browserEnv(['window', 'document', 'navigator']);
39-
```
40-
41-
You can expose more global variables by assigning them to the `global` object. For instance, jQuery is typically available through the `$` variable:
42-
43-
```js
44-
import browserEnv from 'browser-env';
45-
import jQuery from 'jquery';
46-
47-
browserEnv();
48-
global.$ = jQuery(window);
49-
```
50-
51-
## Configure tests to use browser-env
52-
53-
Configure AVA to `require` the helper before every test file.
54-
55-
**`package.json`:**
56-
57-
```json
58-
{
59-
"ava": {
60-
"require": [
61-
"./test/_setup-browser-env.js"
62-
]
63-
}
64-
}
65-
```
66-
67-
## Enjoy!
68-
69-
Write your tests and enjoy a mocked browser environment.
70-
71-
`test.js`:
21+
To make the code testable with `ava` running in __Node.js__, you can add the element to `jsdom`.
7222

7323
```js
7424
import test from 'ava';
25+
import { JSDOM } from 'jsdom';
7526

76-
test('Insert to DOM', t => {
77-
const div = document.createElement('div');
78-
document.body.appendChild(div);
27+
test.before(() => {
28+
const dom = new JSDOM('<div id="my-element-id" />'); // insert any html needed for the unit test suite here
29+
global.document = dom.window.document; // add the globals needed for the unit tests in this suite.
30+
});
31+
32+
test('this is an example', (t) => {
33+
const res = myTarget.runFunctionThatExpectsTheDocumentGlobalAndElement();
7934

80-
t.is(document.querySelector('div'), div);
35+
test.truthy(res);
8136
});
8237
```
38+
39+
## Important note
40+
In general, adding globals to the `Node.js` environment is [recommended against](https://github.com/jsdom/jsdom/wiki/Don't-stuff-jsdom-globals-onto-the-Node-global) by `jsdom`.
41+
Please read through the linked wiki page and make sure you understand why.

0 commit comments

Comments
 (0)