
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
jsdom-context-require
Advanced tools
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.
npm install jsdom jsdom-context-require -D
Note: JSDOM is required as a peerDependency as of jsdom-context-require@4
./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;
}
The result of createBrowser api gives you a jsdom instance.
The following methods are added to the jsdom instance:
This is the same as the require function in node, except with all modules evaluated in the jsdom window's context.
Waits for one macro task (setImmediate) to occur in the browser.
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.
npm test to build and run tests.Please feel free to create a PR!
FAQs
Allows you to require files in a jsdom window context.
We found that jsdom-context-require demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.