简体   繁体   English

如何在开玩笑测试中模拟 moment()

[英]How to mock moment() in jest testing

I need run some testing using jest and moment.我需要使用 jest 和 moment 进行一些测试。 Some of the imported functions work with moment current date (moment() ).一些导入的函数适用于当前日期 (moment() )。 But I can't seem to find a way of always run the same date for testing or mock moment constructor, like pin moment date at moment('2020-07-05'), even if current day is 2020-07-10, so the test should always run under day 5.但是我似乎无法找到一种始终运行相同日期来测试或模拟时刻构造函数的方法,例如在时刻('2020-07-05')的固定时刻日期,即使当天是 2020-07-10,因此测试应始终在第 5 天下运行。

My./UtilsModule file:我的./UtilsModule 文件:

import moment from 'moment'
export const getIntervalDates = groupOfDates => {
   const CURRENT_DATE = moment().format('YYYY-MM-DD');
   return getDates(CURRENT_DATE, groupOfDates ) //another function that does some extra processing;
};

export const nextDates = (date, group) => {
  let newDate= getIntervalDates(group);
}

My test.js file, and what i tried for:我的 test.js 文件,以及我尝试的内容:

import { nextDates ,getIntervalDates } from '../UtilsModule';
it('testing function', () => {
      const PAYLOAD = {...};
      const DATE= '2020-07-20';

      const SpyGetIntervalDates = jest.spyOn(UtilsModule, 'getIntervalDates');
      SpyGetIntervalDates.mockImplementation(() => Promise.resolve({ minAge: '2020-08-04' }));

      const nextDate = UtilsModule.nextDates(DATE, PAYLOAD);
      expect(nextDate).toEqual({ minDate: '2020-11-04' });
    });

I also tried, but i couldn't make it to work:我也试过了,但我无法让它工作:

jest.mock('moment', () => {
  return () => jest.requireActual('moment')('2020-07-04');
});

and
global.moment = jest.fn(moment('2021-07-04'));

You were trying to test nextDates function with mocked getIntervalDates function.您正在尝试使用模拟的getIntervalDates function 测试nextDates function。 You need to do a little refactoring, you should keep the same reference for getIntervalDates function called inside nextDates .您需要进行一些重构,您应该对在nextDates中调用的getIntervalDates function 保持相同的引用。 Then, you can use jest.spyOn to replace getIntervalDates with a mocked one.然后,您可以使用jest.spyOngetIntervalDates替换为模拟的。

Eg例如

utilsModule.js : utilsModule.js

import moment from 'moment';

function getDates() {}

const getIntervalDates = (groupOfDates) => {
  const CURRENT_DATE = moment().format('YYYY-MM-DD');
  return getDates(CURRENT_DATE, groupOfDates);
};

const nextDates = (date, group) => {
  return exports.getIntervalDates(group);
};

exports.getIntervalDates = getIntervalDates;
exports.nextDates = nextDates;

utilsModule.test.js : utilsModule.test.js

const UtilsModule = require('./utilsModule');

describe('62736904', () => {
  it('testing function', async () => {
    const PAYLOAD = {};
    const DATE = '2020-07-20';

    const SpyGetIntervalDates = jest.spyOn(UtilsModule, 'getIntervalDates');
    SpyGetIntervalDates.mockImplementation(() => Promise.resolve({ minAge: '2020-08-04' }));

    const nextDate = await UtilsModule.nextDates(DATE, PAYLOAD);
    expect(nextDate).toEqual({ minAge: '2020-08-04' });
  });
});

unit test result:单元测试结果:

 PASS  stackoverflow/62736904/utilsModule.test.js (11.51s)
  62736904
    ✓ testing function (3ms)

----------------|---------|----------|---------|---------|-------------------
File            | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------------|---------|----------|---------|---------|-------------------
All files       |      75 |      100 |   33.33 |      75 |                   
 utilsModule.js |      75 |      100 |   33.33 |      75 | 6-7               
----------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        12.892s

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM