emotion/docs/testing.mdx
Sam Magura 4003819a2a
Upgrade to latest Next.js (attempt 2) (#2805)
* Remove old site, create new Next app

* Site header

* Fix manypkg issues, copy in bootstrap-reboot.min.css

* Website progress

* Website progress

* Website progress

* yarn.lock

* Website progress

* Fix manypkg errors

* Website progress

* Website progress: markdown CSS

* Website progress

* Website progress

* Remove spectrum badge from README

* Add Carbon ads

* Add DocSearch

* Add favicon and set page <title>

* Website tweaks

* Update Netlify config and use next-export

* Remove unnecessary dependencies from root package.json

* Add custom 404 page

* Live editor works (no Babel yet)

* [wip] Babel compilation

* About to remove web worker stuff

* Website: Live code editor stuff

* Remove Mac-specific step from CONTRIBUTING

* Update CONTRIBUTING for new website development

* Done with live code editors

* Bring awesome-emotion into repo as an MDX doc

* Add redirects to netlify.toml

* Website styling, remove @jsx pragma from live code examples

* Remove incorrect slack link from community.mdx

* Review & tweak website styles

* Make tables responsive on mobile

* Set up @next/bundle-analyzer

* Get @emotion/babel-plugin to work in the browser (HACKS)

* Change Carbon ad when path changes

* Fix live editor issues

* Do LiveEditor compilation in web worker

* Fork react-live

* Working on live editors

* Fix LiveProvider useEffect

* Live editor tweaks

* Fix web worker @emotion/babel-plugin import

* Add website TS TODO

* Add more detail to comment

* Fix some but not all manypkg issues

* Fix Safari-specific styling issues

* Website: Use webpack alias for @emotion/babel-plugin

* Add comment explaining module aliasing hack

* site: Upgrade docsearch

* site: Upgrade some packages

* site: Upgrade some packages

* site: Upgrade next-mdx-remote

* Fix layout shift when carbon ads is loading

* Make CI use Node 14

This is required for next-mdx-remote v4

* Remove .nvmrc to fix Netlify build

* Add Ukraine banner to new website

* Fix minor issues in styled.mdx

* Minor mobile fixes

* Convert GFM tables to HTML so we don't need the remark-gfm plugin

* Upgrade all website packages

* Update docsearch appId and apiKey (see #2718)

* Fixed CacheProvider demo in the docs (see #2678)

* CodeSandbox CI node 16

* Simplify .gitignore

* Website cleanup after reviewing diff

* Fix multiple @types/react

* Add test:typescript script to emotion-site

* Make all code blocks have the same font-size

* Autolink headings

* Minor website fixes

* remark-responsive-tables
2022-07-13 09:48:27 -04:00

95 lines
2.3 KiB
Plaintext

---
title: 'Snapshot Testing'
---
Adding [snapshot tests with Jest](https://facebook.github.io/jest/docs/en/snapshot-testing.html) is a great way to help avoid unintended changes to your app's UI.
By diffing the serialized value of your React tree Jest can show you what changed in your app and allow you to fix it or update the snapshot.
By default snapshots with emotion show generated class names. Adding [@emotion/jest](https://github.com/emotion-js/emotion/tree/main/packages/jest) allows you to output the actual styles being applied.
<img src="https://user-images.githubusercontent.com/514026/31314015-02b79ca6-abc3-11e7-8f70-1edb31c7f43b.jpg" />
### Installation
```bash
npm install --save-dev @emotion/jest
```
Add the `"@emotion/jest/serializer"` to the [`snapshotSerializers`](https://jestjs.io/docs/en/configuration#snapshotserializers-arraystring) option.
```json
{
"snapshotSerializers": ["@emotion/jest/serializer"]
}
```
Or use `expect.addSnapshotSerializer` to add it.
```javascript
import { createSerializer } from '@emotion/jest'
expect.addSnapshotSerializer(createSerializer())
```
When using Enzyme, you can add `"@emotion/jest/enzyme-serializer"` instead.
```json
{
"snapshotSerializers": ["@emotion/jest/enzyme-serializer"]
}
```
Or use `expect.addSnapshotSerializer` to add it like this:
```javascript
// also adds the enzyme-to-json serializer
import { createEnzymeSerializer } from '@emotion/jest/enzyme-serializer'
expect.addSnapshotSerializer(createEnzymeSerializer())
```
### Writing a test
Writing a test with `@emotion/jest` involves creating a snapshot from the `react-test-renderer` or `enzyme-to-json`'s resulting JSON.
```jsx
import React from 'react'
import renderer from 'react-test-renderer'
const Button = props => (
<button
css={{
color: 'hotpink'
}}
{...props}
/>
)
test('Button renders correctly', () => {
expect(
renderer.create(<Button>This is hotpink.</Button>).toJSON()
).toMatchSnapshot()
})
```
It'll create a snapshot that looks like this.
```jsx
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Button renders correctly 1`] = `
.emotion-0 {
color: hotpink;
}
<div
className="emotion-0"
>
This is hotpink.
</div>
`
```
When the styles of a component change, the snapshot will fail and you'll be able to update the snapshot or fix the component.