enzyme: TypeError: Cannot read property 'current' of undefined

image

This happened after “enzyme-adapter-utils” was updated to version 1.10.0.

As well, I have import 'jsdom-global/register'; at the beginning of the test file.

API

  • shallow
  • mount
  • render

Version

library version
enzyme 3.7.0
react 16.6.3
react-dom 16.6.3
react-test-renderer 16.8.0
adapter (below) 1.6.0
jsdom-global 3.0.2

Adapter

  • enzyme-adapter-react-16
  • enzyme-adapter-react-16.3
  • enzyme-adapter-react-16.2
  • enzyme-adapter-react-16.1
  • enzyme-adapter-react-15
  • enzyme-adapter-react-15.4
  • enzyme-adapter-react-14
  • enzyme-adapter-react-13
  • enzyme-adapter-react-helper
  • others ( )

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 18
  • Comments: 35 (6 by maintainers)

Most upvoted comments

@benedyktdryl Updating React isn’t always an option.

I updated both react & react Dom 16.8.0 along with enzyme-react-adapter version 1.9.1, and I get document is not defined error

the error:

var actContainerElement = document.createElement('div');
                          ^

ReferenceError: document is not defined
    at /var/code/node_modules/react-dom/cjs/react-dom-test-utils.development.js:944:27
    at Object.<anonymous> (/var/code/node_modules/react-dom/cjs/react-dom-test-utils.development.js:1283:5)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Module._compile (/var/code/node_modules/pirates/lib/index.js:83:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Object.newLoader [as .js] (/var/code/node_modules/pirates/lib/index.js:88:7)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/var/code/node_modules/react-dom/test-utils.js:6:20)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Module._compile (/var/code/node_modules/pirates/lib/index.js:83:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:699:10)

Update your React to 16.8. ReactCurrentDispatcher is missing from ReactSharedInternals in earlier releases when react-test-renderer in 16.8 is expecting it (probably because newer version of enzyme-react-adapter in 1.9.1 is downloading react-test-renderer in this version, due to not locked dependencies)

I’ve been fighting with the document is not defined error for most of the morning. I got it working by rearranging things in my helper file. Note: I’m using AVA for my test runner

Before (broken)

import browserEnv from 'browser-env';

import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

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

After (working)

const browserEnv = require('browser-env');
browserEnv();

const { configure } = require('enzyme');
const Adapter = require('enzyme-adapter-react-16');

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

My guess is the adapter is trying to access document during import.

@tolumide-ng all react packages (including react-dom and react-test-renderer) must always have an identical minor version.

Hey We found a solution, which was:

  1. update "enzyme-adapter-react-16": "1.1.0" to "enzyme-adapter-react-16.3": "1.6.1",
  2. within your enzymeSetup change import Adapter from 'enzyme-adapter-react-16 to Adapter from enzyme-adapter-react-16.3

I updated those packages in my local branch and the original find error went away. Of course, it created a different error and invalidated over 60 snapshots. Yikes! It broke other tests, as well. Like I mentioned earlier, though, it’s not as simple as making a command decision to update npm packages.

So, since this issue is closed, should I create a different issue to address my original problem?

You are correct @ljharb. I just overlooked it in the enormously long list of packages. 😉

Keeping the react-test-renderer version the same as react and react-dom.

https://github.com/facebook/react/issues/14763#issuecomment-461016379

@Zveroboev every react package must always have an identical minor; so the fact that you’re using react-test-render 16.8 will break, since you’re using react 16.6. You’ll need to peg that as a dev dep at ~16.6.

If you’ve done that, and you’re still having an issue, I’ll reopen - if anyone else is having an issue, please file a separate one - and make sure all react packages have identical minor versions.

@benedyktdryl thanks, that update, plus adding the --env=jsdom to the test command fixed half of my test, now I get a different error. Cannot find module 'schedule/tracking' from 'ReactNativeRenderer-dev.js'. I’m looking to see if I can find what this means.

Is this to do with being a stateless component that you’re testing? We have the same issue, but its only happening on components that are not extending from React.Component