boblauer / MockDate

A JavaScript Mock Date object that can be used to change when "now" is.
MIT License
664 stars 50 forks source link

'ReferenceError: Date is not defined' with Babel (Includes fix) #46

Open builtbybrayne opened 4 years ago

builtbybrayne commented 4 years ago

v3.0.2

I got ReferenceError: Date is not defined when calling reset().

My tests are Babel-compiled on the fly, and I believe this breaks the link between the Date reference and the actual global.Date class.

I have a fix, and would offer a PR but I'm not familiar with typescript.

I did copy your earlier js version (27a1a733709da04d9463fa53c9454ff7c3885e17 - Remove Dead Code) and replaced all Date references with global.Date. This works fine.

(Also, by the way, when you merged your ts branch, you accidentally undid the removal of the dead code in the mentioned commit).

Frozen-byte commented 3 years ago

Getting the same error on our Test-Pipeline. We are required to sync our time with the backend, so we mock the time on Server-Side:

    <% if Rails.env.test? && defined?(Timecop) && Timecop.top_stack_item %>
      <%= javascript_include_tag "mockdate/lib/mockdate" %>
      <%= javascript_tag "set('#{Time.now}')" %>
    <% end %>

On a simple new Date() call in frontend, we are facing the same error :

severe http://application:3333/assets/mockdate/lib/mockdate-718c595589fef86d66b941965fcaf05ed4a00f53815587930cc75d790fdf9156.js 14:22 application_1 | Uncaught ReferenceError: exports is not defined application_1 | Uncaught TypeError: Date is not a constructor

boblauer commented 3 years ago

@builtbybrayne Sorry for such a long delay 😦

I'm wondering if the fix here is to do globalThis.Date = MockDate. Is that essentially what you did to get this working on your end?

@Frozen-byte Your issue might be due to the fact that I accidentally broke the browser build in version 3.x. Please try version 3.0.3, it should be fixed.

brettz9 commented 3 years ago

I'm wondering if the fix here is to do globalThis.Date = MockDate. Is that essentially what you did to get this working on your end?

I know I'd like to be able to try this at overwriting the global Date in Node, so I imagine such an export would work great.