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.
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
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