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 tests failing with error TypeError _moment.default.localeData is not a function

See original GitHub issue

react-dates version e.g. react-dates@18.3.1

Describe the bug I’ve set up a jest test to render my component that is using a react-dates datepicker, and I’ve imported 'react-dates/initialize', but my test fails with the error:

TypeError: _moment.default.localeData is not a function

Source code (including props configuration) Steps to reproduce the behavior:

<DateRangePicker
  startDate={this.state.startDate}
  startDateId="your_unique_start_date_id"
  endDate={this.state.endDate}
  endDateId="your_unique_end_date_id"
  onDatesChange={({ startDate, endDate }) => this.setState({ startDate, endDate })}
  focusedInput={this.state.focusedInput}
  onFocusChange={focusedInput => this.setState({ focusedInput })}
/>

If you have custom methods that you are passing into a react-dates component, e.g. onDatesChange, onFocusChange, renderMonth, isDayBlocked, etc., please include the source for those as well.

Screenshots/Gifs If applicable, add screenshots or gifs to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. iOS] Mac OS 10.13.6
  • Browser [e.g. chrome, safari] Chrome
  • Version [e.g. 22] 72.0.3626.121 (Official Build) (64-bit)

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Is the issue reproducible in Storybook? The issue is not reproducible in storybook. This only happens in test.

Additional context Add any other context about the problem here.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
YM-KAcommented, Feb 16, 2020

Hi @ljharb ! I managed to get it to work. My mock looked like this:

const moment = require.requireActual('moment')
export default (timestamp = 0) => {
  return moment(timestamp)
}

I changed it into

const moment = require.requireActual('moment')
const myMoment = (timestamp = 0) => {
  return moment(timestamp)
}
Object.assign(myMoment, moment)
export default myMoment

😃

0reactions
ljharbcommented, Feb 16, 2020

Closing, but will be happy to reopen if @mkelley33 can reply with clarification.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest tests failing with error TypeError _moment.default ...
react-dates version e.g. react-dates@18.3.1. Describe the bug. I've set up a jest test to render my component that is using a react-dates ...
Read more >
Jest failing with TypeError: (0 , _user.default) is not a function
What is wrong here? I'm thinking it has something to do with useEffect because on removal, the test works and <App/> renders.
Read more >
Moment.js documentation - DevDocs
Note: Function parameters default to undefined when not passed in. Moment treats moment(undefined) as moment() . Note: Moments are created at evaluation ...
Read more >
Component testing scenarios - Angular
Error : This test module uses the component BannerComponent which is using a "templateUrl" or "styleUrls", but they were never compiled. Please call...
Read more >
Jest terminates with unknown error (ReactJS, Jest, Jasmine ...
When running all of our frontend tests, Jest terminates with the following error: ... getTime is not a function TypeError: (intermediate value).
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