question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`closest` return a wrapper with `length: 1` even with unmatched `selector`

See original GitHub issue
const wrapper = shallow(
    React.createElement('div', { className: 'foo' },
        React.createElement('div', { className: 'bar' })
    )
);

console.log(
    wrapper.find('.bar').closest('.fooooo')
);
{
  root:
   ShallowWrapper {
     root: [Circular],
     unrendered:
      { '$$typeof': Symbol(react.element),
        type: 'div',
        key: null,
        ref: null,
        props: [Object],
        _owner: null,
        _store: {} },
     renderer:
      { _instance: null,
        render: [Function: render],
        getRenderOutput: [Function: getRenderOutput] },
     node:
      { '$$typeof': Symbol(react.element),
        type: 'div',
        key: null,
        ref: null,
        props: [Object],
        _owner: null,
        _store: {} },
     nodes: [ [Object] ],
     length: 1,
     options: {} },
  unrendered: null,
  renderer: null,
  node: undefined,
  nodes: [ undefined ],
  length: 1,
  options: {} }

Dummy ShallowWrapper is ok, but length should be 0.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:3
  • Comments:5

github_iconTop GitHub Comments

1reaction
anthonatorcommented, May 26, 2017

I’m seeing this as well.

const wrapper = shallow(
  React.createElement('div', { className: 'foo' },
    React.createElement('div', { className: 'bar' }),
  ),
);

console.log(wrapper.find('.fooooo').length); // 0
console.log(wrapper.find('.bar').closest('.fooooo').length); // 1
0reactions
jmnsfcommented, Sep 20, 2017

Finding this too. A workaround to make sure it’s a true match:

wrapper.closest('.fooooooo').is('.fooooooo'); // false

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is my JQuery selector returning a n.fn.init[0], and what is it?
I want to select them by data-id. Now, using JQuery, I know I can select the relevant checkboxes like this: $(".checkbox-wrapper>input[data ...
Read more >
can't find() element but wrapper.contains() it #1952 - GitHub
Current behavior I cannot find(Component|".selector") but I can see it via ... not present length... expected 1 but received 0") // wrapper.
Read more >
Common mistakes with React Testing Library - Kent C. Dodds
The name wrapper is old cruft from enzyme and we don't need that here. The return value from render is not "wrapping" anything....
Read more >
Searching: getElement*, querySelector*
The method elem. closest(css) looks for the nearest ancestor that matches the CSS-selector. The elem itself is also included in the search. In ......
Read more >
Querying | Cypress examples (v8.7.0) - Gleb Bahmutov
cy.get uses jQuery selectors , thus you can immediately use them to find elements ... it will still be the original parent DOM...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found