0

I am testing a class like:

const wrapper = shallow(<Features {...props}/>);
expect(wrapper.find('.Feature__Item:not(.Feature__Showmore)').length).toBe(2);

But, I am getting an error like this: Error: Enzyme::Selector does not support pseudo-element or pseudo-class selectors.

How can I work around this limitation?

Update: Interesting observation, when I wrote the following in the debug console: wrapper.find('.Feature__Item').findWhere(a => a.hasClass('Feature__Showmore') === false).length

The output was:

0:Object {nodeType: "host", type: "li", props: Object, …}
1:Object {nodeType: "function", type: , props: Object, …}
2:Object {nodeType: "host", type: "li", props: Object, …}
3:Object {nodeType: "function", type: , props: Object, …}

2 Answers 2

4

I found another way which did the trick:

expect(wrapper.find('.Feature__Item').not('.Feature__Showmore').length).toBe(2);
1
  • 1
    You can also use .toHaveLength(2) instead of .length).toBe(2) Jun 13, 2019 at 8:52
1

You could try with findWhere:

expect(wrapper.find('.Feature__Item').findWhere(a => a.hasClass('Feature__Showmore') === false).length).toBe(2);

This will first get all the elements having class Feature__Item and then exclude from this list the ones which don't have Feature__Showmore class.

2
  • This makes sense to me, but I expect 2 and I get 4 for some reason. Jun 13, 2019 at 8:48
  • Check what is actually rendered with console.log(wrapper.debug())
    – Dario
    Jun 13, 2019 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.