如何模拟dayjs链式方法

ato*_*ULL 11 javascript node.js jestjs dayjs

我有 this dayjs 对象:

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

我试图用笑话来嘲笑它,但无济于事。这是我尝试过的方法:

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(),
}));
Run Code Online (Sandbox Code Playgroud)

我也尝试过这个:

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");
    }),
  })),
}));
Run Code Online (Sandbox Code Playgroud)

两者都不工作。有人有建议吗?

Job*_*eso 7

我发现的最佳解决方案是使用笑话假计时器,请参阅文档

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()
  })
})
Run Code Online (Sandbox Code Playgroud)


Joh*_*ner 6

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

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

Run Code Online (Sandbox Code Playgroud)

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


Ten*_*eff 4

假设您尝试创建一致的输出,而不考虑给定的日期参数,您可以像这样创建节点模块模拟:

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;
Run Code Online (Sandbox Code Playgroud)

然后在src文件夹内的测试中将dayjs.utc始终使用模拟日期

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"`);
  });
});
Run Code Online (Sandbox Code Playgroud)

github 上的示例