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

JSX diff #37

Closed
vvo opened this issue Oct 9, 2015 · 9 comments
Closed

JSX diff #37

vvo opened this issue Oct 9, 2015 · 9 comments

Comments

@vvo
Copy link
Collaborator

vvo commented Oct 9, 2015

Hi, I am using https://facebook.github.io/react/docs/test-utils.html#shallow-rendering and wondering how we could automatically have expect() show a nice diff when JSX passed?

I am willing to do the development (maybe in another module?) but would like some insight of you. There's already a jasmine project doing it, I find it really nice: https://www.npmjs.com/package/jasmine-react-diff

Let me know how I can help

@rstacruz
Copy link
Collaborator

Expect just earned an API to allow for extensions... this seems like something that can be done as a separate npm package. Why don't you give it a shot? I expect it's something you can do as expect(a).toJsxEqual(b), or you can patch toEqual to check for JSX-ness.

@mjackson
Copy link
Owner

You can start like this:

expect.extend({
  toEqualJSX(jsx) {
    // this.actual is the thing that was passed to expect()
  }
})

expect(<div>hello</div>).toEqualJSX(<div>hello</div>)

@vvo
Copy link
Collaborator Author

vvo commented Oct 15, 2015

So I did:

And I get (mocha):
2015-10-16-002254_418x344_scrot

It was cool, hope this will be usable for a lot of other people. Since I have been testing with react shallow renderer, comparing using toEqualJSX is a joy.

We will write a blog post soon about all this.

@mjackson what do you think?

@rstacruz
Copy link
Collaborator

export default function toEqualJSX(ReactElement) {
  return expect(
    reactElementToJSXString(this.actual)
  ).toEqual(
    reactElementToJSXString(ReactElement)
  );
}

I like it.

@mjackson
Copy link
Owner

This looks great! Let's add something to the README about extensions that people are building.

One small nitpick is that people who aren't using ES6 will have to do:

var toEqualJSX = require('expect-to-equal-jsx');

expect.extend({
  toEqualJSX: toEqualJSX
});

It would be nice instead if your module just exported the object to pass directly to expect.extend, so people could do this:

expect.extend(require('expect-to-equal-jsx'));

@rstacruz
Copy link
Collaborator

👍

@vvo
Copy link
Collaborator Author

vvo commented Oct 16, 2015

@mjackson Yes will do, then will open a PR here and we can close this issue then :)

@redox
Copy link

redox commented Oct 16, 2015

👍

vvo pushed a commit to algolia/expect-jsx that referenced this issue Oct 19, 2015
BREAKING CHANGE: you now have to use the package like this:

```js
import expectJSX from 'expect-jsx';

expect.extend(expectJSX);
```

fixes #2
fixes #4
fixes #5
ref mjackson/expect#37 (comment)
fixes #6
@vvo
Copy link
Collaborator Author

vvo commented Oct 20, 2015

@vvo vvo closed this as completed Oct 20, 2015
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

4 participants