# window

> Exports a jsdom window object.

Latest version **4.2.7** (published 2020-03-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install window
pnpm add window
yarn add window
bun add window
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.2.7 |
| Published | 2020-03-20 |
| First published | 2015-06-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=4 |
| Dependencies | 1 |
| Unpacked size | 12.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 78 |
| Author | Luke Childs |
| Maintainers | lukechilds |
| Keywords | export, jsdom, window, object, simulate, node, browser, environment, env |

## Links

- npm: https://www.npmjs.com/package/window
- Repository: https://github.com/lukechilds/window
- Homepage: https://github.com/lukechilds/window#readme
- Issues: https://github.com/lukechilds/window/issues
- npm.io page: https://npm.io/package/window

## Dependencies (1)

- [jsdom](https://npm.io/package/jsdom.md) 13.2.0

## Alternatives

- [replicas-cli](https://npm.io/package/replicas-cli.md) — 3.0K weekly downloads
- [env-contract](https://npm.io/package/env-contract.md) — 133 weekly downloads
- [@openveo/api](https://npm.io/package/@openveo/api.md) — 61 weekly downloads
- [@ryniaubenpm2/cumque-error-reiciendis](https://npm.io/package/@ryniaubenpm2/cumque-error-reiciendis.md) — 54 weekly downloads
- [ts-global-type-extra](https://npm.io/package/ts-global-type-extra.md) — 11 weekly downloads

## Recent versions

- 4.2.7 (latest) — 2020-03-20
- 4.2.6 — 2019-02-04
- 4.2.5 — 2018-02-06
- 4.2.4 — 2017-11-27
- 4.2.3 — 2017-11-23
- 4.2.2 — 2017-11-01
- 4.2.1 — 2017-10-01
- 4.2.0 — 2017-08-23
- 4.1.2 — 2017-08-10
- 4.1.1 — 2017-08-10
- 4.1.0 — 2017-07-05
- 4.0.1 — 2017-06-30
- 4.0.0 — 2017-06-30
- 3.1.6 — 2017-04-10
- 3.1.5 — 2017-04-10
- … 10 more at https://npm.io/package/window/versions

## README

# window

> Exports a [`jsdom`](https://github.com/tmpvar/jsdom) window object.

[![Build Status](https://travis-ci.org/lukechilds/window.svg?branch=master)](https://travis-ci.org/lukechilds/window)
[![Coverage Status](https://coveralls.io/repos/github/lukechilds/window/badge.svg?branch=master)](https://coveralls.io/github/lukechilds/window?branch=master)
[![npm](https://img.shields.io/npm/dm/window.svg)](https://www.npmjs.com/package/window)
[![npm](https://img.shields.io/npm/v/window.svg)](https://www.npmjs.com/package/window)

Exports a jsdom window object. This is useful for enabling browser modules to run in Node.js or testing browser modules in any Node.js test framework.

Requires Node.js v6 or newer, use `window@3` to support older Node.js versions.

## Install

```shell
npm install --save window
```

Or if you're just using for testing you'll probably want:

```shell
npm install --save-dev window
```

## Usage

```js
const Window = require('window');

const window = new Window();

const div = window.document.createElement('div');
// HTMLDivElement

div instanceof window.HTMLElement
// true
```

Because `window` is just a normal JavaScript object it can be used more efficiently with object destructuring.

```js
const { document } = new Window();

document.body.innerHTML = '<div class="foo">Hi!</div>';
document.body.querySelector('.foo').textContent;
// "Hi!"
```

### Config

You can also pass a jsdom config object that will be passed along to the underlying jsdom instance.

```js
const jsdomConfig = { userAgent: 'Custom UA' };
const window = new Window(jsdomConfig);

window.navigator.userAgent;
// "Custom UA"
```

## Universal Testing Pattern

You can use a really simple pattern to enable your browser modules to run in Node.js. Just allow a window object to be passed in to your module and prepend any references to browser globals with `win`. Set `win` to the passed in window object if it exists, otherwise fallback to global `window`.

```js
function createTitle(text, win) {
  win = win || (typeof window === 'undefined' ? undefined : window);

  const title = win.document.createElement('h1');
  title.innerHTML = text;
  return title;
};

module.exports = createTitle;
```

Browser usage:

```js
createTitle('Hi');
// <h1>Hi</h1>
```

Node.js usage:

```js
const window = new Window();

createTitle('Hi', window);
// <h1>Hi</h1>
```

Obviously you don't need to follow this exact pattern, maybe you already have an options object and you only need `document` not the entire window object:

```js
function createTitle(text, opts = {}) {
  const doc = opts.document || window.document;

  const title = doc.createElement('h1');
  ...
```

You can see an example of this pattern in `lukechilds/create-node`. Specifically [src/create-node.js](https://github.com/lukechilds/create-node/blob/master/src/create-node.js) and  [test/unit.js](https://github.com/lukechilds/create-node/blob/master/test/unit.js).

## What about dependencies?

Sometimes you may have dependencies that you can't pass a window object to. In that scenario you can alternatively use [`browser-env`](https://github.com/lukechilds/browser-env) which will simulate a global browser environment.

## License

MIT © Luke Childs

---
_Source: https://npm.io/package/window · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
