最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Mocking dayjs extend - Stack Overflow

programmeradmin3浏览0评论

In my code that needs testing I use

import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
dayjs.extend(utc);

dayjs().add(15, 'minute')

In my test I need to mock dayjs in order to always have the same date when paring snapshots in jest so I did

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

It fails with

TypeError: _dayjs.default.extend is not a function

Unfortunately similar questions on here didn't help me. How could I mock both default dayjs but also extend?

In my code that needs testing I use

import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
dayjs.extend(utc);

dayjs().add(15, 'minute')

In my test I need to mock dayjs in order to always have the same date when paring snapshots in jest so I did

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

It fails with

TypeError: _dayjs.default.extend is not a function

Unfortunately similar questions on here didn't help me. How could I mock both default dayjs but also extend?

Share Improve this question edited Dec 3, 2020 at 17:32 jonrsharpe 122k30 gold badges267 silver badges474 bronze badges asked Dec 3, 2020 at 17:03 ver.iver.i 5861 gold badge5 silver badges13 bronze badges 1
  • 1 Try not to mock dayjs, you don't own that interface. Treat time as a dependency, so you can mock out your own module that returns a Date - then use dayjs(yourFunction()). – jonrsharpe Commented Dec 3, 2020 at 17:11
Add a ment  | 

3 Answers 3

Reset to default 4

You could write a more thorough manual mock for dayjs, one that has the extend method, but then you're coupling your tests to a 3rd party interface. "Don't mock what you don't own" - you'll end up having to recreate more and more of the dayjs interface in your mock, and then if that interface changes your tests will continue to pass but your code will be broken. Or if you decide to swap to a different time library, you have to rewrite all of your tests to manually mock the new interface.

Instead, treat time as a dependency. Have your own function, in your own module, that simply provides the current time as a Date object:

export const howSoonIsNow = () => new Date();

Then, when you need to create a dayjs object, do so from that (dayjs() is equivalent to dayjs(new Date()) per the docs):

import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';

import { howSoonIsNow } from './path/to/noTimeLikeThePresent';

dayjs.extend(utc);

dayjs(howSoonIsNow()).add(15, 'minute');

Now in your test you can swap out something you actually own, and not have to interfere with dayjs at all:

import { howSoonIsNow } from './path/to/noTimeLikeThePresent';

jest.mock('./path/to/noTimeLikeThePresent');

howSoonIsNow.mockReturnValue(new Date(2020, 8, 12));

Now if a new version of dayjs changes in a way that breaks your use of it, your tests will fail and tell you as much. Or if you swap to a different time library (here's an example using Moment) you don't have to rewrite all of your tests, so you can be confident you've swapped over correctly.

Also FWIW I don't rate snapshot testing in general - it just bees change detection, failing for irrelevant changes and encouraging people to ignore the test results and blindly recreate the snapshots if anything fails. Test based on the behaviour you want to see from your ponents.

You can try this:

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

It works for me.

Mock dayjs like you wish and don't forget to set the static function as follows:

import dayjs from "dayjs";

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

dayjs.extend = jest.fn();

发布评论

评论列表(0)

  1. 暂无评论