Skip to content

Commit

Permalink
test(dep): replace mockdate package with jest.fakeTimers (#23542)
Browse files Browse the repository at this point in the history
  • Loading branch information
RahulGautamSingh authored Jul 25, 2023
1 parent 992b336 commit d255f81
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 22 deletions.
2 changes: 0 additions & 2 deletions lib/modules/datasource/npm/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import mockDate from 'mockdate';
import { getPkgReleases } from '..';
import * as httpMock from '../../../../test/http-mock';
import { GlobalConfig } from '../../../config/global';
Expand Down Expand Up @@ -44,7 +43,6 @@ describe('modules/datasource/npm/index', () => {

afterEach(() => {
delete process.env.RENOVATE_CACHE_NPM_MINUTES;
mockDate.reset();
});

it('should return null for no versions', async () => {
Expand Down
31 changes: 17 additions & 14 deletions lib/workers/repository/update/branch/schedule.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import mockDate from 'mockdate';
import type { RenovateConfig } from '../../../../config/types';
import * as schedule from './schedule';

Expand Down Expand Up @@ -141,8 +140,12 @@ describe('workers/repository/update/branch/schedule', () => {
describe('isScheduledNow(config)', () => {
let config: RenovateConfig;

beforeAll(() => {
jest.useFakeTimers();
});

beforeEach(() => {
mockDate.set('2017-06-30T10:50:00.000'); // Locally 2017-06-30 10:50am
jest.setSystemTime(new Date('2017-06-30T10:50:00.000')); // Locally 2017-06-30 10:50am
jest.resetAllMocks();
config = {};
});
Expand Down Expand Up @@ -243,7 +246,7 @@ describe('workers/repository/update/branch/schedule', () => {

describe('supports cron syntax on Sundays', () => {
beforeEach(() => {
mockDate.set('2023-01-08T10:50:00.000'); // Locally Sunday 8 January 2023 10:50am
jest.setSystemTime(new Date('2023-01-08T10:50:00.000')); // Locally Sunday 8 January 2023 10:50am
});

it('approves if the weekday is *', () => {
Expand Down Expand Up @@ -275,7 +278,7 @@ describe('workers/repository/update/branch/schedule', () => {
`('$sched, $tz, $datetime', ({ sched, tz, datetime, expected }) => {
config.schedule = [sched];
config.timezone = tz;
mockDate.set(datetime);
jest.setSystemTime(new Date(datetime));
expect(schedule.isScheduledNow(config)).toBe(expected);
});
});
Expand Down Expand Up @@ -330,63 +333,63 @@ describe('workers/repository/update/branch/schedule', () => {

it('approves first day of the month', () => {
config.schedule = ['before 11am on the first day of the month'];
mockDate.set('2017-10-01T05:26:06.000'); // Locally Sunday, 1 October 2017 05:26:06
jest.setSystemTime(new Date('2017-10-01T05:26:06.000')); // Locally Sunday, 1 October 2017 05:26:06
const res = schedule.isScheduledNow(config);
expect(res).toBeTrue();
});

it('approves valid weeks of year', () => {
config.schedule = ['every 2 weeks of the year before 08:00 on Monday'];
mockDate.set('2017-01-02T06:00:00.000'); // Locally Monday, 2 January 2017 6am (first Monday of the year)
jest.setSystemTime(new Date('2017-01-02T06:00:00.000')); // Locally Monday, 2 January 2017 6am (first Monday of the year)
const res = schedule.isScheduledNow(config);
expect(res).toBeTrue();
});

it('rejects on weeks of year', () => {
config.schedule = ['every 2 weeks of the year before 08:00 on Monday'];
mockDate.set('2017-01-09T06:00:00.000'); // Locally Monday, 2 January 2017 6am (second Monday of the year)
jest.setSystemTime(new Date('2017-01-09T06:00:00.000')); // Locally Monday, 2 January 2017 6am (second Monday of the year)
const res = schedule.isScheduledNow(config);
expect(res).toBeFalse();
});

it('approves on months of year', () => {
config.schedule = ['of January'];
mockDate.set('2017-01-02T06:00:00.000'); // Locally Monday, 2 January 2017 6am
jest.setSystemTime(new Date('2017-01-02T06:00:00.000')); // Locally Monday, 2 January 2017 6am
const res = schedule.isScheduledNow(config);
expect(res).toBeTrue();
});

it('rejects on months of year', () => {
config.schedule = ['of January'];
mockDate.set('2017-02-02T06:00:00.000'); // Locally Thursday, 2 February 2017 6am
jest.setSystemTime(new Date('2017-02-02T06:00:00.000')); // Locally Thursday, 2 February 2017 6am
const res = schedule.isScheduledNow(config);
expect(res).toBeFalse();
});

it('approves schedule longer than 1 month', () => {
config.schedule = ['every 3 months'];
mockDate.set('2017-07-01T06:00:00.000'); // Locally Saturday, 1 July 2017 6am
jest.setSystemTime(new Date('2017-07-01T06:00:00.000')); // Locally Saturday, 1 July 2017 6am
const res = schedule.isScheduledNow(config);
expect(res).toBeTrue();
});

it('rejects schedule longer than 1 month', () => {
config.schedule = ['every 6 months'];
mockDate.set('2017-02-01T06:00:00.000'); // Locally Thursday, 2 February 2017 6am
jest.setSystemTime(new Date('2017-02-01T06:00:00.000')); // Locally Thursday, 2 February 2017 6am
const res = schedule.isScheduledNow(config);
expect(res).toBeFalse();
});

it('approves schedule longer than 1 month with day of month', () => {
config.schedule = ['every 3 months on the first day of the month'];
mockDate.set('2017-07-01T06:00:00.000'); // Locally Saturday, 1 July 2017 6am
jest.setSystemTime(new Date('2017-07-01T06:00:00.000')); // Locally Saturday, 1 July 2017 6am
const res = schedule.isScheduledNow(config);
expect(res).toBeTrue();
});

it('rejects schedule longer than 1 month with day of month', () => {
config.schedule = ['every 3 months on the first day of the month'];
mockDate.set('2017-02-01T06:00:00.000'); // Locally Thursday, 2 February 2017 6am
jest.setSystemTime(new Date('2017-02-01T06:00:00.000')); // Locally Thursday, 2 February 2017 6am
const res = schedule.isScheduledNow(config);
expect(res).toBeFalse();
});
Expand All @@ -401,7 +404,7 @@ describe('workers/repository/update/branch/schedule', () => {
];

cases.forEach(([datetime, expected]) => {
mockDate.set(datetime);
jest.setSystemTime(new Date(datetime));
expect(schedule.isScheduledNow(config)).toBe(expected);
});
});
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@
"markdownlint-cli2": "0.8.1",
"memfs": "4.2.0",
"mock-fs": "5.2.0",
"mockdate": "3.0.5",
"nock": "13.3.2",
"npm-run-all": "4.1.5",
"nyc": "15.1.0",
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7903,11 +7903,6 @@ mock-fs@5.2.0:
resolved "https://registry.yarnpkg.com/mock-fs/-/mock-fs-5.2.0.tgz#3502a9499c84c0a1218ee4bf92ae5bf2ea9b2b5e"
integrity sha512-2dF2R6YMSZbpip1V1WHKGLNjr/k48uQClqMVb5H3MOvwc9qhYis3/IWbj02qIg/Y8MDXKFF4c5v0rxx2o6xTZw==

mockdate@3.0.5:
version "3.0.5"
resolved "https://registry.yarnpkg.com/mockdate/-/mockdate-3.0.5.tgz#789be686deb3149e7df2b663d2bc4392bc3284fb"
integrity sha512-iniQP4rj1FhBdBYS/+eQv7j1tadJ9lJtdzgOpvsOHng/GbcDh2Fhdeq+ZRldrPYdXvCyfFUmFeEwEGXZB5I/AQ==

modify-values@^1.0.0, modify-values@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022"
Expand Down

0 comments on commit d255f81

Please sign in to comment.