Answer the question
In order to leave comments, you need to log in
How to restore a global method after a test in Jest?
Friends,
In Jest, if you do this: Date.now = jest.fn().mockReturnValue(1000);
inside one test(...)
, then in all subsequent test(...)
calls Date.now()
it will return 1000, instead of the result of the original method Date.now()
.
There are four Jest options:
Answer the question
In order to leave comments, you need to log in
Found the answer purely by accident. Each test file is run in Jest in a separate VM context, so global variable mutation only works within the file, so this is NOT a problem, it does not affect tests outside the current file in any way.
Copy the Date to another variable and use that. Why overwrite a global method?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question