Code Monkey home page Code Monkey logo

jest-jsxstyle's Introduction

jest-jsxstyle

Jest utilities for jsxstyle

The problem

If you use jsxstyle 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="jsxstyle-0"
>
  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:

._15clmrq {
  color:red;
  display:block;
}

<h1
  class="_15clmrq"
>
  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.

Installation

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

npm install --save-dev jest-jsxstyle

Usage

At the top of your test file:

import serializer from 'jest-jsxstyle'

expect.addSnapshotSerializer(serializer)

Or in your Jest serializer config:

{
  "snapshotSerializers": [
    "jest-jsxstyle"
  ]
}

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(
      <Block color="orange">
        <Block margin="4rem">
          Hello World, this is my first jsxstyle component!
        </Block>
      </Block>
    )
    .toJSON()

  expect(tree).toMatchSnapshot()
})

Works with enzyme too:

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

test('enzyme', () => {
  const ui = (
    <Block color="orange">
      <Block margin="4rem">
        Hello World, this is my first jsxstyle component!
      </Block>
    </Block>
  )

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

Inspiration

LICENSE

MIT

jest-jsxstyle's People

Contributors

jaredpalmer avatar sgwilym avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

sgwilym chenxsan

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.