enzyme: Error: This method is only meant to be run on single node. 0 found instead.
Clearly rendering a node. It’s not picking it up…?
render() {
let groceriesComponents = [],
newProductInput,
newProductAddButton,
clearListButton;
for(var index = 0; index < this.state.groceries.length; index++) {
groceriesComponents.push(
<GroceryListItem
key={index}
grocery={this.state.groceries[index]}
/>
);
}
newProductInput = <input className='new-item' type="text" onChange={this.inputChanged}/>;
newProductAddButton = <button className='add-product' onClick={this.addGroceryItem}>Add new Product</button>;
clearListButton = <button onClick={this.clearList} className='clear-list'>Clear the List</button>;
return (
<div>
<ul>
{groceriesComponents}
</ul>
{newProductInput}
{newProductAddButton}
{clearListButton}
</div>
);
}
}
describe("Task #3 - clearing groceries list", () => {
beforeEach( () => {
component = mount(<GroceryListPart3 />);
});
it('Should render required tags', () => {
try { component.find(".clear-list"); }
catch(err){
throw new Error("I can't find 'Clear the List' button");
}
});
it('is possible to remove all list items', () => {
let clearListButton = component.find(".clear-list");
clearListButton.simulate('click');
let groceryListItems = component.find("li");
assert.equal(groceryListItems.length, 0, "There should be exactly zero elements on the groceries list");
});
});
Error: This method is only meant to be run on single node. 0 found instead.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 61 (21 by maintainers)
Commits related to this issue
- [Fix] include method name in “single node” errors Per https://github.com/airbnb/enzyme/issues/184#issuecomment-238045505 — committed to enzymejs/enzyme by ljharb 8 years ago
@BennyHinrichs @ljharb
I wasn’t sure what you meant by trying to find with a reference instead but after a lot of trial and error / googling around I found out that I could select the element using
I found the actual display name by digging through the React Chrome extension and finding out it’s exact name.
Though this doesn’t seem like a good solution. Have you found a better one?
@BennyHinrichs @sanyangkkun - I believe what @ljharb meant is that instead of using the form -
wrapper.find('SingleDatePicker')
you should remove the quotes (string) and use the component class reference, like so -wrapper.find(SingleDatePicker);
@emragins I’ve added that extra info in 857760c, and it will be included in the next release. Thanks for the suggestion!
Looks like my project was reactjs_koans had duplicate folders and I was editing in the wrong one. No problems. Apologies for that.
Just to add to @revik . SingleDatePicker should be imported as well.
@cameronroe try outputting
component.debug()
to the console and see if that gives you any clues. From the context you’ve given us here, I can’t see any reason why it would be failing.I got it by rewriting
expect(wrapper.find('Redirect').at(0).props()).toHaveProperty('to', '/sign-up')
asexpect(wrapper.exists('SignIn')).toBe(true)
figured it out:
we’re not in HTML land. I’m just geting the hang of enzyme.
I get this error with the following in my test, and no
header
in my componentenzymeWrapper.find('header').hasClass('myClass')
The error happens because no header is found, so hasClass() blows up. I would really appreciate a more meaningful error message that doesn’t force me to google it to figure out what the heck it’s talking about. That said, now that I understand it, I can understand why this would be difficult to do…
A start might be instead of “this method” give the name of the method. ie. “‘hasClass’ is only meant…”
For an uninitiated this would keep from thinking that the it’s an internal problem – my original thought was “oh no… just my luck. Something’s going on and one of the internal rendering nodes (processes) can’t start up and I have no idea how to debug this”.
Another thought is to have find() error if the element(s) can’t be found. Since that would break the existing API, and make it so that negative tests would fail, maybe a second parameter of
.find(expression, throwOnNotFound)
?Just throwing out ideas here for discussion.
@simoneas02 alternatively/better, you could
wrapper.find(DateRangePicker)
(importing DateRangePicker from react-dates) and do the same.@ljharb here #2493
I got the same error
Method “props” is meant to be run on 1 node. 0 found instead.
and I solved it usingwithStyles
I’m getting this same problem. I’m trying to test the onDateChange prop of the react-dates SingleDatePicker, and it thinks I have 0 nodes. Here’s an excerpt of my component file:
And here’s the test:
As the issue title states, it gives me the message,
"Method “props” is only meant to be run on a single node. 0 found instead."
I feel like I’m following the suggestion you made in Issue #1066, but it’s still not working.Edit: After more testing, this seems to be an issue specific to react-dates. react-dates has three components,
DateRangePicker, SingleDatePicker, DayPickerRangeController
, and Enzyme doesn’t register any of them as nodes. I have no idea what could be behind this issue.