loopback4:单元测试冻结时间
阅读原文时间:2023年09月27日阅读:2

解决方案

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);
  });
});

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章