Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jsdom-extra

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsdom-extra

JSDOM with extra tweak ( jquery / cheerio / request )

  • 1.2.7
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
51
increased by13.33%
Maintainers
1
Weekly downloads
 
Created
Source

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

  • API

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()
})

Keywords

FAQs

Package last updated on 25 Mar 2018

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc