Enzyme: Enzyme + jsdom -> TypeError: jsdom is not a function

Created on 14 May 2017  Â·  23Comments  Â·  Source: enzymejs/enzyme

I am trying to get Enzyme up and running with jsdom as describe here:

http://airbnb.io/enzyme/docs/guides/jsdom.html

/* setup.js */

var jsdom = require('jsdom').jsdom;

global.document = jsdom('');
global.window = document.defaultView;
Object.keys(document.defaultView).forEach((property) => {
  if (typeof global[property] === 'undefined') {
    global[property] = document.defaultView[property];
  }
});

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

However, this gives the following:

TypeError: jsdom is not a function from line 3 global.document = jsdom('');, why? And how to resolve this?

Most helpful comment

Got it to work with [email protected] and [email protected] as follows:

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

const { document } = (new JSDOM('')).window;
global.document = document;

All 23 comments

What version of jsdom are you using? Our instructions were written for v6, iirc.

https://github.com/airbnb/enzyme/pull/921 may be helpful.

This is "jsdom": "^10.1.0", and "enzyme": "^2.8.2". #921 appears to be the same stuff, but if that is my issue then I wonder why I have this problem since the guide should be updated ...

That pr is not yet merged; if you use its instructions does it work for you?

pls update live docs to v10 😿

edit: fyi unmerged PR docs make everything work fine 🤘

Yup, unmerged PR docs make everything work fine 🤘

Same issue with "jsdom": "^11.0.0", and "enzyme": "^2.9.1",

Yep, same as @stevematdavies

Using setup docs from here causes error:

TypeError: require(...).jsdom is not a function

      at new JSDOMEnvironment (node_modules/jest-environment-jsdom/build/index.js:26:38)
      at handle (node_modules/worker-farm/lib/child/index.js:41:8)
      at process.<anonymous> (node_modules/worker-farm/lib/child/index.js:47:3)
      at emitTwo (events.js:125:13)
      at process.emit (events.js:213:7)
      at emit (internal/child_process.js:742:12)
      at _combinedTickCallback (internal/process/next_tick.js:105:11)
      at process._tickCallback (internal/process/next_tick.js:161:9)

Does it work with v10? It's possible v11 has a breaking change that necessitates different instructions.

Got it to work with [email protected] and [email protected] as follows:

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

const { document } = (new JSDOM('')).window;
global.document = document;

@vlaurin thanks for posting ur workaround, helped me alot mate :+1:

i use this processing but i have error :
Invariant Violation: dangerouslyReplaceNodeWithMarkup(...): Cannot render markup in a worker thread. Make sure window and document are available globally before requiring React when unit testing or use ReactDOMServer.renderToString() for server rendering.

what i happen, how should i do.

I believe you need to make 'window' available globally in addition to 'document' (shown in the above example). The below works for me:

import jsdom from 'jsdom';
const {JSDOM} = jsdom;
const {document} = (new JSDOM('<!doctype html><html><body></body></html>')).window;
global.document = document;
global.window = document.defaultView;

@vlaurin you saved my day man.

@vlaurin I am stranger on Internet But I really ran into your great post. My hero, Thanks a lot.

@vlaurin thanks. You solved my probelem

@Mike26372. You solved my problem

None of the workarounds presented above helped... :(

@vlaurin @Mike26372
Thanks for the help. Your solution works for me.
My env has:
"enzyme": "^3.2.0",
"jsdom": "^11.5.1",

@vlaurin Where does this code go? At the top of each individual test jsx file? Or in the setup.js? From all of the examples, I am confused where the setup.js is supposed to go.

Had this issue while following this guide: https://semaphoreci.com/community/tutorials/getting-started-with-tdd-in-react
This workaround also solved it for me, thanks!: https://github.com/airbnb/enzyme/issues/942#issuecomment-314715229

hi all ,Am using

**"enzyme": "^3.10.0",
"enzyme-adapter-react-16": "^1.14.0"
"jsdom": "^15.2.1".**

my test file is as follow:

 tape("d3.select(…) returns an instanceof d3.selection", function(test) {
        **var document = jsdom("<h1>hello</h1>");**
        test.ok(d3.select(document) instanceof d3.selection);
        test.end();
      });

am getting an error like

image

can you plz help me with this

@megha-ui this issue is closed; please file a new one if you have a new question. In this case, it looks like you're invoking jsdom without importing/requiring it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abe903 picture abe903  Â·  3Comments

rexonms picture rexonms  Â·  3Comments

dschinkel picture dschinkel  Â·  3Comments

andrewhl picture andrewhl  Â·  3Comments

blainekasten picture blainekasten  Â·  3Comments