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.

Jest failes with "Value does not exist in theme['spacing']" when spacing value is used

See original GitHub issue

I’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:open
  • Created 3 years ago
  • Comments:15

github_iconTop GitHub Comments

2reactions
kulykcommented, Dec 8, 2020

@guytepper hi, restyle requires the ThemeProvider component to wrap the whole application. You pass the theme 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" />, the Box 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.:

// Some global test-utils.js file

import React from 'react';
import { ThemeProvider } from '@shopify/restyle';
import { render as rntlRender } from '@testing-library/react-native';

const theme = {
   // some options here
   spacing: {
     s: 4,
     m: 8,
     l: 10,
   },
}

function render(ui, opts) {
    return rntlRender(ui, {
           wrapper: ({children}) => (
                  <ThemeProvider theme={theme}>
                     {children}
                  </ThemeProvider>
           )
       },
    )
}

export * from '@testing-library/react-native';
export {render};

And then later in your test:

// NiceBox.test.jsx

import React from 'react';
import { render } from './test-utils';
import NiceBox from './NiceBox';

test('renders correctly', () => {
  render(<NiceBox />);
});

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 like import { 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 🙂

1reaction
chj-damoncommented, Jan 4, 2021

@caelinsutch you create a setup.js file. and in your jest.config.js,

setupFiles: ['<rootDir>/jest/setup.js'],
Read more comments on GitHub >

github_iconTop 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 >

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