All Projects โ†’ kentcdodds โ†’ Jest Glamor React

kentcdodds / Jest Glamor React

Licence: mit
Jest utilities for Glamor and React

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Jest Glamor React

Arc
React starter kit based on Atomic Design
Stars: โœญ 2,780 (+2765.98%)
Mutual labels:  jest, css-in-js
Glamorous Primitives
๐Ÿ’„ style primitive React interfaces with glamorous
Stars: โœญ 91 (-6.19%)
Mutual labels:  css-in-js
Jest Marbles
Helpers library for marbles testing with Jest
Stars: โœญ 85 (-12.37%)
Mutual labels:  jest
Tyu
Unit test with no initial configuration.
Stars: โœญ 89 (-8.25%)
Mutual labels:  jest
Codecharta
CodeCharta visualizes multiple code metrics using 3D tree maps.
Stars: โœญ 85 (-12.37%)
Mutual labels:  jest
Nodejs Backend Architecture Typescript
Node.js Backend Architecture Typescript - Learn to build a backend server for Blogging platform like Medium, FreeCodeCamp, MindOrks, AfterAcademy - Learn to write unit and integration tests - Learn to use Docker image - Open-Source Project By AfterAcademy
Stars: โœญ 1,292 (+1231.96%)
Mutual labels:  jest
Reactjs Crud Boilerplate
Live Demo
Stars: โœญ 83 (-14.43%)
Mutual labels:  jest
Vue Mooc
๐Ÿ”ฐไฝฟ็”จVue3.0ๅ…จๅฎถๆกถ+TS+Jest้‡ๆž„ๆ…•่ฏพ็ฝ‘PC็ซฏ
Stars: โœญ 97 (+0%)
Mutual labels:  jest
Jest Allure
Generate Allure Report for jest. Allure Report, a flexible lightweight multi-language test report tool with the possibility to add steps, attachments, parameters and so on.
Stars: โœญ 90 (-7.22%)
Mutual labels:  jest
Jest Environment Jsdom Global
A Jest environment that allows you to configure jsdom
Stars: โœญ 89 (-8.25%)
Mutual labels:  jest
React Usestyles
๐Ÿ– Style components using React hooks. Abstracts the styling library away.
Stars: โœญ 89 (-8.25%)
Mutual labels:  css-in-js
Compiled
A familiar and performant compile time CSS-in-JS library for React.
Stars: โœญ 1,235 (+1173.2%)
Mutual labels:  css-in-js
Express App Testing Demo
a simple express app for demonstrating testing and code coverage
Stars: โœญ 89 (-8.25%)
Mutual labels:  jest
Nano Style
React functional CSS-in-JS
Stars: โœญ 85 (-12.37%)
Mutual labels:  css-in-js
Match Media
Universal polyfill for match media API using Expo APIs on mobile
Stars: โœญ 95 (-2.06%)
Mutual labels:  css-in-js
React Image Smooth Loading
[not maintained] Images which just flick to appear aren't cool. Images which appear smoothly with a fade like Instagram are cool
Stars: โœญ 84 (-13.4%)
Mutual labels:  css-in-js
React Boilerplate
This project is deprecated. Please use CRA instead.
Stars: โœญ 88 (-9.28%)
Mutual labels:  jest
Jest Webpack
Use jest with webpack.
Stars: โœญ 89 (-8.25%)
Mutual labels:  jest
Npm Registry Browser
Browse the npm registry with an SPA made in React, with full dev workflow.
Stars: โœญ 97 (+0%)
Mutual labels:  jest
Onno
Responsive style props for building themed design systems
Stars: โœญ 95 (-2.06%)
Mutual labels:  css-in-js

jest-glamor-react

Jest utilities for Glamor and React

Build Status Code Coverage version downloads MIT License

All Contributors PRs Welcome Donate Code of Conduct Roadmap Examples

Watch on GitHub Star on GitHub Tweet

Sponsor

The problem

If you use glamor as your CSS-in-JS solution, and you use snapshot testing with jest then you probably have some test snapshots that look like:

<h1
  class="css-1tnuino"
>
  Hello World
</h1>

And that's not super helpful from a styling perspective. Especially when there are changes to the class, you can see that it changed, but you have to look through the code to know what caused the class name to change.

This solution

This allows your snapshots to look more like:

.css-0,
[data-css-0] {
  font-size: 1.5em;
  text-align: center;
  color: palevioletred;
}

<h1
  class="css-0"
>
  Hello World
</h1>

This is much more helpful because now you can see the CSS applied and over time it becomes even more helpful to see how that changes over time.

This builds on the work from @MicheleBertoli in jest-styled-components to bring a similar experience to React projects that use glamor.

Table of Contents

Preview

Terminal Screenshot

Installation

This module is distributed via npm which is bundled with node and should be installed as one of your project's devDependencies:

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

Usage

At the top of your test file:

import serializer from 'jest-glamor-react'

expect.addSnapshotSerializer(serializer)

Or in your Jest serializer config:

{
  "snapshotSerializers": [
    "jest-glamor-react"
  ]
}

If you have set jest.config variable "testEnvironment": "node", you will need to manually mock up browser gloabl objects so it is recommended to use "testEnvironment": "jsdom" instead.

Here are some components:

import React from 'react'
import * as glamor from 'glamor'

function Wrapper(props) {
  const className = glamor.css({
    padding: '4em',
    background: 'papayawhip',
  })
  return <section className={`${className}`} {...props} />
}

function Title(props) {
  const className = glamor.css({
    fontSize: '1.5em',
    textAlign: 'center',
    color: 'palevioletred',
  })
  return <h1 className={`${className}`} {...props} />
}

Here's how we'd test them with ReactDOM.render:

import React from 'react'
import ReactDOM from 'react-dom'

function render(ui) {
  const div = document.createElement('div')
  ReactDOM.render(ui, div)
  return div.children[0]
}

test('react-dom', () => {
  const node = render(
    <Wrapper>
      <Title>Hello World, this is my first glamor styled component!</Title>
    </Wrapper>,
  )
  expect(node).toMatchSnapshot()
})

And here's how we'd test them with react-test-renderer:

import React from 'react'
import renderer from 'react-test-renderer'

test('react-test-renderer', () => {
  const tree = renderer
    .create(
      <Wrapper>
        <Title>Hello World, this is my first glamor styled component!</Title>
      </Wrapper>,
    )
    .toJSON()

  expect(tree).toMatchSnapshot()
})

Works with enzyme too:

import * as enzyme from 'enzyme'
import toJson from 'enzyme-to-json'

test('enzyme', () => {
  const ui = (
    <Wrapper>
      <Title>Hello World, this is my first glamor styled component!</Title>
    </Wrapper>
  )

  expect(toJson(enzyme.shallow(ui))).toMatchSnapshot(`enzyme.shallow`)
  expect(toJson(enzyme.mount(ui))).toMatchSnapshot(`enzyme.mount`)
  expect(toJson(enzyme.render(ui))).toMatchSnapshot(`enzyme.render`)
})

Custom matchers

toHaveStyleRule(property, value)

expect(node).toHaveStyleRule(property: string, value: string | RegExp)

Installation:

import serializer, {toHaveStyleRule} from 'jest-glamor-react'
expect.addSnapshotSerializer(serializer)
expect.extend({toHaveStyleRule})

Usage:

If we use the same examples as those above:

import React from 'react'
import ReactDOM from 'react-dom'

function render(ui) {
  const div = document.createElement('div')
  ReactDOM.render(ui, div)
  return div.children[0]
}

test('react-dom', () => {
  const node = render(
    <Wrapper>
      <Title>Hello World, this is my first glamor styled component!</Title>
    </Wrapper>,
  )
  expect(node).toHaveStyleRule('background', 'papayawhip')
})

Or with react-test-renderer:

import React from 'react'
import renderer from 'react-test-renderer'

test('react-test-renderer', () => {
  const tree = renderer
    .create(
      <Wrapper>
        <Title>Hello World, this is my first glamor styled component!</Title>
      </Wrapper>,
    )
    .toJSON()

  expect(tree).toHaveStyleRule('background', 'papayawhip')
})

Or using Enzyme:

import {mount} from 'enzyme'

test('enzyme', () => {
  const wrapper = mount(
    <Wrapper>
      <Title>Hello World, this is my first glamor styled component!</Title>
    </Wrapper>,
  )

  expect(wrapper).toHaveStyleRule('background', 'papayawhip')
  expect(wrapper.find(Title)).toHaveStyleRule('color', 'palevioletred')
})

Integration with snapshot-diff

snapshot-diff is this really neat project that can help you get more value out of your snapshots. As far as I know, this is the best example of how to integrate this serializer with that handy matcher:

import React from 'react'
import ReactDOM from 'react-dom'
import {Simulate} from 'react-dom/test-utils'
import * as glamor from 'glamor'
import {toMatchDiffSnapshot, getSnapshotDiffSerializer} from 'snapshot-diff'
import serializer, {fromDOMNode} from 'jest-glamor-react'

expect.addSnapshotSerializer(getSnapshotDiffSerializer())
expect.addSnapshotSerializer(serializer)
expect.extend({toMatchDiffSnapshot})

function Button({count, ...props}) {
  const className = glamor.css({margin: 10 + count})
  return <button className={`${className}`} {...props} />
}

class Counter extends React.Component {
  state = {count: 0}
  increment = () => {
    this.setState(({count}) => ({count: count + 1}))
  }
  render() {
    const {count} = this.state
    return (
      <Button onClick={this.increment} count={count}>
        {count}
      </Button>
    )
  }
}

test('snapshot diff works', () => {
  const control = render(<Counter />)
  const variable = render(<Counter />)
  Simulate.click(variable)
  expect(fromDOMNode(control)).toMatchDiffSnapshot(fromDOMNode(variable))
})

function render(ui) {
  const div = document.createElement('div')
  ReactDOM.render(ui, div)
  return div.children[0]
}

The result of this snapshot is:

Snapshot Diff:
- First value
+ Second value

  .css-0,
  [data-css-0] {
-   margin: 10px;
+   margin: 11px;
  }

- <button class="css-0">0</button>
+ <button class="css-0">1</button>

Pretty handy right?!

Notice the fromHTMLString function you can import from jest-glamor-react. That's what jest-glamor-react uses internally if you try to snapshot a string that looks like HTML and includes css- in it. I can't think of any other context where it would be useful, so it's not documented beyond this example.

Inspiration

As mentioned earlier, @MicheleBertoli's jest-styled-components was a huge inspiration for this project. And much of the original code came from from that MIT Licensed project. Thank you so much Michele! ๐Ÿ‘

Other Solutions

I'm unaware of other solutions. Please file a PR if you know of any!

Contributors

Thanks goes to these people (emoji key):


Michele Bertoli

๐Ÿ’ป ๐Ÿ“– โš ๏ธ

Kent C. Dodds

๐Ÿ’ป ๐Ÿ“– ๐Ÿš‡ โš ๏ธ

Mitchell Hamilton

๐Ÿ’ป ๐Ÿ“– โš ๏ธ

jhurley23

๐Ÿ’ป โš ๏ธ ๐Ÿ“–

Gaurav Talwar


Henry Lewis

๐Ÿ› ๐Ÿ’ป

Alexey Svetliakov

๐Ÿ’ป โš ๏ธ

James W Lane

๐Ÿ› ๐Ÿ’ป โš ๏ธ

Brent Ertz

๐Ÿ“–

Junyoung Clare Jang

๐Ÿ’ป โš ๏ธ

This project follows the all-contributors specification. Contributions of any kind welcome!

LICENSE

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].