theme context not working with styled components in jest
See original GitHub issueI 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:
- Created 2 years ago
- Comments:9 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yep, this means that for some reason your dependency tree is wrong and you load separate versions of our libraries.
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 😃