An open API service indexing awesome lists of open source software.

https://github.com/iamogbz/jest-react-snapshot

📷 Snapshot react components as images in jest tests
https://github.com/iamogbz/jest-react-snapshot

dom-image image jest node nodejs react snapshot testing typescript

Last synced: 3 months ago
JSON representation

📷 Snapshot react components as images in jest tests

Awesome Lists containing this project

README

          

# Jest React Snapshot

logo

[![Build Status](https://github.com/iamogbz/jest-react-snapshot/workflows/Build/badge.svg)](https://github.com/iamogbz/jest-react-snapshot/actions)
[![Coverage Status](https://coveralls.io/repos/github/iamogbz/jest-react-snapshot/badge.svg?branch=refs/heads/master)](https://coveralls.io/github/iamogbz/jest-react-snapshot?branch=refs/heads/main)
[![NPM Version](https://img.shields.io/npm/v/jest-react-snapshot.svg)](https://www.npmjs.com/package/jest-react-snapshot)

> Snapshot react components as images in jest tests

## Usage

```typescript
import { imageToMatchSnapshot } from "jest-react-snapshot";

expect.extend({ imageToMatchSnapshot });

it("renders component matching snapshot", async (): Promise => {
await expect().imageToMatchSnapshot();
});
```

See [repo test](./tests/index.test.tsx) for more details.

![image-to-match-snapshot](https://raw.githubusercontent.com/iamogbz/jest-react-snapshot/main/tests/__image_snapshots__/index-test-tsx-image-to-match-snapshot-renders-component-as-image-and-matches-snapshot-3-snap.png)

### Advanced Usage

Uses [`jest-image-snapshot`](https://github.com/americanexpress/jest-image-snapshot) to power the image snapshot and diffing functionality.

Supports the `toMatchImageSnapshot` [API](https://github.com/americanexpress/jest-image-snapshot#%EF%B8%8F-api), providing some default configuration.

```typescript
await expect().imageToMatchSnapshot(customOptions);
```

### Test Environment Setup

Easiest way is to use [`jest-puppeeter`](https://github.com/smooth-code/jest-puppeteer) which provides sensible defaults for your testing environment.

#### Example Jest Config

```json
"jest": {
"preset": "jest-puppeteer",
"moduleDirectories": [
"./src",
"./tests",
"./node_modules"
],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testPathIgnorePatterns": [
"./artifacts/",
"./node_modules/"
]
}
```

## Further Work

- Replace `puppeteer` with lighter renderer as only the `HTML` and `CSS` layout engine is used.