question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Styled component + storybook crashes the snapshots

See original GitHub issue

Describe the bug Running yarn test causes all my tests to crash on components that use styled components

To Reproduce Steps to reproduce the behavior:

  1. CRA setup
  2. Add storybook
  3. Run the snapshots for a component
  4. Crashes…

Expected behavior Shouldn’t crash.

Code snippets

The custom .babelrc added because I’m using context to load all the .story.js files.

{
    "presets": ["@babel/preset-env", "@babel/preset-react"],
    "env": {
        "test": {
            "plugins": ["require-context-hook", "babel-plugin-styled-components"]
        }
    }
}

Storybook config

import { configure } from '@storybook/react';
import 'react-chromatic/storybook-addon';

const req = require.context('../src/components', true, /.stories.js$/);

function loadStories() {
  req.keys().forEach(filename => req(filename));
}

configure(loadStories, module);

A component that crashes:

import React from 'react';
import PropTypes from 'prop-types';
import styled from 'styled-components'

const Card = ({ children, size }) => (
    <CardContainer size={size} >
        {children}
    </CardContainer>
)
const CardContainer = styled.div`
  border: 1px solid grey;
  border-radius: 5px;
  padding: 20px;
  width: ${props => props.size === 'fixed' ? '200px' : 'auto'}
`

Card.propTypes = {
    children: PropTypes.node
}

export default Card;

The error that occurs:

 console.error node_modules/react-test-renderer/cjs/react-test-renderer.development.js:8060
      The above error occurred in the <Context.Consumer> component:
          in StyledComponent (created by styled.input)
          in input (at input.stories.js:17)

      Consider adding an error boundary to your tree to customize error handling behavior.
      Visit https://fb.me/react-error-boundaries to learn more about error boundaries.

  ● Storyshots › Input › default
    TypeError: chunk is not a constructor
      at flatten (node_modules/styled-components/dist/styled-components.cjs.js:1288:29)
      at flatten (node_modules/styled-components/dist/styled-components.cjs.js:1267:16)
      at ComponentStyle.generateAndInjectStyles (node_modules/styled-components/dist/styled-components.cjs.js:1474:19)
      at StyledComponent.generateAndInjectStyles (node_modules/styled-components/dist/styled-components.cjs.js:2136:36)
      at StyledComponent.renderInner (node_modules/styled-components/dist/styled-components.cjs.js:2043:33)
      at renderInner (node_modules/styled-components/dist/styled-components.cjs.js:1936:35)
      at updateContextConsumer (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:7275:19)
      at beginWork (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:7441:14)
      at performUnitOfWork (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:10138:12)
      at workLoop (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:10170:24)
      at renderRoot (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:10256:7)
      at performWorkOnRoot (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11121:7)      at performWork (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11033:7)
      at performSyncWork (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11007:3)
      at requestWork (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:10876:5)
      at scheduleWork (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:10686:5)
      at scheduleRootUpdate (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11273:3)
      at updateContainerAtExpirationTime (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11301:10)
      at updateContainer (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11312:10)
      at create (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11828:5)
      at getRenderedTree (node_modules/@storybook/addon-storyshots/dist/frameworks/react/renderTree.js:20:16)
      at node_modules/@storybook/addon-storyshots/dist/test-bodies.js:21:18
      at Object.<anonymous> (node_modules/@storybook/addon-storyshots/dist/api/snapshotsTestsTemplate.js:39:33)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
Pixelatexcommented, Dec 9, 2018

I managed to fix this I think. I’ll update it this week when I’m back at work with the issue/fix/etc

0reactions
stale[bot]commented, Jan 30, 2019

Hey there, it’s me again! I am going close this issue to help our maintainers focus on the current development roadmap instead. If the issue mentioned is still a concern, please open a new ticket and mention this old one. Cheers and thanks for using Storybook!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Frequently Asked Questions - Storybook - JS.ORG
Storybook is a frontend workshop for building UI components and pages in isolation. ... Is snapshot testing with Storyshots supported for Vue 3?...
Read more >
Unhandled JS Exception: Cannot create styled-component for ...
I had a snapshot of a working application with version 0.53.3 that I had set aside. ... I did so and the app...
Read more >
storybook-addon-styled-component-theme - npm
storybook addon with styled-components theme. Latest version: 2.0.0, last published: 2 years ago.
Read more >
storybook cannot read properties of undefined ... - You.com
Please help me out to solve a problem. I'm making my library for react apps using styled-components, react, storybook. Also have babel config...
Read more >
How to increase CSS-in-JS performance by 175x - ITNEXT
That is until you put a bunch of them on one page in Storybook. Let's look at the ubiquitous Button component as it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found