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?
utc(...args)
instead ofutc(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. – Castoffdayjs()
function within current implementation? – Kinesics