Coder Social home page Coder Social logo

window's Introduction

window

Exports a jsdom window object.

Build Status Coverage Status npm npm

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

npm install --save window

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

npm install --save-dev window

Usage

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.

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.

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.

function createTitle(text, win) {
  win = win || window;

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

module.exports = createTitle;

Browser usage:

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

Node.js usage:

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:

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 and 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 which will simulate a global browser environment.

License

MIT © Luke Childs

window's People

Contributors

lukechilds avatar greenkeeper[bot] avatar tehshrike avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.