{"id":15598822,"url":"https://github.com/lukechilds/window","last_synced_at":"2025-04-05T10:08:48.718Z","repository":{"id":44178326,"uuid":"83620215","full_name":"lukechilds/window","owner":"lukechilds","description":"Exports a jsdom window object.","archived":false,"fork":false,"pushed_at":"2023-01-03T15:14:43.000Z","size":1188,"stargazers_count":77,"open_issues_count":23,"forks_count":9,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-29T09:09:34.678Z","etag":null,"topics":["jsdom","testing","window-namespace"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lukechilds.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-02T01:32:14.000Z","updated_at":"2024-12-16T01:54:56.000Z","dependencies_parsed_at":"2023-02-01T06:45:54.478Z","dependency_job_id":null,"html_url":"https://github.com/lukechilds/window","commit_stats":null,"previous_names":[],"tags_count":26,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukechilds%2Fwindow","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukechilds%2Fwindow/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukechilds%2Fwindow/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukechilds%2Fwindow/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lukechilds","download_url":"https://codeload.github.com/lukechilds/window/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247318744,"owners_count":20919484,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["jsdom","testing","window-namespace"],"created_at":"2024-10-03T01:41:26.220Z","updated_at":"2025-04-05T10:08:48.701Z","avatar_url":"https://github.com/lukechilds.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# window\n\n\u003e Exports a [`jsdom`](https://github.com/tmpvar/jsdom) window object.\n\n[![Build Status](https://travis-ci.org/lukechilds/window.svg?branch=master)](https://travis-ci.org/lukechilds/window)\n[![Coverage Status](https://coveralls.io/repos/github/lukechilds/window/badge.svg?branch=master)](https://coveralls.io/github/lukechilds/window?branch=master)\n[![npm](https://img.shields.io/npm/dm/window.svg)](https://www.npmjs.com/package/window)\n[![npm](https://img.shields.io/npm/v/window.svg)](https://www.npmjs.com/package/window)\n\nExports 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.\n\nRequires Node.js v6 or newer, use `window@3` to support older Node.js versions.\n\n## Install\n\n```shell\nnpm install --save window\n```\n\nOr if you're just using for testing you'll probably want:\n\n```shell\nnpm install --save-dev window\n```\n\n## Usage\n\n```js\nconst Window = require('window');\n\nconst window = new Window();\n\nconst div = window.document.createElement('div');\n// HTMLDivElement\n\ndiv instanceof window.HTMLElement\n// true\n```\n\nBecause `window` is just a normal JavaScript object it can be used more efficiently with object destructuring.\n\n```js\nconst { document } = new Window();\n\ndocument.body.innerHTML = '\u003cdiv class=\"foo\"\u003eHi!\u003c/div\u003e';\ndocument.body.querySelector('.foo').textContent;\n// \"Hi!\"\n```\n\n### Config\n\nYou can also pass a jsdom config object that will be passed along to the underlying jsdom instance.\n\n```js\nconst jsdomConfig = { userAgent: 'Custom UA' };\nconst window = new Window(jsdomConfig);\n\nwindow.navigator.userAgent;\n// \"Custom UA\"\n```\n\n## Universal Testing Pattern\n\nYou 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`.\n\n```js\nfunction createTitle(text, win) {\n  win = win || (typeof window === 'undefined' ? undefined : window);\n\n  const title = win.document.createElement('h1');\n  title.innerHTML = text;\n  return title;\n};\n\nmodule.exports = createTitle;\n```\n\nBrowser usage:\n\n```js\ncreateTitle('Hi');\n// \u003ch1\u003eHi\u003c/h1\u003e\n```\n\nNode.js usage:\n\n```js\nconst window = new Window();\n\ncreateTitle('Hi', window);\n// \u003ch1\u003eHi\u003c/h1\u003e\n```\n\nObviously 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:\n\n```js\nfunction createTitle(text, opts = {}) {\n  const doc = opts.document || window.document;\n\n  const title = doc.createElement('h1');\n  ...\n```\n\nYou 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).\n\n## What about dependencies?\n\nSometimes 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.\n\n## License\n\nMIT © Luke Childs\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flukechilds%2Fwindow","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flukechilds%2Fwindow","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flukechilds%2Fwindow/lists"}