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 @ 2023-04-26 15:24  胸怀丶若谷  阅读(17)  评论(0编辑  收藏  举报