Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Exports a jsdom window object.

License

NotificationsYou must be signed in to change notification settings

lukechilds/window

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

88 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Exports ajsdom window object.

Build StatusCoverage Statusnpmnpm

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, usewindow@3 to support older Node.js versions.

Install

npm install --save window

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

npm install --save-dev window

Usage

constWindow=require('window');constwindow=newWindow();constdiv=window.document.createElement('div');// HTMLDivElementdivinstanceofwindow.HTMLElement// true

Becausewindow is just a normal JavaScript object it can be used more efficiently with object destructuring.

const{ document}=newWindow();document.body.innerHTML='<div>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.

constjsdomConfig={userAgent:'Custom UA'};constwindow=newWindow(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 withwin. Setwin to the passed in window object if it exists, otherwise fallback to globalwindow.

functioncreateTitle(text,win){win=win||(typeofwindow==='undefined' ?undefined :window);consttitle=win.document.createElement('h1');title.innerHTML=text;returntitle;};module.exports=createTitle;

Browser usage:

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

Node.js usage:

constwindow=newWindow();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 needdocument not the entire window object:

functioncreateTitle(text,opts={}){constdoc=opts.document||window.document;consttitle=doc.createElement('h1');  ...

You can see an example of this pattern inlukechilds/create-node. Specificallysrc/create-node.js andtest/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 usebrowser-env which will simulate a global browser environment.

License

MIT © Luke Childs

About

Exports a jsdom window object.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp