import { JSDOM, VirtualConsole, CookieJar, FromUrlOptions, FromFileOptions, DOMWindow, ResourceLoader, FetchOptions, ConstructorOptions } from 'jsdom'; import { CookieJar as ToughCookieJar, MemoryCookieStore } from 'tough-cookie'; import { Script } from 'vm'; function test_basic_usage() { const dom = new JSDOM(`
Hello world
`); console.log(dom.window.document.querySelector('p')!.textContent); // "Hello world" const { window } = new JSDOM(`...`); // or even const { document } = (new JSDOM(`...`)).window; } function test_executing_scripts1() { const dom = new JSDOM(` `); // The script will not be executed, by default: dom.window.document.body.children.length === 1; } function test_executing_scripts2() { const dom = new JSDOM(` `, { runScripts: 'dangerously' }); // The script will be executed and modify the DOM: dom.window.document.body.children.length === 2; } function test_executing_scripts3() { const window = (new JSDOM(``, { runScripts: 'outside-only' })).window; window.eval(`document.body.innerHTML = "Hello, world!
";`); window.document.body.children.length === 1; } function test_virtualConsole() { const virtualConsole = new VirtualConsole(); const dom = new JSDOM(``, { virtualConsole }); virtualConsole.on('error', () => { }); virtualConsole.on('warn', () => { }); virtualConsole.on('info', () => { }); virtualConsole.on('dir', () => { }); // ... etc. See https://console.spec.whatwg.org/#logging virtualConsole.sendTo(console); const c = console; virtualConsole.sendTo(c, { omitJSDOMErrors: true }); } function test_cookieJar() { const store = {} as MemoryCookieStore; const options = {} as ToughCookieJar.Options; const cookieJar: CookieJar = new CookieJar(store, options); const constructorOptions: ConstructorOptions = { cookieJar }; const dom = new JSDOM(``, constructorOptions); } function test_beforeParse() { const dom = new JSDOM(`Hello
`, { beforeParse(window) { window.document.childNodes.length === 0; } }); } function test_storageQuota() { new JSDOM('', { storageQuota: 1337 }); } function test_pretendToBeVisual() { new JSDOM('', { pretendToBeVisual: true }); } function test_serialize() { const dom = new JSDOM(`hello`); dom.serialize() === 'hello'; // Contrast with: // tslint:disable-next-line no-unnecessary-type-assertion dom.window.document.documentElement!.outerHTML === 'hello'; } function test_nodeLocation() { const dom = new JSDOM( `Hello
Hello
Hi!`); frag.childNodes.length === 2; frag.querySelector('strong')!.textContent = 'Why hello there!'; // etc. } function test_fragment_serialization() { const frag = JSDOM.fragment(`
Hello
`); if (frag instanceof Element) { if (frag.firstChild instanceof Element) { console.log(frag.firstChild.outerHTML); // logs "Hello
" } } } function test_custom_resource_loader() { class CustomResourceLoader extends ResourceLoader { fetch(url: string, options: FetchOptions) { if (options.element) { console.log(`Element ${options.element.localName} is requesting the url ${url}`); } return super.fetch(url, options); } } new JSDOM('', { resources: new CustomResourceLoader() }); }