Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read property 'firstChild' of undefined when using import (ES6 via Babel) #297

Closed
sebastiandeutsch opened this issue Mar 11, 2015 · 9 comments

Comments

@sebastiandeutsch
Copy link

I want to move my code from require to the official ES6 import syntax:

'use strict';

jest.dontMock('../../../timer/trackings/date-view');

import React from 'react/addons';

describe('DateView', function() {
  it('should show the actuall day', function() {
    var TestUtils = React.addons.TestUtils;
    var DateView = require('../../../timer/trackings/date-view');
    var dateMock = {
      calendar: function() {
        return "Today";
      }
    };
    var dateview = TestUtils.renderIntoDocument(<DateView date={dateMock}/>);

    expect(dateview.getDOMNode().textContent).toBe('Today');
  });
});

Unfortunately this gives me this error:

Using Jest CLI v0.2.2
 FAIL  app/assets/javascripts/jsx/__tests__/timer/trackings/date-view-test.jsx (0.711s)
● DateView › it should show the actuall day
  - TypeError: Cannot read property 'firstChild' of undefined
        at Object.ReactMount.findComponentRoot (/Users/sebastiandeutsch/development/ruby/timetracker/node_modules/react/lib/ReactMount.js:730:39)
        at Object.ReactMount.findReactNodeByID (/Users/sebastiandeutsch/development/ruby/timetracker/node_modules/react/lib/ReactMount.js:676:23)
        at Object.getNodeFromInstance (/Users/sebastiandeutsch/development/ruby/timetracker/node_modules/react/lib/ReactMount.js:165:32)
        at findDOMNode (/Users/sebastiandeutsch/development/ruby/timetracker/node_modules/react/lib/findDOMNode.js:52:23)
        at ReactBrowserComponentMixin.getDOMNode (/Users/sebastiandeutsch/development/ruby/timetracker/node_modules/react/lib/ReactBrowserComponentMixin.js:25:12)
        at Spec.<anonymous> (/Users/sebastiandeutsch/development/ruby/timetracker/app/assets/javascripts/jsx/__tests__/timer/trackings/date-view-test.jsx:20:21)
        at jasmine.Block.execute (/Users/sebastiandeutsch/development/ruby/timetracker/node_modules/jest-cli/vendor/jasmine/jasmine-1.3.0.js:1065:17)
        at jasmine.Queue.next_ (/Users/sebastiandeutsch/development/ruby/timetracker/node_modules/jest-cli/vendor/jasmine/jasmine-1.3.0.js:2098:31)
        at null._onTimeout (/Users/sebastiandeutsch/development/ruby/timetracker/node_modules/jest-cli/vendor/jasmine/jasmine-1.3.0.js:2088:18)
        at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
1 test failed, 0 tests passed (1 total)
Run time: 0.864s
npm ERR! Test failed.  See above for more details.
npm ERR! not ok code 0

when I'm writing the test case oldschool style:

'use strict';

jest.dontMock('../../../timer/trackings/date-view');

describe('DateView', function() {
  it('should show the actuall day', function() {
    var React = require('react/addons');
    var TestUtils = React.addons.TestUtils;
    var DateView = require('../../../timer/trackings/date-view');
    var dateMock = {
      calendar: function() {
        return "Today";
      }
    };
    var dateview = TestUtils.renderIntoDocument(<DateView date={dateMock}/>);

    expect(dateview.getDOMNode().textContent).toBe('Today');
  });
});

it passes. I assume it's the same issue like #186 but if I move the import inside the it(...) or a beforeEach I'll get a compile error:

'import' and 'export' may only appear at the top level (8:4)

Does anyone have a workaround or a solution to this problem?

@nifgraup
Copy link

It could be unrelated but I get the same error when adding new tests. The problem was introduced in this React commit: facebook/react@a194e51

It acts like we are hitting some kind of limit. Any added test causes the "Cannot read property 'firstChild' of undefined" error and it doesn't have to test react components.

Using karma+jasmine (not jest), babel, browserify. No ES6 modules.

@tom76kimo
Copy link

I bumped into this issue too yet without using ES6 or babel. The container from containersByReactRootID[reactRootID] is undefined in file ReactMount.js. And the function registerContainer seems not be called.

@nifgraup
Copy link

@tom76kimo can you test if this problem was introduced with facebook/react@a194e51 ?

@nifgraup
Copy link

Fixed by going to jasmine 1.3 from 2.2.

@sebastiandeutsch
Copy link
Author

@nifgraup did you downgrade jasmine from 2.2 to 1.3?

@nifgraup
Copy link

Yes, that did the trick for some reason.

@nifgraup
Copy link

Back to Jasmine 2.x. Things seem to behave better when I unmount components after each test.

@cpojer
Copy link
Member

cpojer commented Feb 25, 2016

Fixed in #741 for 0.9.0.

@cpojer cpojer closed this as completed Feb 25, 2016
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants