8

I have this dayjs objects:

const today = dayjs.utc(date).startOf("day")

I am trying to mock it using jest but to no avail. Here is the approach I tried:

jest.mock("dayjs", () => ({
  extend: jest.fn(),
  utc: jest.fn((...args) => {
    const dayjs = jest.requireActual("dayjs");
    dayjs.extend(jest.requireActual("dayjs/plugin/utc"));

    return dayjs
      .utc(args.filter((arg) => arg).length > 0 ? args : mockDate)
      .startOf("day");
  }),
  startOf: jest.fn().mockReturnThis(),
}));

I also tried this:

jest.mock("dayjs", () => ({
  extend: jest.fn(),
  utc: jest.fn((...args) => ({
    startOf: jest.fn(() => {
      const dayjs = jest.requireActual("dayjs");
      dayjs.extend(jest.requireActual("dayjs/plugin/utc"));

      return dayjs
        .utc(args.filter((arg) => arg).length > 0 ? args : mockEventData)
        .startOf("day");
    }),
  })),
}));

Both are not working. Anyone got an advice?

Teneff
  • 30,564
  • 13
  • 72
  • 103
atomNULL
  • 209
  • 1
  • 2
  • 6
  • 1
    Shouldn't it be `utc(...args)` instead of `utc(args)`? What exactly does 'not working' mean? If there are errors, please, show them. The question should contain a clear problem statement and a way to reproduce the problem. – Estus Flask Jul 15 '20 at 06:36
  • @atomNULL Can you share the working example? Also, how can I mock the default `dayjs()` function within current implementation? – Mohammed Amir Ansari Aug 18 '20 at 14:21
  • What is mockDate and mockDateData for your args above? – Roger Perez Oct 13 '21 at 21:27

5 Answers5

5

MockDate works great for this, no intense mocking code required.

https://www.npmjs.com/package/mockdate

import MockDate from 'mockdate'
import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
dayjs.extend(utc)

MockDate.set('2000-11-22')
console.log(dayjs.utc().format())

// >>> 2000-11-22T00:00:00Z

Remember to clear the mock after your test with: MockDate.reset();

John Culviner
  • 22,235
  • 6
  • 55
  • 51
3

Presuming you're trying to create a consistent output disregarding the given date argument, you can create Node Module mock like this:

src/__mocks__/dayjs.js
const mock = jest.genMockFromModule('dayjs');

const dayjs = jest.requireActual("dayjs");
const utc = jest.requireActual('dayjs/plugin/utc')
dayjs.extend(utc);

mock.utc = jest.fn().mockReturnValue(dayjs.utc(new Date('1995-12-17T03:24:00')))

module.exports = mock;

and then in your tests within the src folder dayjs.utc will always be using the mocked date

src/today.spec.js
const today = require("./today");
const dayjs = require("dayjs");

describe("today", () => {
  let result;
  beforeAll(() => {
    result = today();
  });

  it("should be called with a date", () => {
    expect(dayjs.utc).toHaveBeenCalledWith(expect.any(Date));
  });

  it("should return consistent date", () => {
    expect(result).toMatchInlineSnapshot(`"1995-12-17T00:00:00.000Z"`);
  });
});

example on github

Teneff
  • 30,564
  • 13
  • 72
  • 103
  • I've discovered that my initial approach was actually working (with small modification), I was just feeding it the wrong input data. But thanks for the answer, it sure is a valid approach, albeit different. – atomNULL Jul 16 '20 at 07:37
3

The best solution that I found is using jest fake timers, see docs.

describe('myDayjsFunction', () => {
  it('returns my value', () => {
    const param = 3

    jest.useFakeTimers().setSystemTime(new Date('2023-01-10'))

    const actual = myDayjsFunction(param)
    const expected = 5

    expect(actual).toEqual(5)

    jest.useRealTimers()
  })
})
Jobeso
  • 653
  • 1
  • 9
  • 11
0

I'm using plugin customParseFormat and I just mock like this:

jest.mock('dayjs/plugin/customParseFormat', () => ({
  default: jest.requireActual('dayjs/plugin/customParseFormat'),
}));

It works for me.

0

I've solved my problem with expect.any(Date) in a simple way! When I need to do an update, I record the actual date and in the test I used the expect.any(Date) to see if any date have been passed on query in the updatedAt field.

expect(prisma.transaction.update).toBeCalledWith({
    where: {
      id: 'any_id'
    },
    data: {
      error: 'Error message here',
      tries: 1,
      updatedAt: expect.any(Date)
    }
});
alvescleiton
  • 1,063
  • 11
  • 8