Skip to content

Instantly share code, notes, and snippets.

@pablopaul
Forked from peduarte/testing-react-components.txt
Last active April 18, 2018 23:34
Show Gist options
  • Save pablopaul/2d4bba03d9a1215061b0f4a9390c6b81 to your computer and use it in GitHub Desktop.
Save pablopaul/2d4bba03d9a1215061b0f4a9390c6b81 to your computer and use it in GitHub Desktop.
Fix "ReferenceError: document is not defined" in jsdom + enzyme + ava environment.
/*
* Step 1
*/
npm install --save-dev jsdom
/*
* Step 2 (http://airbnb.io/enzyme/docs/guides/jsdom.html)
*
* test-browser-env.js
*
*/
const { JSDOM } = require('jsdom');
const jsdom = new JSDOM('<!doctype html><html><body></body></html>');
const { window } = jsdom;
function copyProps(src, target) {
const props = Object.getOwnPropertyNames(src)
.filter(prop => typeof target[prop] === 'undefined')
.map(prop => Object.getOwnPropertyDescriptor(src, prop));
Object.defineProperties(target, props);
}
global.window = window;
global.document = window.document;
global.navigator = {
userAgent: 'node.js',
};
copyProps(window, global);
/*
* Step 3
*
* package.json
*/
"ava": {
"require": [
"./test-browser-env.js"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment