[Vuejs]-How to set a mock Date() with jest when I const it outside export default

0👍

const formatCurrentDate has been already evaluated during component, it cannot be affected without re-importing the component.

It’s a mistake to put it outside the component, this makes it less testable. The way this is done is also determined by how up-to-date current date should be, but generally it is:

export default {
  data() {
    const formatCurrentDate = dateFormat(new Date(), 'yyyy/mm/dd');

    currentDate: dateFormat(new Date(), 'yyyy-mm-dd'),
  },

Then it can be mocked before component instantiation.

Leave a comment