UNPKG

2.94 kBMarkdownView Raw
1# browser-env
2
3> Simulates a global browser environment using [`jsdom`](https://github.com/tmpvar/jsdom).
4
5[![Build Status](https://travis-ci.org/lukechilds/browser-env.svg?branch=master)](https://travis-ci.org/lukechilds/browser-env)
6[![Coverage Status](https://coveralls.io/repos/github/lukechilds/browser-env/badge.svg?branch=master)](https://coveralls.io/github/lukechilds/browser-env?branch=master)
7[![npm](https://img.shields.io/npm/dm/browser-env.svg)](https://www.npmjs.com/package/browser-env)
8[![npm](https://img.shields.io/npm/v/browser-env.svg)](https://www.npmjs.com/package/browser-env)
9
10This allows you to run browser modules in Node.js 4 or newer with minimal or no effort. Can also be used to test browser modules with any Node.js test framework. Please note, only the DOM is simulated, if you want to run a module that requires more advanced browser features (like `localStorage`), you'll need to polyfill that seperately.
11
12Requires Node.js v6 or newer, use `browser-env@2` to support older Node.js versions.
13
14> ❗️**Important note**
15>
16> This module 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`. There may be scenarios where this is ok for your use case but please read through the linked wiki page and make sure you understand the caveats. If you don't need the browser environment enabled globally, [`window`](https://github.com/lukechilds/window) may be a better solution.
17
18## Install
19
20```shell
21npm install --save browser-env
22```
23
24Or if you're just using for testing you'll probably want:
25
26```shell
27npm install --save-dev browser-env
28```
29
30## Usage
31
32```js
33// Init
34require('browser-env')();
35
36// Now you have access to a browser like environment in Node.js:
37
38typeof window;
39// 'object'
40
41typeof document;
42// 'object'
43
44var div = document.createElement('div');
45// HTMLDivElement
46
47div instanceof HTMLElement
48// true
49```
50
51By default everything in the `jsdom` window namespace is tacked on to the Node.js global namespace (excluding existing Node.js properties e.g `console`, `setTimout`). If you want to trim this down you can pass an array of required properties:
52
53```js
54// Init
55require('browser-env')(['window']);
56
57typeof window;
58// 'object'
59
60typeof document;
61// 'undefined'
62```
63
64You can also pass a config object straight through to `jsdom`. This can be done with or without specifying required properties.
65
66```js
67require('browser-env')(['window'], { userAgent: 'My User Agent' });
68
69// or
70
71require('browser-env')({ userAgent: 'My User Agent' });
72```
73
74You can of course also assign to a function:
75
76```js
77var browserEnv = require('browser-env');
78browserEnv();
79
80// or
81
82import browserEnv from 'browser-env';
83browserEnv();
84```
85
86## Related
87
88- [`window`](https://github.com/lukechilds/window) - Exports a jsdom window object
89
90## License
91
92MIT © Luke Childs