TypeError: Utils is not a constructor

4.9k Views Asked by At

I am using MuiPickersUtilsProvider from @material-ui/pickers@ v-3.2.10

and using like followings:

import DateFnsUtils from '@date-io/date-fns'
import { DateTimePicker, MuiPickersUtilsProvider } from '@material-ui/pickers'

<MuiPickersUtilsProvider utils={DateFnsUtils}>
                <DateTimePicker .../>
</MuiPickersUtilsProvider>

It works on UI but while running test case it throws error as follows:

TypeError: Utils is not a constructor

But as soon as I use import * as DateFnsUtils from '@date-io/date-fns' our test case works but same error comes on UI,

And When I use import {default as DateFnsUtils} from '@date-io/date-fns' our test case works but same error comes on both place(UI and test case)

I found similar discussion on below link but no help

material-ui-pickers/issues/805

// setup file
var enzyme = require('enzyme');
var Adapter = require('enzyme-adapter-react-16');
require('jest-canvas-mock')
enzyme.configure({ adapter: new Adapter() });

crypto = require('@trust/webcrypto')

// fetch undefined
const fetchPolifill = require('whatwg-fetch')
global.fetch = fetchPolifill.fetch
global.Request = fetchPolifill.Request
global.Headers = fetchPolifill.Headers
global.Response = fetchPolifill.Response

function noOp () { }
if (typeof window.URL.createObjectURL === 'undefined') {
  Object.defineProperty(window.URL, 'createObjectURL', { value: noOp})
}
2

There are 2 best solutions below

0
On

I got the same issue and I resolved by following code,

"<MuiPickersUtilsProvider utils={DateFnsUtils}>"


import React, { useState } from 'react'
import DateFnsUtils from '@date-io/date-fns';
import { MuiPickersUtilsProvider, KeyboardDatePicker } from '@material-ui/pickers';

const ProblemStatement = () => {
 const [selectedDate, setSelectedDate] = useState(new Date('2014-08-18T21:11:54'))
    const handleDateChange = (date) => {
        setSelectedDate(date)
    }
    return (
        <div>
            ProblemStatement
            <h2>datepicker</h2>
            //this one
            <MuiPickersUtilsProvider utils={DateFnsUtils}>
                <KeyboardDatePicker
                    margin="normal"
                    id="date-picker-dialog"
                    label="Date picker dialog"
                    format="MM/dd/yyyy"
                    value={selectedDate}
                    onChange={handleDateChange}
                    KeyboardButtonProps={{
                        'aria-label': 'change date',
                    }}
                />
            </MuiPickersUtilsProvider>


        </div>
    )
}
1
On

I got this error when using LocalizationProvider in order to use DatePicker component of mui. In my case, the problem was related to a typo in dateAdapter prop. I passed dataAdapter={AdapterDateFns} instead of dateAdapter={AdapterDateFns}. The problem is solved after fixing the typo:

import AdapterDateFns from '@mui/lab/AdapterDateFns';
import LocalizationProvider from '@mui/lab/LocalizationProvider';

export default function App() {

  return (
    <LocalizationProvider dateAdapter={AdapterDateFns}>
        <Main />
    </LocalizationProvider>
  );
}