Enzyme: Mounted component componentDidMount.calledOnce is undefined

Created on 7 Apr 2016  路  3Comments  路  Source: enzymejs/enzyme

I'm using the enzyme Mocha setup, and I get the following error:

  1) <TestComponent /> calls componentDidMount:
     AssertionError: expected undefined to equal true
      at Context.<anonymous> (test_component_spec.js:11:73)

TestComponent is just:

import React, {Component} from 'react';

class TestComponent extends Component {

    render() {
        return (
            <div>
                Test
            </div>
        )
    }
}

export default TestComponent;

My test definition is here:

import React from 'react';
import { mount, shallow } from 'enzyme';
import {expect} from 'chai';
import TestComponent from '../components/TestComponent.jsx';

describe('<TestComponent />', () => {

    it('calls componentDidMount', () => {
        const wrapper = mount(<TestComponent />);
        expect(TestComponent.prototype.componentDidMount.calledOnce).to.equal(true);
    });

});

And I have a test_helper.js file that sets up the jsdom:

import jsdom from 'jsdom';

const doc = jsdom.jsdom('<!doctype html>' +
    '<html><body></body></html>');
const win = doc.defaultView;

global.document = doc;
global.window = win;

Object.keys(window).forEach((key) => {
    if (!(key in global)) {
        global[key] = window[key];
    }
});

Not sure what the issue is.

Most helpful comment

Just ran into same issue, fix sounds pretty obvious but should probably have this in the official example => http://airbnb.io/enzyme/docs/guides/mocha.html

All 3 comments

I don't see anywhere you're spying on TestComponent.prototype.componentDidMount?

Ah, I see. I need to import sinon, and do:

        sinon.spy(TestComponent.prototype, 'componentDidMount');

Thanks. The documentation was not clear.

Just ran into same issue, fix sounds pretty obvious but should probably have this in the official example => http://airbnb.io/enzyme/docs/guides/mocha.html

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ahuth picture ahuth  路  3Comments

timhonders picture timhonders  路  3Comments

heikkimu picture heikkimu  路  3Comments

nelsonchen90 picture nelsonchen90  路  3Comments

mattkauffman23 picture mattkauffman23  路  3Comments