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

dirty-chai and chai-enzyme clashing? #17

Closed
devboell opened this issue Jun 30, 2016 · 4 comments
Closed

dirty-chai and chai-enzyme clashing? #17

devboell opened this issue Jun 30, 2016 · 4 comments

Comments

@devboell
Copy link

Hi,

I came across some glitches when using chai-enzyme and dirty-chai together.
This small repo explains it, and has code that reproduces the bug:

https://github.com/devboell/chai-enzyme-error.git

I have also seen it occur without mocha in watch mode, but I can't reproduce it.
Would you please let me know if you can reproduce the error on your machine? If only for my sanity's sake :)

thanks

@travisperson
Copy link

travisperson commented Oct 11, 2016

The following also fails when watching

let Comp = () => <div className="hello">hello world</div>

describe('Test', () => {
  it('should contain text', () => {
    const wrapper = shallow(<Comp />)

    expect(wrapper).to.contain.text("world");
  })
})
  Test
    ✓ should contain text


  1 passing (22ms)



  Test
    1) should contain text


  0 passing (17ms)
  1 failing

  1) Test should contain text:
     AssertionError: expected <Comp /> to have text 'world', but it has 'hello world'      

     HTML:

     <div class="hello">hello world</div>

@devboell I also have seen it fail on start, can't make a minimal reproduction of failing on first run either, but my application does have that failure.

@saifelse
Copy link

My hypothesis is that it has to do with contain being already defined by chai and chai-enzyme overriding it.

The following repro works when run from node (4) directly:

const chai = require('chai');
const chaiEnzyme = require('chai-enzyme');
const dirtyChai = require('dirty-chai');
const enzyme = require('enzyme');
const React = require('react');

chai.use(dirtyChai);
chai.use(chaiEnzyme());

const User = React.createClass({
  displayName: 'User',
  propTypes: {
    index: React.PropTypes.number,
  },
  render() {
    return React.DOM.span(null, `User ${this.props.index}`);
  },
});

const Fixture = React.createClass({
  displayName: 'Fixture',
  render() {
    return React.DOM.div(null,
      React.DOM.ul(null,
        React.DOM.li(null, React.createElement(User, {index: 1})),
        React.DOM.li(null, React.createElement(User, {index: 2}))
      )
    );
  },
});

var wrapper = enzyme.shallow(React.createElement(Fixture));
chai.expect(wrapper).contain(React.createElement(User, {index: 1}));
chai.expect(wrapper).not.contain(React.createElement(User, {index: 3}));  // errors, not is ignored.

Out:

/Users/saif/bnch/aurelia/node_modules/chai/lib/chai/assertion.js:107
      throw new AssertionError(msg, {
      ^
AssertionError: expected <Fixture /> to contain <User index={3} />

     HTML:

     <div>
       <ul>
         <li><span>User 1</span>
         </li>
         <li><span>User 2</span>
         </li>
       </ul>
     </div>
    at Object.<anonymous> (dirty-chai-test.js:35:26)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:968:3

For me, switching the order of the imports fixes the problem:

chai.use(chaiEnzyme());
chai.use(dirtyChai);

@dietergeerts
Copy link

dietergeerts commented Dec 21, 2017

I also had a problem with the combinatino of chai-as-promised and sinon-chai. doing:

chai.use(sinonChai);
chai.use(chaiAsPromised);
chai.use(dirtyChai);
chai.use(chaiDatetime);

fixed the issue, which is strange, as the readme explicitly says the use sinonChai before dirtyChai....

BUT then I get other problems. It seems that dirtyChai can't work together with certain combinations of other packages.....

@joshperry
Copy link
Contributor

joshperry commented Mar 12, 2018

The method we use to detect, modify, and hook assertion properties is necessarily a little non-deterministic because of the dynamic way Chai's plugin system works. Unfortunately we can't address issues like these classes without testing and possibly customization of some logic discriminated by the target plugin.

There are so many plugins that this may be a never-ending task, but perhaps there are a few changes that can make this more compatible. For issues like this I've been tracking some major changes that Chai is planning to make. There have been some discussions about making assertion styles pluggable, but I believe that would come after some major plugin refactoring they have been hashing out for quite awhile in chaijs/chai#585.

I'm going to close and track this work in the more generic #32.

Thank you for the discussion!

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

5 participants