Coder Social home page Coder Social logo

mmmurray / jest-component-snapshot Goto Github PK

View Code? Open in Web Editor NEW
7.0 1.0 0.0 1.69 MB

Component snapshot testing made easy.

License: MIT License

JavaScript 2.88% TypeScript 97.12%
jest snapshot snapshot-testing jest-snapshots react a11y a11y-testing puppeteer typescript

jest-component-snapshot's Introduction

jest-component-snapshot ๐Ÿ“ธ

Travis npm License: MIT

Component snapshot testing made easy.

Install โš™๏ธ

yarn add -D jest-component-snapshot puppeteer

Usage ๐Ÿค–

The recommended way to use this library is by adding the following to your Jest config:

{
  "globalSetup": "jest-component-snapshot/setup",
  "globalTeardown": "jest-component-snapshot/teardown",
  "setupFilesAfterEnv": ["jest-component-snapshot/extend-expect"]
}

This config is optional - the globalSetup and globalTeardown functions ensure that the same instance of Puppeteer is shared between tests to improve performance. If you do not set these options then a new instance of Puppeteer will be launched for each test.

If you cannot configure setupFilesAfterEnv (such as in create-react-app), you can manually extend Jest in your test files using:

import { extendExpect } from 'jest-component-snapshot'

extendExpect()

Image snapshot tests

Creates an image snapshot from a component using jest-image-snapshot. All of the same options are supported.

test('image snapshot from HTML string', () => {
  expect('<h1>Hello world</h1>').toMatchImageSnapshot()
})

test('image snapshot from DOM element', () => {
  const headingElement = document.createElement('h1')
  headingElement.innerHtml = 'Hello world'

  expect(headingElement).toMatchImageSnapshot()
})

test('image snapshot from React element', () => {
  expect(<h1>Hello world</h1>).toMatchImageSnapshot()
})

A11y snapshot tests

Uses Puppeteer to create an accessibility tree snapshot. The snapshot is converted to YAML for readability and empty properties and generic containers are removed.

test('a11y snapshot from HTML string', () => {
  expect('<h1>Hello world</h1>').toMatchA11ySnapshot()
})

test('a11y snapshot from DOM element', () => {
  const headingElement = document.createElement('h1')
  headingElement.innerHtml = 'Hello world'

  expect(headingElement).toMatchA11ySnapshot()
})

test('a11y snapshot from React element', () => {
  expect(<h1>Hello world</h1>).toMatchA11ySnapshot()
})

DOM snapshot tests

Snapshots formatted HTML for the given component.

test('DOM snapshot from HTML string', () => {
  expect('<h1>Hello world</h1>').toMatchDomSnapshot()
})

test('DOM snapshot from DOM element', () => {
  const headingElement = document.createElement('h1')
  headingElement.innerHtml = 'Hello world'

  expect(headingElement).toMatchDomSnapshot()
})

test('DOM snapshot from React element', () => {
  expect(<h1>Hello world</h1>).toMatchDomSnapshot()
})

Alternatives ๐Ÿ™Œ

jest-component-snapshot's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar mmmurray avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.