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-extra

v3.0.7

Published

JSDOM with extra tweak ( jquery / cheerio / request )

Downloads

227

Readme

jsdom-extra

JSDOM with extra tweak ( jquery / cheerio / request )

npm i jsdom-extra

optional install

if u didn't need jquery or cheerio, then no need install them.
but never try set/get $ from jsdom
or will throw Error

API options for jquery / cheerio

use with jquery

npm i jsdom-extra jquery

use with cheerio

npm i jsdom-extra cheerio

options

virtualConsole

if virtualConsole === false

this is help if u didn't wanna see console output from jsdom

// old way
options.virtualConsole = new VirtualConsole();

// new way
options.virtualConsole = false;

demo

jquery or cheerio

// return jquery or cheerio
let $ = createJSDOM().$;
$(':root').length;

createJSDOM

with create jsdom with extra prototype

export interface IJSDOM extends JSDOM {
    $: JQueryStatic;
    url: URL;
    document: Document;
    _options: IJSDOM_Symbol_Options;
    fakeThen<T>(cb: (jsdom: IJSDOM) => T): T;
}

export declare function createJSDOM(html?: string | Buffer | BinaryData, options?: IConstructorOptions): IJSDOM;

fake Promise then

// fake .then, not realy Promise, still is sync
createJSDOM().fakeThen(function (jsdom)
{
	console.log(jsdom._options);
});

asyncJSDOM / Promise

asyncJSDOM().then(function (jsdom)
{
	console.log(jsdom._options);
});

packJSDOM(jsdom: JSDOM): IJSDOM

pack any jsdom object with extra

import { JSDOM } from 'jsdom';
import { packJSDOM } from 'jsdom-extra';

let jsdom2 = new JSDOM();

// will overwrite jsdom2 too
packJSDOM(jsdom2) // => return jsdom2

fromURL

allow use POST on fromURL

function fromURL(url: string, options?: IFromUrlOptions): Promise<IJSDOM>
export interface IFromUrlOptions extends Partial<FromUrlOptions & IOptionsJSDOM>
{
	requestOptions?: Partial<IRequestOptions>,
	cookieJar?: ICookieJar,
}

export interface IRequestOptionsJSDOM
{
	resolveWithFullResponse: boolean;
	encoding: null;
	gzip: boolean;
	headers: {
		"User-Agent": string;
		Referer: string;
		Accept: string;
		"Accept-Language": string;
	};
	jar: IRequestJar;
}

export interface IRequestOptions extends Partial<IRequestOptionsJSDOM>
{
	method?: 'POST' | 'GET' | string,
	form?: {
		[key: string]: any,
		[key: number]: any,
	},
}

fromFile

function fromFile(url: string, options?: IFromFileOptions): Promise<IJSDOM>

get the finally options that use on create JSDOM

createJSDOM()._options.ConstructorOptions

output

   { windowOptions: 
      { url: 'about:blank',
        referrer: '',
        contentType: 'text/html',
        parsingMode: 'html',
        userAgent: 'Mozilla/5.0 (win32) AppleWebKit/537.36 (KHTML, like Gecko) jsdom/11.6.2',
        parseOptions: [Object],
        runScripts: undefined,
        encoding: 'UTF-8',
        pretendToBeVisual: false,
        virtualConsole: [VirtualConsole],
        cookieJar: [CookieJar] },
     resources: undefined,
     beforeParse: [Function] }

lazy overwrite / require

will overwrite global JSDOM

import JSDOM from 'jsdom-extra/auto';
const JSDOM = require("jsdom-extra/auto").JSDOM;

globalJsdom

same as jsdom-global, but with

function globalJsdom<T>(html?, options: Partial<T & IConstructorOptions & IOptions> = {})

create

require("jsdom-extra/global")()

const globalJsdom = require("jsdom-extra/global");
globalJsdom();
let ret: {
       jsdom: IGlobalJSDOM;
       window: DOMWindow;
       document: Document;
       cleanup: () => void;
   };

ret = globalJsdom();

cleanup

ret.cleanup();
ret.jsdom.cleanup();

Mocha

Simple: Use Mocha's --require option. Add this to the test/mocha.opts file (create it if it doesn't exist)

mocha --require jsdom-extra/lib/global/register

Advanced: For finer control, you can instead add it via mocha's before and after hooks.

before(function () {
  this.globalJsdom = require('jsdom-extra/global')()
})

after(function () {
  this.globalJsdom.cleanup()
})