Package detail

expect-jsx

algolia5.5kMIT5.0.0

toEqualJSX for mjackson/expect

expect, expect-extension, jsx, string

readme

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(<div />).toEqualJSX(<div />);
    // ok

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

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

    expect(<div><TestComponent /></div>).toIncludeJSX(<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:

  • chai-equal-jsx, assertions for chai: expect(<div />).to.equalJSX(<div />);
  • chai-jsx, assertions for chai: expect(<div />).jsx.to.equal(<div />);
  • jsx-chai, assertions for chai: expect(<div />).to.deep.equal(<div />);
  • tape-jsx-equals, assertions for tape: t.jsxEquals(<div />, <div />);
  • jasmine-expect-jsx, assertions for jasmine: expect(<div />).toEqualJSX(<div />);

Thanks

To the people pointing me in the right directions like:

changelog

5.0.0 (2017-11-07)

4.0.0 (2017-09-18)

3.0.0 (2017-01-03)

Chores

  • lib: upgrade to react-element-to-jsx-string latest (a014672)

BREAKING CHANGES

  • lib: You need an ES2015 env to use expect-jsx now

You can use the Babel polyfill to do so.

2.6.0 (2016-06-06)

2.5.1 (2016-04-15)

Bug Fixes

2.5.0 (2016-04-15)

Features

  • React: React v15 stable compat (7557e63)

2.4.0 (2016-03-09)

2.3.0 (2016-02-02)

2.2.2 (2016-01-05)

2.2.1 (2015-12-10)

Bug Fixes

  • dep: update underlying react-element-to-jsx-string to 2.1.4 (cd963cf)

2.2.0 (2015-12-08)

Bug Fixes

  • handle {number} and {false} {true} cases (0a4ccf5)
  • stop replacing with <whitespace> (b109405)
  • to-jsx: upgrade react-element-to-jsx-string, provide Array.fill polyfill (19ddde5)

2.1.0 (2015-10-25)

Features

2.0.1 (2015-10-21)

Bug Fixes

  • handle inline props string replacement (5e89032)

2.0.0 (2015-10-19)

Bug Fixes

  • change package name to expect-jsx (665ed66), closes #3

Features

  • add toNotEqualJSX, toIncludeJSX, change default export (6402777), closes #2 #4 #5 #6

BREAKING CHANGES

  • you now have to use the package like this:
import expectJSX from 'expect-jsx';

expect.extend(expectJSX);
  • package is now installed with npm install expect-jsx --save-dev

1.1.4 (2015-10-16)

Bug Fixes

  • handle high order components (~decorators) name (96f13af)

1.1.3 (2015-10-16)

Bug Fixes

  • handle props.children.length > 1 + array values (c86a9a4)

1.1.2 (2015-10-16)

Bug Fixes

  • handle props.children.length > 1 (a76b7c2)

1.1.1 (2015-10-16)

Bug Fixes

1.1.0 (2015-10-16)

Features

  • deeply set functions and React elements now well inlined (8424e21)

1.0.3 (2015-10-16)

Bug Fixes

  • handle null and undefined prop values (33b8fca)

1.0.2 (2015-10-15)

Bug Fixes

  • deps: lower down the peer dep requirements (5d526d5)

1.0.1 (2015-10-15)

Bug Fixes

  • upgrade react-element-to-jsx-string (whitespace, sort keys) (df977b4)