npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsdom-context-require

v5.2.4

Published

Allows you to require files in a jsdom window context.

Downloads

745

Readme

Creates a new require function which runs all required modules in a new jsdom window context instead of global. Supports custom require extensions and resolvers. Also automatically resolves browser fields in package.json.

Ultimately this allows you to run tests with JSDOM without exposing your nodejs globals, or using a bundler.

Installation

npm install jsdom jsdom-context-require -D

Note: JSDOM is required as a peerDependency as of jsdom-context-require@4

Example

./index.js

import { createBrowser } from "jsdom-context-require";

const browser = createBrowser({
  dir: __dirname, // The path to resolve new requires from.
  html: "<div>Hello World</div>" // Initial jsdom html.
  extensions: ..., // Same as require.extensions but only used in the jsdom context.
  // All other options forwarded to jsdom.
});

const titleSetter = browser.require("./set-document-title");

titleSetter("Updated!");

assert.equal(browser.window.document.title, "Updated!");

./set-document-title.js

const $ = require("jquery"); // Any subsequent requires are evaluated in the jsdom window as well.

typeof global; // undefined

module.exports = (title) => {
  document.title = title;
}

Additional browser apis

The result of createBrowser api gives you a jsdom instance. The following methods are added to the jsdom instance:

browser.require(id: string): Exports

This is the same as the require function in node, except with all modules evaluated in the jsdom window's context.

browser.yield(): Promise

Waits for one macro task (setImmediate) to occur in the browser.

browser.act(fn?: () => T): Promise<Awaited>

Runs and awaits a given (optional) function, then waits for a macro task (see browser.yield). This helper also keeps track of any uncaught errors within the browser context and will reject the promise with the error, or an AggregateError if there are any.

Contributions

  • Use npm test to build and run tests.

Please feel free to create a PR!