All Projects โ†’ denisraslov โ†’ snappify

denisraslov / snappify

Licence: MIT License
A generator of Jest snapshot-based tests for React components written with TypeScript ๐Ÿ“ธ

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to snappify

Enzyme
JavaScript Testing utilities for React
Stars: โœญ 19,781 (+30807.81%)
Mutual labels:  jest, react-components
testing-reactjs-examples
๐Ÿงช "What should we test in our React components" - presentation examples.
Stars: โœญ 23 (-64.06%)
Mutual labels:  jest, jest-snapshots
jest-playground
Playing around with Jest - Subscribe to my YouTube channel: https://bit.ly/CognitiveSurge
Stars: โœญ 24 (-62.5%)
Mutual labels:  jest, jest-snapshots
nuxt-boilerplate
Nuxt.js Boilerplate
Stars: โœญ 25 (-60.94%)
Mutual labels:  jest, jest-snapshots
Jest Image Snapshot
โœจ Jest matcher for image comparisons. Most commonly used for visual regression testing.
Stars: โœญ 2,940 (+4493.75%)
Mutual labels:  jest, jest-snapshots
angularjs-jest
Testing AngularJS applications with Jest
Stars: โœญ 24 (-62.5%)
Mutual labels:  jest, jest-snapshots
jest-extended-snapshot
Additional Jest matchers for snapshot testing.
Stars: โœญ 18 (-71.87%)
Mutual labels:  jest, jest-snapshots
sqrs
๐ŸšŒSQRS is a JavaScript library for implementing CQRS pattern.
Stars: โœญ 23 (-64.06%)
Mutual labels:  jest
ng-mono-repo-starter
Angular Mono Repo Starter
Stars: โœญ 79 (+23.44%)
Mutual labels:  jest
license-auditor
License Auditor helps you track and validate licenses inside your project.
Stars: โœญ 15 (-76.56%)
Mutual labels:  jest
vscode-react-javascript-snippets
Extension for React/Javascript snippets with search supporting ES7+ and babel features
Stars: โœญ 782 (+1121.88%)
Mutual labels:  react-components
react-components
React components library.
Stars: โœญ 27 (-57.81%)
Mutual labels:  react-components
angular-unit-testing-examples
Showroom for different Angular unit testing concepts
Stars: โœญ 19 (-70.31%)
Mutual labels:  jest
jest-file-snapshot
Jest matcher to write snapshots to a separate file instead of the default snapshot file used by Jest
Stars: โœญ 35 (-45.31%)
Mutual labels:  jest
react-multi-context
Manage multiple React 16 contexts with a single component.
Stars: โœญ 19 (-70.31%)
Mutual labels:  jest
material-react-components
React components implementing the Material Design specification
Stars: โœญ 21 (-67.19%)
Mutual labels:  react-components
nextjs-typescript-graphql-starter
A Next.js starter with Typescript, Jest, GraphQL Apollo Server & Apollo Client v3 with graphql-codegen.
Stars: โœญ 15 (-76.56%)
Mutual labels:  jest
jest-matchmedia-mock
๐Ÿƒ Mock for fully testing any media queries with Jest
Stars: โœญ 35 (-45.31%)
Mutual labels:  jest
enzyme-extensions
๐ŸŽฉ Enzyme extensions to test shallowly rendered enzyme wrappers ๐Ÿ„๐Ÿป
Stars: โœญ 30 (-53.12%)
Mutual labels:  jest
Fable.SemanticUI
React.SeamanticUI to Fable bindings
Stars: โœญ 15 (-76.56%)
Mutual labels:  react-components

Snappify
Snappify logo
React + Jest + TypeScript

A generator of Jest snapshot-based tests for React components written with TypeScript

Why

Snapshot testing of React components is a useful tool that Jest provides to make sure your UI does not change unexpectedly. To apply it you have to describe several different states of a component configuring it with different props that this component can have.

However, if you write components with TypeScript, you have an interface describing the types of the props. There is no reason to fill props up manually with random values in your tests. It could be done automatically based on the described types. That is what Snappify does.

Snappify generates files with tests for your React components. You just run them. โœจ

A related blog post

Quick Overview

Install Snappify globally:

npm install -g snappify

Run Snappify inside of a project folder with a configured glob pattern of the React components files and a name of a root folder for generated tests:

snappify components/**/*.tsx --testsRoot=tests

It will create a directory called tests inside the current folder.

Inside that directory, it will generate the files with snapshot-based tests for every of the components from the files that matched to components/**/*.tsx pattern.

Folders structure

The structure of the included folders with tests will be the same as the structure of the folders with components. The names of the files with tests will be the same as the names of the files with the components.

For example, if you have this structure of folders:

my-app
โ”œโ”€โ”€ components
โ”‚   โ””โ”€โ”€ Header.tsx
โ”‚   โ””โ”€โ”€ Content.tsx
โ”‚   โ””โ”€โ”€ Footer.tsx
โ”‚   โ””โ”€โ”€ Button
โ”‚       โ””โ”€โ”€ index.tsx

You will get this structure of the folders with the tests (when you run the command above):

my-app
โ”œโ”€โ”€ tests
โ”‚   โ””โ”€โ”€ Header.js
โ”‚   โ””โ”€โ”€ Content.js
โ”‚   โ””โ”€โ”€ Footer.js
โ”‚   โ””โ”€โ”€ Button
โ”‚       โ””โ”€โ”€ index.js

How it works

Snappify takes a component and its interface, and generates a file with snapshot-based tests with up to 10 test cases each.

The props values for test cases be generated with uniformly distributed sets of random values based on the types of the props. That means you get the count of test cases that you can take control of, but they are still cover the details of your components as wide as it possible in this situation.

An example

Here we have the Button component that have the declared interface for its props called IButtonProps.

import * as React from 'react';

interface IButtonProps {
    children: React.ReactNode;
    className?: string;
    isDisabled?: boolean;
    onClick?: () => void;
}

const Button: React.StatelessComponent<IButtonProps> = (props) => {
    const { className, isDisabled, onClick } = props;

    return (
        <div className={className} onClick={!isDisabled && onClick}>
            {props.children}
        </div>
    );
};

export default Button;

Snappify will generate a file with tests for this component that will looks like:

import React from 'react';
import renderer from 'react-test-renderer';

import Button from 'components/Button.tsx';

test('Button case #1', () => {
    const tree = renderer.create(
        <Button
            // some randomly generated values for props
            className={'value'}
            isDisabled={true}
            onClick={() => undefined}
            children={<div />}
        />
    ).toJSON();

    expect(tree).toMatchSnapshot();
});

test('Button case #2', () => {
    const tree = renderer.create(
        <Button
            // another set of randomly generated values for props
            // (the not required props were skipped)
            children={<div />}
        />
    ).toJSON();

    expect(tree).toMatchSnapshot();
});

// Other test cases go here...

Supported TypeScript types

Right now Snappify supports the basic types of TypeScript. It also supports a few of the React types: React.ReactNode, JSX.Element and React.CSSProperties.

It doesn't support using other declared TypeScript interfaces as types of items of a React component's interface yet.

I condiser increase of the supported types as a future improvement.

Supported TypeScript syntax

Right now Snappify supports only the semicolon (;) as a delimiter between interface items. See example:

interface IButtonProps {
    children: React.ReactNode;
    className?: string;
}

It also supports only React components declared with one of the following statements:

class Button extends React.Component<IButtonProps, IButtonState> {}
class Button extends React.PureComponent<IButtonProps, IButtonState> {}
const Button:React.StatelessComponent<IButtonProps> = (props) => {}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].