Skip to content

Commit 6b7f2ca

Browse files
authored
Merge pull request #2 from mmmurray/docs
Docs
2 parents e27e728 + 4555c7d commit 6b7f2ca

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

README.md

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,93 @@
1-
# jest-component-snapshot
1+
# jest-component-snapshot 📸
2+
3+
[![Travis](https://img.shields.io/travis/mmmurray/jest-component-snapshot.svg)](https://travis-ci.org/mmmurray/jest-component-snapshot)
4+
[![npm](https://img.shields.io/npm/v/jest-component-snapshot.svg)](https://www.npmjs.com/package/jest-component-snapshot)
5+
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
6+
7+
Component snapshot testing made easy.
8+
9+
## Install ⚙️
10+
11+
```bash
12+
yarn add -D jest-component-snapshot puppeteer
13+
```
14+
15+
## Usage 🤖
16+
17+
Update your Jest config to automatically setup and teardown Puppeteer:
18+
19+
```json
20+
{
21+
"globalSetup": "jest-component-snapshot/setup",
22+
"globalTeardown": "jest-component-snapshot/teardown",
23+
"setupFilesAfterEnv": ["jest-component-snapshot/extend-expect"]
24+
}
25+
```
26+
27+
### Image snapshot tests
28+
29+
Creates an image snapshot from a component using [jest-image-snapshot](https://www.npmjs.com/package/jest-image-snapshot). All of the same options are supported.
30+
31+
```js
32+
test('image snapshot from HTML string', () => {
33+
expect('<h1>Hello world</h1>').toMatchImageSnapshot()
34+
})
35+
36+
test('image snapshot from DOM element', () => {
37+
const headingElement = document.createElement('h1')
38+
headingElement.innerHtml = 'Hello world'
39+
40+
expect(headingElement).toMatchImageSnapshot()
41+
})
42+
43+
test('image snapshot from React element', () => {
44+
expect(<h1>Hello world</h1>).toMatchImageSnapshot()
45+
})
46+
```
47+
48+
### A11y snapshot tests
49+
50+
Uses Puppeteer to create an [accessibility tree snapshot](https://pptr.dev/#?product=Puppeteer&show=api-class-accessibility). The snapshot is converted to YAML for readability and empty properties and generic containers are removed.
51+
52+
```js
53+
test('a11y snapshot from HTML string', () => {
54+
expect('<h1>Hello world</h1>').toMatchA11ySnapshot()
55+
})
56+
57+
test('a11y snapshot from DOM element', () => {
58+
const headingElement = document.createElement('h1')
59+
headingElement.innerHtml = 'Hello world'
60+
61+
expect(headingElement).toMatchA11ySnapshot()
62+
})
63+
64+
test('a11y snapshot from React element', () => {
65+
expect(<h1>Hello world</h1>).toMatchA11ySnapshot()
66+
})
67+
```
68+
69+
### DOM snapshot tests
70+
71+
Snapshots formatted HTML for the given component.
72+
73+
```js
74+
test('DOM snapshot from HTML string', () => {
75+
expect('<h1>Hello world</h1>').toMatchDomSnapshot()
76+
})
77+
78+
test('DOM snapshot from DOM element', () => {
79+
const headingElement = document.createElement('h1')
80+
headingElement.innerHtml = 'Hello world'
81+
82+
expect(headingElement).toMatchDomSnapshot()
83+
})
84+
85+
test('DOM snapshot from React element', () => {
86+
expect(<h1>Hello world</h1>).toMatchDomSnapshot()
87+
})
88+
```
89+
90+
## Alternatives 🙌
91+
92+
- [jest-puppeteer](https://github.com/smooth-code/jest-puppeteer)
93+
- [jest-image-snapshot](https://www.npmjs.com/package/jest-image-snapshot)

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
{
22
"name": "jest-component-snapshot",
33
"version": "0.1.0",
4-
"description": "Component snapshot matchers",
4+
"description": "Component snapshot testing made easy.",
55
"author": "Mark Murray",
66
"license": "MIT",
77
"repository": "[email protected]:mmmurray/jest-component-snapshot.git",
88
"keywords": [
99
"jest",
1010
"component",
1111
"snapshot",
12+
"snapshot-testing",
13+
"jest-snapshots",
1214
"react",
1315
"dom",
1416
"a11y",

0 commit comments

Comments
 (0)