Skip to content

Instantly share code, notes, and snippets.

@jordanell
Created May 23, 2018 17:32
Show Gist options
  • Save jordanell/d0cf41159b503bb76ecc2eb44c14065e to your computer and use it in GitHub Desktop.
Save jordanell/d0cf41159b503bb76ecc2eb44c14065e to your computer and use it in GitHub Desktop.
import Enzyme from 'enzyme';
import { JSDOM } from 'jsdom';
import initialize from 'src/initialize';
// Initialize our project
initialize();
// Force the test environment
process.env.NODE_ENV = 'test';
// Simulate window if we're running in Node.js
if (!global.window && !global.document) {
const { window } = new JSDOM('<!doctype html><html><body></body></html>', {
beforeParse(win) {
win.scrollTo = () => {};
},
pretendToBeVisual: false,
userAgent: 'mocha',
});
// Configure global variables which like to be used in testing
global.window = window;
global.document = window.document;
global.navigator = window.navigator;
}
// Critical: This Enzyme adapter imports React, but the JSDOM above // must be created BEFORE React is imported.
const Adapter = require('enzyme-adapter-react-15');
Enzyme.configure({ adapter: new Adapter() });
// Create a place for react to grab onto.
const app = document.createElement('div');
app.id = 'react-root';
document
.getElementsByTagName('body')
.item(0)
.appendChild(app);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment