How to use the jsdom.JSDOM function in jsdom

To help you get started, we’ve selected a few jsdom examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sysgears / apollo-universal-starter-kit / modules / testing / client-react / Renderer.tsx View on Github external
import { addTypenameToDocument } from 'apollo-utilities';
import { Router, Switch } from 'react-router-dom';
import { createMemoryHistory, MemoryHistory } from 'history';
import { JSDOM } from 'jsdom';
import { makeExecutableSchema, addMockFunctionsToSchema } from 'graphql-tools';
import { combineReducers, createStore, Store } from 'redux';
import { graphql, print, getOperationAST, DocumentNode, GraphQLSchema } from 'graphql';
import { Provider } from 'react-redux';
import { ApolloClient } from 'apollo-client';
import { render, RenderResult } from '@testing-library/react';

import { createApolloClient } from '@gqlapp/core-common';
import ClientModule from '@gqlapp/module-client-react';

if (!process.env.JEST_WORKER_ID) {
  const dom = new JSDOM('<div id="root"><div>');
  (global as any).document = dom.window.document;
  (global as any).window = dom.window;
  // Needed by Formik &gt;= 1.x
  (global as any).HTMLButtonElement = dom.window.HTMLButtonElement;
  (global as any).navigator = dom.window.navigator;
  process.on('uncaughtException', ex =&gt; {
    console.error('Uncaught error', ex.stack);
  });
}

const ref: { clientModules: ClientModule; typeDefs: DocumentNode[] } = { clientModules: null, typeDefs: null };

export const initRenderer = (graphqlTypeDefs: DocumentNode[], clientModules: ClientModule) =&gt; {
  ref.clientModules = clientModules;
  ref.typeDefs = graphqlTypeDefs;
};</div></div>
github sveltejs / svelte / test / helpers.js View on Github external
}
}

export function tryToReadFile(file) {
	try {
		return fs.readFileSync(file, 'utf-8');
	} catch (err) {
		if (err.code !== 'ENOENT') throw err;
		return null;
	}
}

const virtualConsole = new jsdom.VirtualConsole();
virtualConsole.sendTo(console);

const window = new jsdom.JSDOM('<main></main>', {virtualConsole}).window;
global.document = window.document;
global.navigator = window.navigator;
global.getComputedStyle = window.getComputedStyle;
global.requestAnimationFrame = null; // placeholder, filled in using set_raf
global.window = window;

// add missing ecmascript globals to window
for (const key of Object.getOwnPropertyNames(global)) {
	window[key] = window[key] || global[key];
}

// implement mock scroll
window.scrollTo = function(pageXOffset, pageYOffset) {
	window.pageXOffset = pageXOffset;
	window.pageYOffset = pageYOffset;
};
github danburzo / percollate / index.js View on Github external
function createDom({ url, content }) {
	const dom = new JSDOM(content, { url });

	// Force relative URL resolution
	dom.window.document.body.setAttribute(null, null);

	return dom;
}
github CrocoDillon / universal-react-redux-boilerplate / mocha.config.js View on Github external
// Set up chai and sinon
const chai  = require('chai')
const sinon = require('sinon')

chai.use(require('chai-as-promised'))
chai.use(require('sinon-chai'))
chai.use(require('chai-enzyme')())

global.expect = chai.expect
global.sinon  = sinon

// Set up jsdom
const jsdom = require('jsdom')

const document = new jsdom.JSDOM()

global.document  = document
global.window    = document.defaultView
global.navigator = { userAgent: 'node.js' }

// Hook for CSS Module imports enables using classes in tests
const hook = require('css-modules-require-hook')
const sass = require('node-sass')

hook({
  extensions: ['.scss'],
  generateScopedName: '[local]__[hash:base64:4]',
  preprocessCss: (css, file) => sass.renderSync({ file }).css
})

// Load tests
github neuroanatomy / BrainBox / controller / atlasmakerServer / atlasmakerServer.js View on Github external
socket.destroy();
        }, 5000);
    }

    if (useBlacklist && blacklist[ip]) {
        tracer.log("------------------------------> blacklist", ip);
        setTimeout(function() {
            tracer.log("blacklist: end");
            socket.destroy();
        }, 5000);
    }
});

const createDOMPurify = require('dompurify');
const { JSDOM } = require('jsdom');
const window = (new JSDOM('', {
    features: {
        FetchExternalResources: false, // disables resource loading over HTTP / filesystem
        ProcessExternalResources: false // do not execute JS within script blocks
    }
})).window;
const DOMPurify = createDOMPurify(window);

const jsonpatch = require('fast-json-patch');

function bufferTag(str, sz) {
    var buf = new Buffer(sz).fill(32);
    buf.write(str);

    return buf;
}
github susam / texme / test / test-render-page.js View on Github external
it('content in textarea', function () {
    var html = '<textarea>Foo'

    global.window = new jsdom.JSDOM(html).window
    global.window.MathJax = { Hub: { Queue: sinon.fake() } }

    texme.renderPage()
    assert.strictEqual(global.window.document.body.innerHTML,
      '&lt;main&gt;&lt;p&gt;Foo&lt;/p&gt;\n&lt;/main&gt;')

    delete global.window
  })
</textarea>
github MaxArt2501 / share-this / test / sharers / linked-in.js View on Github external
it("must open a new window with the link provided by `getShareUrl`", (done) => {
            const html = linkedInSharer.render("foo", "foo", "path/to/whatever");
            const { window } = new JSDOM(html);

            const event = new window.Event("click");
            const openStub = stub(window, "open");
            openStub.returns({});

            const text = linkedInSharer.getText("foo");
            const url = linkedInSharer.getShareUrl(text, "path/to/whatever");

            linkedInSharer.action(event, window.document.body);
            expect(openStub).to.have.been.calledWith(url, match.any, match.any);
            done();
        });
github romac / react-if / test / ReactIf.js View on Github external
function setUpDomEnvironment() {
	const dom = new JSDOM('', {url: 'http://localhost/'});
	const { window } = dom;

	global.window = window;
	global.document = window.document;
	global.navigator = {
			userAgent: 'node.js',
	};
	copyProps(window, global);
}
github Dean177 / with-url-state / setupTestFramework.js View on Github external
const Enzyme = require('enzyme');
const Adapter = require('enzyme-adapter-react-16');

Enzyme.configure({ adapter: new Adapter() });

const { JSDOM } = require('jsdom');

const jsdom = new JSDOM('', {
  url: 'https://example.com',
});
const { window } = jsdom;

function copyProps(src, target) {
  const props = Object.getOwnPropertyNames(src)
    .filter(prop =&gt; typeof target[prop] === 'undefined')
    .map(prop =&gt; Object.getOwnPropertyDescriptor(src, prop));
  Object.defineProperties(target, props);
}

global.window = window;
global.document = window.document;
global.navigator = {
  userAgent: 'node.js',
};