|
1 | | -# jest-component-snapshot |
| 1 | +# jest-component-snapshot 📸 |
| 2 | + |
| 3 | +[](https://travis-ci.org/mmmurray/jest-component-snapshot) |
| 4 | +[](https://www.npmjs.com/package/jest-component-snapshot) |
| 5 | +[](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) |
0 commit comments