Moment Doesn't work With Jest

See original GitHub issue

Describe the bug When imported into a development setting import moment from 'moment' allows moment to work effectively and does everything I want it to. However when running jest tests that run over the code that calls moment() I get the following error:
“TypeError: moment_1.default is not a function” Strangely, Importing it in this way: import * as moment from 'moment' allows functionality in test but not in production. I have looked all through jest’s issues and this has been a reported issue however the Jest team is confident the issue is on moment’s side.

To Reproduce Steps to reproduce the behaviour:

  1. make a simple function in a react app which calls moment()
  2. add a jest test which will traverse this path.
  3. run the test.
  4. See error

Expected behaviour Moment should be callable in both test and production in the same way.

Desktop (please complete the following information):

  • OS: macOS Mojave
  • Testing: Jest -v 24.8.0

Moment-specific environment moment -v 2.24.0

  • Other libraries in use: TypeScript

Please run the following code in your environment and include the output: console output when moment is imported the way development expects i.e. import moment from ‘moment’

console.log src/resources/timestamping.tsx:4
      Tue Jul 16 2019 10:58:16 GMT-0400 (Eastern Daylight Time)
    console.log src/resources/timestamping.tsx:5
      7/16/2019, 10:58:16 AM
    console.log src/resources/timestamping.tsx:6
      240
TypeError: Cannot read property 'version' of undefined.

console output when moment is imported the way it functions in test i.e. import * as moment from ‘moment’

console.log src/resources/timestamping.tsx:4
      Tue Jul 16 2019 11:02:46 GMT-0400 (Eastern Daylight Time)
    console.log src/resources/timestamping.tsx:5
      7/16/2019, 11:02:46 AM
    console.log src/resources/timestamping.tsx:6
      240
    console.log src/resources/timestamping.tsx:8
      2.24.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:8
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

22reactions
DhrubajitPCcommented, May 21, 2020

after some more searching found the solution here https://github.com/aurelia/skeleton-navigation/issues/606#issuecomment-397051406

need to pass in "esModuleInterop": true under compilerOptions in tsconfig.json

5reactions
cstuncsikcommented, Jun 27, 2022

Still having this problem with jest@28.1.0 and moment@2.29.2 I didn’t want to change any source or test files so my solution was to add the following mock to setupFilesAfterEnv file:

jest.mock('moment', () => ({
  default: jest.requireActual('moment')
}))
Read more comments on GitHub >

github_iconTop Results From Across the Web

Mocking moment() and moment().format using jest
The easiest way to mock moment() and any function that it uses (i.e. .day() , .format() ) is to change the Date that...
Read more >
How to mock Moment with Jest ?. Par François Descamps
This post has to explain how to mock momentjs when you are testing some code with Jest. It could be very weird to...
Read more >
Testing MomentJs with Jest - Terodox.tech - wicked tech bub
The solution is not complex, but it is nuanced. The basic idea is to get an actual instance of the moment object first...
Read more >
Mocking moment with Jest - elnathsoft.pl
Most of the functions offered by moment do not require to be mocked and it is hard to imagine a problem requiring mocking...
Read more >
How to correctly mock Moment.js/Dates in Jest
However, this test will fail on any day that is not Jan 23rd. A solution to this is to override Javascript's date function...
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