These are chat archives for airbnb/enzyme

20th
Jan 2016
Willson Mock
@fay-jai
Jan 20 2016 07:47
So I'm using version 2.0.0-rc1 of enzyme and trying to follow the example on Enzyme's github page
The example is this one:

import { mount } from 'enzyme';

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

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

it('allows us to set props', () => {
const wrapper = mount(<Foo bar="baz" />);
expect(wrapper.props().bar).to.equal("baz");
wrapper.setProps({ bar: "foo" });
expect(wrapper.props().bar).to.equal("foo");
});

it('simulates click events', () => {
const onButtonClick = spy();
const wrapper = mount(
<Foo onButtonClick={onButtonClick} />
);
wrapper.find('button').simulate('click');
expect(onButtonClick.calledOnce).to.equal(true);
});

});

however, I'm getting the follow error: Error: It looks like you called mount() without a jsdom document being loaded. Make sure to only use mount() inside of a describeWithDOM(...) call.
The github example seems to imply that you don't need to use describeWithDOM anymore
Jordan Harband
@ljharb
Jan 20 2016 07:51
you still need to have a dom
it's just that describeWithDOM won't be part of enzyme directly
part of why it's an rc is that it's not ready yet :-)
Willson Mock
@fay-jai
Jan 20 2016 07:52
so if my package.json includes mocha, chai, react-addons-test-utils, react-dom, do i need to install something else?

to be exact, this is what i have in my package.json:

{
"name": "test",
"version": "1.0.0",
"scripts": {
"build": "webpack",
"test": "mocha --compilers js:babel-register --recursive"
},
"devDependencies": {
"babel-core": "^6.4.5",
"babel-loader": "^6.2.1",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babel-register": "^6.4.3",
"chai": "^3.4.1",
"enzyme": "^2.0.0-rc1",
"mocha": "^2.3.4",
"react-addons-test-utils": "^0.14.6",
"sinon": "^1.17.2",
"webpack": "^1.12.11",
"webpack-dev-server": "^1.14.1"
},
"dependencies": {
"react": "^0.14.6",
"react-dom": "^0.14.6"
}
}

Jordan Harband
@ljharb
Jan 20 2016 07:54
you still need jsdom
and you need to stub document before you run your tests
because react requires a global document
i believe leland's plan is to make a separate module for "describeWithDOM", but i'm not 100% sure
Willson Mock
@fay-jai
Jan 20 2016 08:00
ah gotcha
thanks a ton for the pointer @ljharb
Jordan Harband
@ljharb
Jan 20 2016 08:02
np
Willson Mock
@fay-jai
Jan 20 2016 08:15
in case anyone else was also curious, this is a good follow up => http://jaketrent.com/post/testing-react-with-jsdom/