Coder Social home page Coder Social logo

jaredlunde / jest-stitches Goto Github PK

View Code? Open in Web Editor NEW
16.0 3.0 1.0 178 KB

Jest utilities for Stitches

Home Page: https://npmjs.com/package/jest-stitches

License: MIT License

JavaScript 100.00%
stitches stitches-utils jest-snapshots jest-matchers jest

jest-stitches's Introduction


jest-stitches


Jest utilities for Stitches
npm i -D jest-stitches

Code coverage Build status NPM Version MIT License

Stitches snapshots

The easiest way to test React, Preact, and Preact X components with Stitches is using the snapshot serializer. You can register the serializer via the snapshotSerializers configuration property in your jest configuration like so:

// jest.config.js
module.exports = {
  // ... other config
  snapshotSerializers: ['jest-stitches'],
}

Or you can customize the serializer via the createSerializer method like so: (the example below is with react-test-renderer but jest-stitches also works with enzyme and react-testing-library)

import React from 'react'
import renderer from 'react-test-renderer'
import {createStyled} from '@stitches/react'
import serializer from 'jest-stitches'

const {styled, css} = createStyled({})

expect.addSnapshotSerializer(serializer)

test('renders with correct styles', () => {
  const Button = styled('button', {
    variants: {
      blue: {
        backgroundColor: 'blue',
      },
    },
  })

  const tree = renderer.create(<Button>Hello world</Button>).toJSON()

  expect(tree).toMatchSnapshot()
})

Options

DOMElements

jest-stitches's snapshot serializer inserts styles and replaces class names in both React and DOM elements. If you would like to disable this behavior for DOM elements, you can do so by passing { DOMElements: false }. For example:

import {createSerializer} from 'jest-stitches'

// configures jest-stitches to ignore DOM elements
expect.addSnapshotSerializer(createSerializer({DOMElements: false}))

Custom assertions

toHaveStyleRule

To make more explicit assertions when testing your components you can use the toHaveStyleRule matcher.

import React from 'react'
import renderer from 'react-test-renderer'
import {createStyled} from '@stitches/react'
import {matchers} from 'jest-stitches'

const {styled, css} = createStyled({})
// Add the custom matchers provided by 'jest-stitches'
expect.extend(matchers)

test('renders with correct styles', () => {
  const Button = styled('button', {
    variants: {
      blue: {
        backgroundColor: 'blue',
      },
    },
  })

  const tree = renderer.create(<Button>Hello world</Button>).toJSON()

  expect(tree).toHaveStyleRule('background-color', 'blue')
  expect(tree).not.toHaveStyleRule('background-color', 'hotpink')
})

Credit

This was inspired by and relies almost entirely on work by jest-emotion which was largely inspired by jest-glamor-react.

LICENSE

MIT

jest-stitches's People

Contributors

jaredlunde avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

atom-learning

jest-stitches's Issues

`:root` included in snapshot

Describe the bug

I'm adding this as a bug rather than a feature request as this behaviour has changed when upgrading to the latest beta version of stitches. Previously jest-stitches would collate all the relevant classes and render them in the snapshot but would not include the :root pseudo-class containing the stitches theme, I think this is the correct behaviour as :root level custom properties are not specific to the component.

To Reproduce

Create a generic styled component from stitches alongside a test using jest-stitches

import { createCss } from '@stitches/react'

const { styled } = createCss({
  theme: {
    colors: { primary: 'red' }
  }
})

export const Box = styled('div', {})
import { render, screen } from '@testing-library/react'
import * as React from 'react'

import { Box } from './'

describe(`Box component`, () => {
  it('renders', async () => {
    const { container } = render(
      <Box css={{ color: '$primary' }}>BOX</Box>
    )
    await screen.findByText('BOX')
    expect(container).toMatchSnapshot()
  })
})

jest.config.js

module.exports = {
  snapshotSerializers: ['jest-stitches']
}

Expected behavior

Expect to see relevant classes from styled component and css, but not document level :root class.

// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Box component renders 1`] = `
:root {
  --colors-primary: red;
}

.sx03kze4nwvh--css {
  color: var(--colors-primary);
}

<div>
  <div
    class="sx03kze sx03kze4nwvh--css"
  >
    BOX
  </div>
</div>
`;

Additional context

Package versions

"@stitches/react": "0.1.0-canary.3",
"jest": "26.6.3",
"jest-dom": "4.0.0",
"jest-stitches": "1.0.4"

Let me know if you need any additional context or detail with the issue, thanks!

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.