Skip to content

Instantly share code, notes, and snippets.

@thevangelist
Created August 4, 2016 13:06
Show Gist options
  • Save thevangelist/e2002bc6b9834def92d46e4d92f15874 to your computer and use it in GitHub Desktop.
Save thevangelist/e2002bc6b9834def92d46e4d92f15874 to your computer and use it in GitHub Desktop.
The only React.js component test you'll ever need (Enzyme + Chai)
import React from 'react';
import { shallow } from 'enzyme';
import MyComponent from '../src/my-component';
const wrapper = shallow(<MyComponent/>);
describe('(Component) MyComponent', () => {
it('renders without exploding', () => {
expect(wrapper).to.have.length(1);
});
});
@kristremblay
Copy link

kristremblay commented Apr 8, 2018

For those of us using Jest, you would check the length with expect(wrapper).toHaveLength(1)

@jacobweber
Copy link

@chiedo Does it do that? I'm not seeing where this happens.

@yagudaev
Copy link

yagudaev commented Aug 8, 2018

For those in 2018 using create-react-app: https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#testing-components

tl;dr; use:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

it('renders without crashing', () => {
  const div = document.createElement('div');
  ReactDOM.render(<App />, div);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment