New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Moment Doesn't work With Jest #5166
Comments
Fixes angular / jest problem. But it's still a hack. |
Thank you sir, that helped. |
Is there something the Moment.js side can do here? |
I was facing this issue when i was using |
after some more searching found the solution here need to pass in |
@DhrubajitPC glad you found the solution. |
Sure... give me some time :) |
hmmm... it already seems to be part of the FAQ |
It seems I only get this error when I set esModuleInterop to true... |
FWIW, I updated my import to use
Not sure what the regular moment package is having issues with. |
I've found 2 ways to use moment in Jest. ( for the note: I use Angular). I needed to test a component method that uses 'moment()'.
The second option uses npm package MockDate https://www.npmjs.com/package/mockdate
|
This hack worked for me and Eslint is not complaining.
|
For anyone who still have no solution for it |
Still having this problem with jest.mock('moment', () => ({
default: jest.requireActual('moment')
})) |
I need to mock this call I've tried this: But it gives me an error TypeError: momentLocaleData.firstDayOfWeek is not a function When I try this: I get this other error: TypeError: Cannot read properties of undefined (reading 'firstDayOfWeek') How should I fix this? |
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:
Expected behaviour
Moment should be callable in both test and production in the same way.
Desktop (please complete the following information):
Moment-specific environment
moment -v 2.24.0
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 output when moment is imported the way it functions in test i.e. import * as moment from 'moment'
The text was updated successfully, but these errors were encountered: