模拟DayJs默认函数及其链接方法

问题描述

我正在尝试模拟具有特定日期和时间的 dayjs()函数,并且还希望模拟其嵌套方法,即。 utc()和。 add()

这是我到目前为止尝试过的:

abc.ts:

getEarliestDate() {
    const {
        minHour,maxHour
    } = operatingHours;
    const earliestDateTime = dayjs().add(sla + 1,'minute');
    const earliestDateTimeUTC = earliestDateTime.utc();

    return dayjs().add(121,'minute').format('YYYY-MM-DD HH:mm');
}

abc.test.ts:

import { abc } from '../../src/common/serviceFactory';
import dayjs from 'dayjs';

jest.mock('dayjs',() =>
  jest.fn((...args) => jest.requireActual('dayjs')(args.filter((arg) => arg).length > 0 ? args : '2020-08-12')),);

jest.mock('dayjs',() => ({
  default: jest.requireActual('dayjs')(`2020-08-18 12:00:00`),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 : '12:00:00').startOf('day');
  }),add: jest.requireActual('dayjs')(`2020-08-18 12:00:00`),}));

describe('getEarliestDate Function',() => {
  it('should return earliest date response',() => {
    const earliestDate = abc.getEarliestDate();
    expect(earliestDate).toMatch(dayjs().add(121,'minute').format('YYYY-MM-DD HH:mm'));
  });

但是得到这个: TypeError: dayjs_1.default is not a function

感谢您的帮助/建议。

解决方法

我认为问题在于,模拟模块的default函数应该是该函数,而不是返回dayjs()的结果。尝试以下操作即可:

jest.mock('dayjs',() => ({
  default: jest.fn((...args) => {
    const yourDay = jest.requireActual('dayjs')(args.filter((arg) => arg).length > 0 ? args : '2020-08-11');
    // Mock add function
    yourDay.add = jest.fn(() => jest.requireActual('dayjs')(`2020-08-10 12:00:00`));
    // more mock
    return yourDay;
  })
}));

还删除了另一个模拟,因为它将替换:

jest.mock('dayjs',() => ({
  default: jest.requireActual('dayjs')(`2020-08-18 12:00:00`),extend: jest.fn(),// ...
}));

另一种解决方案,您可以更改并保持第一个模拟保持不变:

jest.mock('dayjs',() =>
  jest.fn((...args) => jest.requireActual('dayjs')(args.filter((arg) => arg).length > 0 ? args : '2020-08-12')),);

您唯一要做的就是在esModuleInterop中启用选项tsconfig.json,因为此选项将自动为您添加default功能:

{
  "compilerOptions": {
    // ...
    "esModuleInterop": true,},}
,

对于使用dayjs的基本操作,此代码有效:

jest.mock('dayjs',() => jest.fn((...args) => jest.requireActual('dayjs')(args.filter((arg) => arg).length > 0 ? args : '2020-08-12')),);

要模拟的日期为“ 2020-08-12”。

,

MockDate 非常适合这个,不需要大量的模拟代码。

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

import MockDate from 'mockdate'
import dayjs from 'dayjs'

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

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

记住在测试后清除模拟: MockDate.reset();