Skip to content

Instantly share code, notes, and snippets.

View dferber90's full-sized avatar

Dominik Ferber dferber90

View GitHub Profile
@dferber90
dferber90 / visual-regression-testing.md
Last active July 2, 2023 08:45
Visual Regression Testing in Jest

Visual Regression Testing with Jest

This is a walkthrough of how to set up Visual Regression Testing with Jest for an application created with create-react-app.

The following walkthrough uses React as an example, but the approach should work for any modern frontend library! I assume it can be used with Angular, Vue, Cycle.js and more.

This gist walks you through a create-react-app application as an example of how to set up Visual Regression Testing in Jest using libraries I wrote recently which enable this: jsdom-screenshot, jest-transform-css and jest-transform-file.

@dferber90
dferber90 / render-prop-centipede.jsx
Last active May 25, 2018 07:21
Test a Render Prop!
describe('when <SuperApp /> renders', () => {
const wrapper = shallow(<SuperApp />)
.find(Mouse)
.renderProp('render', { x: 0, y: 0})
.find(MouseDown)
.renderProp('render', { down: true });
it('should render a heading containing the coordinates', () => {
expect(
wrapper.equals(
@dferber90
dferber90 / traditional-approach.jsx
Created May 18, 2018 05:00
Test a Render Prop!
describe('when <App /> renders nested render props', () => {
const wrapper = shallow(<SuperApp />);
const mouseWrapper = shallow(
<div>{wrapper.find(Mouse).prop('render')({ x: 0, y: 0 })}</div>
);
const mouseDownWrapper = shallow(
mouseWrapper.find(MouseDown).prop('render')({ down: true })
);
it('should render a string containing the coordinates', () => {
@dferber90
dferber90 / nested-components.jsx
Last active May 25, 2018 07:18
Test a Render Prop!
const SuperApp = () => (
<div style={{ height: '100%' }}>
<Mouse
render={({ x, y }) => (
<MouseDown
render={({ down }) => (
<h1>
The mouse position is ({x}, {y})
and the button is {down ? 'down' : 'up'}
</h1>
@dferber90
dferber90 / the-light.jsx
Last active May 25, 2018 15:14
Test a Render Prop!
describe('when <App /> renders', () => {
const wrapper = shallow(<App />)
.find(Mouse)
.renderProp('render', { x: 0, y: 0 });
it('should render a headline containing the coordinates', () => {
expect(wrapper.equals(<h1>The mouse position is (0, 0)</h1>)).toBe(true);
});
});
@dferber90
dferber90 / two-wrappers.jsx
Last active May 25, 2018 07:08
Test a Render Prop!
describe('when <App /> renders', () => {
const wrapper = shallow(<App />);
const mouseWrapper = shallow(
wrapper.find(Mouse).prop('render')({ x: 0, y: 0 })
);
it('should render a string containing the coordinates', () => {
expect(mouseWrapper.equals(<h1>The mouse position is (0, 0)</h1>)).toBe(
true
);
@dferber90
dferber90 / dumb-approach.js
Created May 18, 2018 04:55
Test a Render Prop!
describe('when <App /> renders', () => {
const wrapper = shallow(<App />);
it('should render a string containing the coordinates', () => {
expect(wrapper.text()).toEqual('The mouse position is (0, 0)');
});
});
@dferber90
dferber90 / render-prop-test.jsx
Created May 18, 2018 04:53
Test a Render Prop!
import { shallow } from 'enzyme'
describe('when the mouse moves', () => {
const renderMock = jest.fn();
const wrapper = shallow(<Mouse render={renderMock} />);
wrapper.find('div').simulate('mousemove', {
clientX: 50,
clientY: 20,
});
@dferber90
dferber90 / michaels-app.jsx
Last active May 25, 2018 07:03
Test a Render Prop
import React from 'react';
import PropTypes from 'prop-types';
// Instead of using a HOC, we can share code using a
// regular component with a render prop!
class Mouse extends React.Component {
static propTypes = {
render: PropTypes.func.isRequired,
};
state = { x: 0, y: 0 };
@dferber90
dferber90 / .eslintrc.yml
Last active June 21, 2019 13:27
Example setup of ESLint-plugin-Meteor with AirBnB's code style
# enable ES6
parserOptions:
ecmaVersion: 6
sourceType: "module"
ecmaFeatures:
jsx: true # enable React's JSX
# register plugins
plugins:
- meteor