1 | # jsdom-global
|
2 |
|
3 | > Enables DOM in Node.js
|
4 |
|
5 | jsdom-global will inject `document`, `window` and other DOM API into your Node.js environment. Useful for running, in Node.js, tests that are made for browsers.
|
6 |
|
7 | [![Status](https://travis-ci.org/rstacruz/jsdom-global.svg?branch=master)](https://travis-ci.org/rstacruz/jsdom-global "See test builds")
|
8 |
|
9 | ## Install
|
10 |
|
11 | Requires [jsdom][].
|
12 |
|
13 | ```
|
14 | npm install --save-dev --save-exact jsdom jsdom-global
|
15 | ```
|
16 |
|
17 | [jsdom]: https://github.com/tmpvar/jsdom
|
18 |
|
19 | ## Usage
|
20 |
|
21 | Just invoke it to turn your Node.js environment into a DOM environment.
|
22 |
|
23 | ```js
|
24 | require('jsdom-global')()
|
25 |
|
26 | // you can now use the DOM
|
27 | document.body.innerHTML = 'hello'
|
28 | ```
|
29 |
|
30 | To clean up after itself, just invoke the function it returns.
|
31 |
|
32 | ```js
|
33 | var cleanup = require('jsdom-global')()
|
34 |
|
35 | // do things
|
36 |
|
37 | cleanup()
|
38 | ```
|
39 |
|
40 | You can also invoke it with a function block so it'll clean up afterwards.
|
41 |
|
42 | ```js
|
43 | var jsdom = require('jsdom-global')
|
44 |
|
45 | jsdom(function () {
|
46 | var $ = require('jquery')
|
47 | $('body').html('hello')
|
48 | })
|
49 | ```
|
50 |
|
51 | ## Tape
|
52 |
|
53 | In [tape][], run it before your other tests.
|
54 |
|
55 | ```js
|
56 | require('jsdom-global')()
|
57 |
|
58 | test('your tests', (t) => {
|
59 | /* and so on... */
|
60 | })
|
61 | ```
|
62 |
|
63 | ## Mocha
|
64 |
|
65 | Just add it to [mocha]'s `before` and `after` hooks.
|
66 |
|
67 | ```js
|
68 | before(function () {
|
69 | this.jsdom = require('jsdom-global')()
|
70 | })
|
71 |
|
72 | after(function () {
|
73 | this.jsdom()
|
74 | })
|
75 | ```
|
76 |
|
77 | [tape]: https://github.com/substack/tape
|
78 | [mocha]: https://mochajs.org/
|
79 |
|
80 | ## Browserify
|
81 |
|
82 | If you use [Browserify] on your tests (eg: [smokestack], [tape-run], [budo], [hihat], [zuul], and so on), doing `require('jsdom-global')()` is a noop. In practice, this means you can use jsdom-global even if your tests are powered by browserify, and your test will now work in both the browser and Node.
|
83 |
|
84 | [zuul]: https://www.npmjs.com/package/zuul
|
85 | [tape-run]: https://www.npmjs.com/package/tape-run
|
86 | [budo]: https://github.com/mattdesl/budo
|
87 | [hihat]: https://www.npmjs.com/package/hihat
|
88 | [smokestack]: https://www.npmjs.com/package/smokestack
|
89 |
|
90 | * Writing your tests (`test.js`):
|
91 |
|
92 | ```js
|
93 | require('jsdom-global')()
|
94 |
|
95 | // ...do your tests here
|
96 | ```
|
97 |
|
98 | * Running it with [smokestack]:
|
99 |
|
100 | ```sh
|
101 | browserify test.js | smokestack # run in a browser
|
102 | node test.js # or the console
|
103 | browserify test.js --no-bundle-external # also works (but why bother?)
|
104 | ```
|
105 |
|
106 | * Running it with Babel ([babelify] or [babel-cli]):
|
107 |
|
108 | ```sh
|
109 | browserify test.js -t babelify | smokestack # run in a browser (with babel)
|
110 | babel-node test.js # or the console
|
111 | ```
|
112 |
|
113 | [Browserify]: http://browserify.org/
|
114 | [babel-cli]: https://babeljs.io/docs/usage/cli/
|
115 | [babelify]: https://github.com/babel/babelify
|
116 |
|
117 | ## Thanks
|
118 |
|
119 | **jsdom-global** © 2016+, Rico Sta. Cruz. Released under the [MIT] License.<br>
|
120 | Authored and maintained by Rico Sta. Cruz with help from contributors ([list][contributors]).
|
121 |
|
122 | > [ricostacruz.com](http://ricostacruz.com) ·
|
123 | > GitHub [@rstacruz](https://github.com/rstacruz) ·
|
124 | > Twitter [@rstacruz](https://twitter.com/rstacruz)
|
125 |
|
126 | [MIT]: http://mit-license.org/
|
127 | [contributors]: http://github.com/rstacruz/jsdom-global/contributors
|
128 |
|
\ | No newline at end of file |