javascript - 如何在开玩笑测试中模拟 moment()
问题描述
我需要使用 jest 和 moment 进行一些测试。一些导入的函数适用于当前日期 (moment() )。但是我似乎无法找到一种始终运行相同日期来测试或模拟时刻构造函数的方法,例如在时刻('2020-07-05')的固定时刻日期,即使当天是 2020-07-10,因此测试应始终在第 5 天下运行。
我的 ./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);
}
我的 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' });
});
我也试过了,但我无法让它工作:
jest.mock('moment', () => {
return () => jest.requireActual('moment')('2020-07-04');
});
and
global.moment = jest.fn(moment('2021-07-04'));
解决方案
您正在尝试nextDates
使用模拟函数测试getIntervalDates
函数。您需要进行一些重构,您应该为getIntervalDates
内部调用的函数保留相同的引用nextDates
。然后,您可以使用jest.spyOn
替换getIntervalDates
为模拟的。
例如
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
:
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' });
});
});
单元测试结果:
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
推荐阅读
- node.js - node-imap 模块在收件箱中接收旧电子邮件
- javascript - 我想返回在 Javascript 中包含对象数组的对象的一部分
- gitlab - Gitlab CI, DIND
- spring-boot - 我的 Expo-App 与 React Native 一起使用什么 IP/端口?
- spring - 当客户端断开连接时,如何从 Spring Cloud Netflix 获取 InstanceInfo?
- python-3.x - py_call_impl(callable, dots$args, dots$keywords) 中的 Keras 错误:
- .net - 改变sidetoc的宽度
- c# - 抓钩关节不会钩到游戏对象 Unity
- python - 如果对象包含键,则从数组中删除对象:值为空
- eclipse - 用于 if 条件和函数参数对齐的 Eclipse Java 格式化程序设置