如何在 Jest 中设置模拟日期? [英] How do I set a mock date in Jest?

查看:49
本文介绍了如何在 Jest 中设置模拟日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 moment.js 在我的 React 组件的帮助文件中执行大部分日期逻辑,但我无法弄清楚如何在 Jest 中模拟日期 sinon.useFakeTimers().

I'm using moment.js to do most of my date logic in a helper file for my React components but I haven't been able to figure out how to mock a date in Jest a la sinon.useFakeTimers().

Jest 文档只讨论了诸如 setTimeoutsetInterval 等定时器函数,但不帮助设置日期,然后检查我的日期函数是否执行它们的操作本来就是要做的.

The Jest docs only speak about timer functions like setTimeout, setInterval etc but don't help with setting a date and then checking that my date functions do what they're meant to do.

这是我的一些 JS 文件:

Here is some of my JS file:

var moment = require('moment');

var DateHelper = {
  
  DATE_FORMAT: 'MMMM D',
  API_DATE_FORMAT: 'YYYY-MM-DD',
  
  formatDate: function(date) {
    return date.format(this.DATE_FORMAT);
  },

  isDateToday: function(date) {
    return this.formatDate(date) === this.formatDate(moment());
  }
};


module.exports = DateHelper;

这是我使用 Jest 设置的:

and here is what I've set up using Jest:

jest.dontMock('../../../dashboard/calendar/date-helper')
    .dontMock('moment');

describe('DateHelper', function() {
  var DateHelper = require('../../../dashboard/calendar/date-helper'),
      moment = require('moment'),
      DATE_FORMAT = 'MMMM D';

  describe('formatDate', function() {

    it('should return the date formatted as DATE_FORMAT', function() {
      var unformattedDate = moment('2014-05-12T00:00:00.000Z'),
          formattedDate = DateHelper.formatDate(unformattedDate);

      expect(formattedDate).toEqual('May 12');
    });

  });

  describe('isDateToday', function() {

    it('should return true if the passed in date is today', function() {
      var today = moment();

      expect(DateHelper.isDateToday(today)).toEqual(true);
    });
    
  });

});

现在这些测试通过了,因为我使用的是 moment,我的函数使用 moment,但它似乎有点不稳定,我想将日期设置为测试的固定时间.

Now these tests pass because I'm using moment and my functions use moment but it seems a bit unstable and I would like to set the date to a fixed time for the tests.

知道如何实现这一点吗?

Any idea on how that could be accomplished?

推荐答案

从 Jest 26 开始,这可以使用现代"实现无需安装任何第 3 方模块的假计时器:https://jestjs.io/blog/2020/05/05/jest-26#new-fake-timers

As of Jest 26 this can be achieved using "modern" fake timers without needing to install any 3rd party modules: https://jestjs.io/blog/2020/05/05/jest-26#new-fake-timers

jest
  .useFakeTimers()
  .setSystemTime(new Date('2020-01-01').getTime());

如果您希望伪计时器在所有测试中处于活动状态,您可以在您的配置中设置 timers: 'modern':https://jestjs.io/docs/configuration#timers-string

If you want the fake timers to be active for all tests, you can set timers: 'modern' in your configuration: https://jestjs.io/docs/configuration#timers-string

从 Jest 27 开始,现代假计时器是默认设置,因此您可以将参数删除到 useFakeTimers.

As of Jest 27 modern fake timers is the default, so you can drop the argument to useFakeTimers.

这篇关于如何在 Jest 中设置模拟日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆