loopback4:单元测试冻结时间
解决方案
import {expect} from '@loopback/testlab';
import sinon from 'sinon';
describe('example test', () => {
let clock: sinon.SinonFakeTimers;
before(() => {
clock = sinon.useFakeTimers();
});
after(() => {
clock.restore();
});
it('should return the correct date', () => {
// set the clock to a specific time
clock.setSystemTime(new Date('2022-05-01T00:00:00Z'));
const currentDate = new Date();
expect(currentDate.getFullYear()).to.equal(2022);
expect(currentDate.getMonth()).to.equal(4);
expect(currentDate.getDate()).to.equal(1);
});
});
学以致用,知行合一
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 百万级群聊的设计实践
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
2022-04-26 WebStorm: 配置React中useState自动补齐功能
2022-04-26 pandas: dataframe转字典,并设置key