global-jsdom 9.0.0 → 9.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +149 -0
- package/package.json +5 -5
- package/types/index.d.ts +2 -0
package/README.md
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# global-jsdom
|
|
2
|
+
[](https://www.npmjs.com/package/global-jsdom)
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
> Enables DOM in Node.js
|
|
6
|
+
`global-jsdom` will inject `document`, `window` and other DOM API into your Node.js environment. This allows you to run browser tests in Node.js. The specific attributes set on `global` come directly from the `jsdom` version you have installed.
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
Requires [node][] >= 16 and [jsdom][] >= 22 or above
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
npm install --save-dev --save-exact jsdom global-jsdom
|
|
13
|
+
```
|
|
14
|
+
[node]: https://github.com/nodejs/node
|
|
15
|
+
[jsdom]: https://github.com/tmpvar/jsdom
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
Just invoke it to turn your Node.js environment into a DOM environment.
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
// commonjs
|
|
23
|
+
require('global-jsdom/register')
|
|
24
|
+
|
|
25
|
+
// or es2015
|
|
26
|
+
import 'global-jsdom/register'
|
|
27
|
+
|
|
28
|
+
// you can now use the DOM
|
|
29
|
+
document.body.innerHTML = 'hello'
|
|
30
|
+
|
|
31
|
+
// you can also access the current jsdom instance through $jsdom
|
|
32
|
+
global.$jsdom.reconfigure({})
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Configuration
|
|
36
|
+
You may pass configuration parameters to `jsdom` like so:
|
|
37
|
+
```js
|
|
38
|
+
// commonjs
|
|
39
|
+
const globalJsdom = require('global-jsdom')
|
|
40
|
+
|
|
41
|
+
// or es2015
|
|
42
|
+
import globalJsdom from 'global-jsdom'
|
|
43
|
+
|
|
44
|
+
// then
|
|
45
|
+
globalJsdom(html, options)
|
|
46
|
+
```
|
|
47
|
+
Check the [jsdom.jsdom()][] documentation for valid values for the `options`
|
|
48
|
+
parameter.
|
|
49
|
+
|
|
50
|
+
### Default Options
|
|
51
|
+
The following set of default options are passed to `jsdom`
|
|
52
|
+
```js
|
|
53
|
+
{
|
|
54
|
+
// if url isn't set then localStorage breaks with a cryptic error, see
|
|
55
|
+
// https://github.com/jsdom/jsdom/issues/2304#issuecomment-408320484
|
|
56
|
+
url: 'http://localhost:3000',
|
|
57
|
+
// pretendToBeVisual is enabled so that react works, see
|
|
58
|
+
// https://github.com/jsdom/jsdom#pretending-to-be-a-visual-browser
|
|
59
|
+
pretendToBeVisual: true,
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
### Cleanup
|
|
63
|
+
To clean up the global namespace just invoke the returned function:
|
|
64
|
+
```js
|
|
65
|
+
// commonjs
|
|
66
|
+
const cleanup = require('global-jsdom')()
|
|
67
|
+
|
|
68
|
+
// es2015
|
|
69
|
+
import globalJsdom from 'global-jsdom'
|
|
70
|
+
const cleanup = globalJsdom()
|
|
71
|
+
|
|
72
|
+
// do things, then
|
|
73
|
+
cleanup()
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Tape
|
|
77
|
+
|
|
78
|
+
In [tape][], run it before your other tests.
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
require('global-jsdom/register')
|
|
82
|
+
|
|
83
|
+
test('your tests', (t) => {
|
|
84
|
+
/* and so on... */
|
|
85
|
+
})
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Mocha
|
|
89
|
+
|
|
90
|
+
__Simple:__ Use Mocha's `--require` option. Add this to the `test/mocha.opts` file (create it if it doesn't exist)
|
|
91
|
+
|
|
92
|
+
```
|
|
93
|
+
-r global-jsdom/register
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
__Advanced:__ For finer control, you can instead add it via [mocha]'s `before` and `after` hooks.
|
|
97
|
+
|
|
98
|
+
```js
|
|
99
|
+
before(function () {
|
|
100
|
+
this.jsdom = require('global-jsdom')()
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
after(function () {
|
|
104
|
+
this.jsdom()
|
|
105
|
+
})
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
[tape]: https://github.com/substack/tape
|
|
109
|
+
[mocha]: https://mochajs.org/
|
|
110
|
+
[jsdom.jsdom()]: https://github.com/tmpvar/jsdom/#for-the-hardcore-jsdomjsdom
|
|
111
|
+
|
|
112
|
+
## ES2015
|
|
113
|
+
|
|
114
|
+
If you're using a [recent version of
|
|
115
|
+
node](https://nodejs.org/api/esm.html#esm_conditional_exports) then `import` should
|
|
116
|
+
just work:
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
```js
|
|
120
|
+
import 'global-jsdom/register'
|
|
121
|
+
import React from 'react'
|
|
122
|
+
import jQuery from 'jquery'
|
|
123
|
+
// ...
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## Typescript
|
|
127
|
+
|
|
128
|
+
The library includes automatic support providing the necessary type declarations for an integration without further configuration.
|
|
129
|
+
|
|
130
|
+
```ts
|
|
131
|
+
import globalJsdom from "global-jsdom";
|
|
132
|
+
|
|
133
|
+
describe("Typescript test example", () => {
|
|
134
|
+
let cleanup: { (): void };
|
|
135
|
+
|
|
136
|
+
before(() => {
|
|
137
|
+
cleanup = globalJsdom();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
after(() => {
|
|
141
|
+
cleanup();
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
})
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Thanks
|
|
148
|
+
|
|
149
|
+
original code forked from [jsdom-global](https://github.com/rstacruz/jsdom-global)
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "global-jsdom",
|
|
3
3
|
"description": "Enable DOM in Node.js",
|
|
4
|
-
"version": "9.
|
|
4
|
+
"version": "9.1.0",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "jonathan schatz",
|
|
7
7
|
"email": "jon@divisionbyzero.com",
|
|
@@ -34,11 +34,11 @@
|
|
|
34
34
|
],
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"chai": "^4.3.7",
|
|
37
|
-
"eslint": "^8.
|
|
37
|
+
"eslint": "^8.46.0",
|
|
38
38
|
"eslint-config-airbnb-base": "^15.0.0",
|
|
39
|
-
"eslint-plugin-import": "^2.
|
|
39
|
+
"eslint-plugin-import": "^2.28.0",
|
|
40
40
|
"eslint-plugin-mocha": "^10.1.0",
|
|
41
|
-
"jsdom": "^22.
|
|
41
|
+
"jsdom": "^22.1.0",
|
|
42
42
|
"mocha": "^10.2.0"
|
|
43
43
|
},
|
|
44
44
|
"homepage": "https://github.com/modosc/global-jsdom#readme",
|
|
@@ -65,4 +65,4 @@
|
|
|
65
65
|
"lint": "yarn eslint --ext=cjs,mjs --cache .eslintrc.cjs commonjs/ esm/ test/",
|
|
66
66
|
"lint:fix": "yarn eslint --ext=cjs,mjs --cache --fix .eslintrc.cjs commonjs/ esm/ test/"
|
|
67
67
|
}
|
|
68
|
-
}
|
|
68
|
+
}
|