Need help with expect-jsx?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

algolia
411 Stars 25 Forks MIT License 215 Commits 8 Opened issues

Description

✅ toEqualJSX for expect assertion library

Services available

!
?

Need anything else?

Contributors list

Note from maintainers Since Jest supports snapshot testing, we recommend you to use that instead of

expect-jsx
.

expect-jsx
is no more compatible with recent Jest versions (22), it can be made compatible so feel free to open a PR if so.

expect-jsx

Version Build Status License Downloads

toEqualJSX for mjackson/expect.

It uses algolia/react-element-to-jsx-string in the background to turn React elements into formatted strings.

Table of Contents generated with DocToc

Setup

You will most probably use this plugin as a development dependency.

yarn add expect-jsx --dev

API

  • expect(ReactComponent|JSX).toEqualJSX(ReactComponent|JSX)
  • expect(ReactComponent|JSX).toNotEqualJSX(ReactComponent|JSX)
  • expect(ReactComponent|JSX).toIncludeJSX(ReactComponent|JSX)
  • expect(ReactComponent|JSX).toNotIncludeJSX(ReactComponent|JSX)

Usage

Here's an example using mochajs/mocha.

import React from 'react';
import expect from 'expect';
import expectJSX from 'expect-jsx';

expect.extend(expectJSX);

class TestComponent extends React.Component {}

describe('expect-jsx', () => { it('works', () => { expect(

).toEqualJSX(
); // ok

expect(<div a="1" b="2"></div>).toEqualJSX(<div></div>);
// Error: Expected '<div a="1" b="2"></div>' to equal '<div></div>'

expect(<span></span>).toNotEqualJSX(<div></div>);
// ok

expect(<div><testcomponent></testcomponent></div>).toIncludeJSX(<testcomponent></testcomponent>);
// ok

}); });

It looks like this when ran:

Screenshot when using mocha

A note about functions

toEqualJSX
will not check for function references, it only checks that if a
function
was expected somewhere, there's also a function in the actual data.

It's your responsibility to then unit test those functions.

Environment requirements

The environment you use to use

react-element-to-jsx-string
should have ES2015 support.

Use the Babel polyfill or any other method that will make you environment behave like an ES2015 environment.

Test

yarn test
yarn test:watch

Build

yarn build
yarn build:watch

Release

Decide if this is a

patch
,
minor
or
major
release, look at http://semver.org/
yarn release [major|minor|patch|x.x.x]

Similar projects

There are multiple similar projects for other assertions libraries, all based on algolia/react-element-to-jsx-string. For instance:

Thanks

To the people pointing me in the right directions like: - https://github.com/facebook/react/issues/4835 - https://github.com/mjackson/expect/issues/37

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.