Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Theme.spacing is not a function

I have recently migrated my react app from material-ui v3 to v4 and replaced '@material-ui/core/styles' with '@material-ui/styles',for testing we have used jest and enzyme.Tests failed after migration.How can I resolve this issue? Errors are as follows:

Warning: Material-UI: the styles argument provided is invalid. You are providing a function without a theme in the context. One of the parent elements needs to use a ThemeProvider. Error: Uncaught [TypeError: theme.spacing is not a function] Full DOM rendering › encountered a declaration exception TypeError: theme.spacing is not a function

  17 |   },
  18 |   selectEmpty: {
> 19 |     marginTop: theme.spacing(2),
     |                      ^
  20 |   },
like image 422
Swetha D Avatar asked Jul 06 '19 12:07

Swetha D


2 Answers

Material UI old version is like import { makeStyles } from '@material-ui/styles';

but they have updated new version like that import { makeStyles } from '@material-ui/core/styles'

like image 126
Bharath Mb Avatar answered Nov 11 '22 20:11

Bharath Mb


You have to wrap every test with ThemeProvider. This resolves the issue. Example:

    wrapper = mount(
      <ThemeProvider>
        <Dropdown handleSelect={handleSelectSpy} options={options} />
      </ThemeProvider>,
    );

like image 3
Swetha D Avatar answered Nov 11 '22 19:11

Swetha D