Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

459
Views
TypeError: (0 , _style.default) is not a function - jest + material-ui

I have a setup like the following:

// .jestrc.json

...
"moduleNameMapper": {
  "style$": "<rootDir>/tests/mock.ts"
}

// mock.ts

export default {}

Which is how I usually exclude my assets from jest so that it doesn't break on trying to parse it.

However I had to use @mui/material (not my choice) and when using the same setup as I usually do running my tests now breaks on any @mui imports. Anywhere that I've imported a @mui component my test breaks with the error TypeError: (0 , _style.default) is not a function.

I've gone through every stackoverflow question I can find about this error and several other posts, but nothing I try fixes it.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The error is happening because the regular expression in the moduleNameMapper is also catching all the @mui styles, which makes @mui break because of not being able to access the theme provider and HOC styles that it internally uses. It results in @mui also now using the empty mocked object and breaks because of it.

I found and tried several solutions with jest.mock and mocking out the styles/theme, however most of them are utterly whacky solutions and none of them worked.

In the end the way I fixed it was to change:

"moduleNameMapper": {
  "style$": "<rootDir>/tests/mock.ts"
}

to:

"moduleNameMapper": {
  "style.(scss)$": "<rootDir>/tests/mock.ts"
}

So that it will only exclude my own styles and not @mui as well. @mui will deal with its own styles without issues when using jest, when using moduleNameMapper you have to be careful not to mock the modules that are required by your dependencies and to only exclude the modules you need to.

Using "style" in the regular expression accidentally also excluded all the @mui things without realizing, just fixing that and allowing @mui to handle itself made everything work perfect.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!