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);
  });
});
posted @   胸怀丶若谷  阅读(22)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 百万级群聊的设计实践
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
历史上的今天:
2022-04-26 WebStorm: 配置React中useState自动补齐功能
2022-04-26 pandas: dataframe转字典,并设置key
点击右上角即可分享
微信分享提示

目录导航