Jest failes with "Value does not exist in theme['spacing']" when spacing value is used
See original GitHub issueI’m using a spacing
theme value on a Box
component:
import React from 'react';
import { Box, Text } from '../';
function NiceBox() {
return (
<Box padding="m">
<Text>So nice!</Text>
</Box>
);
}
export default NiceBox;
Inside a Jest test file, I try to render the component using @testing-library/react-native
(see https://github.com/Shopify/restyle/issues/62#issuecomment-712883700)
import React from 'react';
import { render } from '@testing-library/react-native';
import NiceBox from './NiceBox';
test('renders correctly', () => {
render(<NiceBox />);
});
Which throws this error:
Value 'm' does not exist in theme['spacing']
The spacing value exist in the theme deceleration, and renders correctly (outside Jest).
Any idea why is that? 🥺
Issue Analytics
- State:
- Created 3 years ago
- Comments:15
Top Results From Across the Web
jestjs - Theme.spacing is not a function - Stack Overflow
You are providing a function without a theme in the context. One of the parent elements needs to use a ThemeProvider. Error: Uncaught ......
Read more >Troubleshooting - Material UI - MUI
[Types] Property "palette", "spacing" does not exist on type 'DefaultTheme'. This error arises because makeStyles is now exported from the @mui/styles package, ...
Read more >Customize the look-and-feel of your App - Backstage.io
Notice how the padding is getting its value from theme.spacing , that means that setting a value for spacing in your custom theme...
Read more >Extend Material-UI theme in TypeScript - In Plain English
You found it ! In Theme type declaration, Palette is used in palette key. So you should extend not only PaletteOptions but also...
Read more >API Reference - styled-components
You can pass an arbitrary classname to a styled component without problem ... a value, that is then going to be merged into...
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 Free
Top 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
@guytepper hi,
restyle
requires theThemeProvider
component to wrap the whole application. You pass thetheme
object as the ThemeProvider’s prop and it makes the theme available to nested components via React Context. So when you’re rendering<Bod padding="m" />
, theBox
component tries to get the theme context. But it’s not present in your test.Usually, a good practice here is to create your custom
render
wrapper to set up your contexts, e.g.:And then later in your test:
You can use Jest’s moduleDirectory option and eslint-import-resolver-jest to allow absolute imports for
test-utils
, so no matter how deep in your folder structure the component test file is located, you can import it likeimport { render } from 'test-utils'
This is a very common scenario, I guess having a small note on that in the documentation would be very helpful I’d be happy to open the PR if it aligns with Shopify’s doc vision for this project 🙂
@caelinsutch you create a
setup.js
file. and in yourjest.config.js
,