Solution: TypeError: Cannot read properties of undefined (reading ‘DemoContainer’) and failing because of components={[‘DatePicker’, ‘TimePicker’]} in the DemoContainer tag.

We will get this error because we are mocking ‘@mui/x-date-pickers/internals/demo’ as follows.

but here it is not covering  components={[‘DatePicker’, ‘TimePicker’]} in the <DemoContainer components={[‘DatePicker’, ‘TimePicker’]}> as we are returning null in the mock. So it is throwing the error as Cannot read properties of undefined (reading ‘DemoContainer’). So here we need to mock DatePicker and TimePicker as follows.

Solution: Jest encountered an unexpected token and failing at —- import { DemoContainer } from ‘@mui/x-date-pickers/internals/demo’

If test cases are failing with the error message Jest encountered an unexpected token and shows SyntaxError: Unexpected token ‘export’ at line import { DemoContainer } from ‘@mui/x-date-pickers/internals/demo’, then we need to mock ‘@mui/x-date-pickers/internals/demo’ as follows.

Mock a function or setState using jest in ReactJS.

lets assume that we need to mock setStatus(). So, in test file we will mock by using jest as follows.