Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positives with .to.exist #777

Closed
AlexEscalante opened this issue Aug 25, 2016 · 3 comments
Closed

False positives with .to.exist #777

AlexEscalante opened this issue Aug 25, 2016 · 3 comments

Comments

@AlexEscalante
Copy link

AlexEscalante commented Aug 25, 2016

I am using Mocha, Enzyme and Chai, and I am getting false positives on tests like these:

import React from 'react';
import {shallow} from 'enzyme';
import {expect} from 'chai';

it('renders a barcode', () => {
  const wrapper = shallow(<ReceiptPage {...props} />);

  expect(wrapper.find('bogus')).to.exist;
});

That test should not pass and it does. I should be doing something wrong, but I can't figure out what… Any ideas?

@lucasfcosta
Copy link
Member

lucasfcosta commented Aug 26, 2016

Hi @AlexEscalante, thanks for your issue! 😄
Can you give me some more details about this? I'd like to know what does wrapper.find('bogus') returns, is it really undefined or is it null?
As you can see here the code for our exist assertion is really simple, I can't really think of why this is happening.

I noticed that we're using != here. It works, but perhaps writing <whatever> !== undefined && <whatever> !== null would be more explicit and it would also meet most of JS code recommendations.

EDIT:
I also noticed we need more tests for that and we also need to put that assertion on the assert interface.
I have a PR almost done for that, I'll be submiting it by today's night :)

@keithamus
Copy link
Member

AFAICT this is because ShallowWrapper#find returns a new wrapper object, so that you can chain those kinds of commands. A Wrapper "exists" and so the assertion is failing because Wrapper !== null && Wrapper !== undefined.

I would recommend, @AlexEscalante, using something like chai-enzyme - which overrides .to.exist to detect for Enzyme Wrapper objects, and so would return correctly. Alternatively, you could reword your assertion to something like expect(wrapper.find('bogus')).to.have.lengthOf(0);.

Hopefully this helps. I'll close the issue for now but let us know if you have any more problems with it.

@AlexEscalante
Copy link
Author

I tried using chai-enzyme but had no luck:

import chai, {expect} from 'chai';
import chaiEnzyme from 'chai-enzyme';

chai.use(chaiEnzyme());

With that setup, every time I get a wrapper:

const wrapper = shallow(<ReceiptPage {...props} />);

... I get an error:

 TypeError: (0 , _enzyme2.default) is not a function

These are the versions I am using:

"chai": "3.5.0",
"chai-enzyme": "0.5.1",
"enzyme": "2.2.0",
"cheerio": "0.20.0",
"react": "15.0.2"

I didn't find any reference to this issue, neither here nor in the chai-enzyme project which is strange, isn't? What would you suggest?

I am aware I could use the .to.have.lengthOf() syntax, but I find the to.exist or to.be.present syntax semantically superior…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants