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.

theme context not working with styled components in jest

See original GitHub issue

I am upgrading to emotion v11

I am getting errors in my unit tests indicating that the theme context is not provided properly, it is just an empty object {}

Sample render util

import { Router } from 'react-router-dom'
import { render } from '@testing-library/react'
import { createMemoryHistory } from 'history'
import { ThemeProvider } from '@emotion/react'
import theme from '../src/theme'

function renderWithRouterAndTheme(ui, { route = '/', ...renderOptions } = {}) {
  const history = createMemoryHistory({ initialEntries: [route] })
  const utils = render(
    <ThemeProvider theme={theme}>
      <Router history={history}>{ui}</Router>
    </ThemeProvider>,
    renderOptions,
  )
  return {
    ...utils,
    history,
  }
}

Sample test case

test('will render without error', () => {
  const { container, getByText } = renderWithRouterAndTheme(
    <Component {...data} />,
  )
})

Sample component

import styled from '@emotion/styled'
import { Link } from 'react-router-dom'
import { Box } from '../box'

const StyledComponent = styled(Box)`
  ...
  background-color: ${props => props.theme.colors.secondary.greylight};
  ...
`

const App = (props) => {
  ...

  return (
    <StyledComponent />
    ...
  )
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Andaristcommented, Apr 18, 2021

Yep, this means that for some reason your dependency tree is wrong and you load separate versions of our libraries.

0reactions
coltonehrmancommented, Apr 19, 2021

I was able to finally resolve this by using the babel-macro libs See - https://emotion.sh/docs/babel-macros

So, I assume the issue lied within my app’s babel/webpack configs across dev and test environment.

Anyways, thanks @iChenLei and @Andarist for your input and help 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't get Jest to work with Styled Components which contain ...
Wrapping the ThemeProvider around the component and passing the theme object to it, works fine for me. import React from 'react'; ...
Read more >
Releases - styled-components
You can now pass an element to a StyleSheetManager and all the components in its context below in the tree will add their...
Read more >
jest-styled-components - npm Package Health Analysis - Snyk
If Jest Styled Components is not working, it is likely caused by loading multiple instances of styled-components . This can happen especially when...
Read more >
jest cannot create styled-component for component: undefined.
This solved my problem. Open side panel. "Error: Cannot create styled-component for component: undefined" when I use new ServerStyleSheet.
Read more >
jest-styled-components - npm
Start using jest-styled-components in your project by running `npm i jest-styled-components`. There are 212 other projects in the npm ...
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