Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jest-glamor-react docs #390

Merged
merged 4 commits into from
Oct 8, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions docs/testing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
## 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 [jest-glamor-react](https://github.com/kentcdodds/jest-glamor-react) allows you to output the actual styles being applied.

<img height="360px" src="https://user-images.githubusercontent.com/514026/31314015-02b79ca6-abc3-11e7-8f70-1edb31c7f43b.jpg"/>


### Installation

```bash
npm install --save-dev jest-glamor-react
```

**testSetup.js** _or_ at the top of your test file

```javascript
import { sheet } from 'emotion'
import serializer from 'jest-glamor-react'

expect.addSnapshotSerializer(serializer(sheet))
```

**package.json**

```json
"jest": {
[...]
"setupTestFrameworkScriptFile": "<rootDir>/testSetup.js",
"testEnvironment": "jsdom"
[...]
}
```

### Adding a test

```javascript
import React from 'react';
import renderer from 'react-test-renderer';
import App from './App';

test('Link renders correctly', () => {
const tree = renderer.create(<App />).toJSON();
expect(tree).toMatchSnapshot();
});
```

### Notes
It's recommended to set your Jest `testEnvironment` to `jsdom`, but you can mock global browser objects instead.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't checked, does manually mocking global browser objects work with jest-glamor-react?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally just set my env to jsdom, but the docs say it does. https://github.com/kentcdodds/jest-glamor-react#usage

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok 👍


Your snapshot class names will now appear as `glamor-[0...n]`