From d255f811ae28bf3574ab0a762204fb16bf1368bd Mon Sep 17 00:00:00 2001 From: RahulGautamSingh Date: Tue, 25 Jul 2023 21:59:18 +0545 Subject: [PATCH] test(dep): replace `mockdate` package with jest.fakeTimers (#23542) --- lib/modules/datasource/npm/index.spec.ts | 2 -- .../repository/update/branch/schedule.spec.ts | 31 ++++++++++--------- package.json | 1 - yarn.lock | 5 --- 4 files changed, 17 insertions(+), 22 deletions(-) diff --git a/lib/modules/datasource/npm/index.spec.ts b/lib/modules/datasource/npm/index.spec.ts index 8acfe3bab1ad61..54a37ed1603abd 100644 --- a/lib/modules/datasource/npm/index.spec.ts +++ b/lib/modules/datasource/npm/index.spec.ts @@ -1,4 +1,3 @@ -import mockDate from 'mockdate'; import { getPkgReleases } from '..'; import * as httpMock from '../../../../test/http-mock'; import { GlobalConfig } from '../../../config/global'; @@ -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 () => { diff --git a/lib/workers/repository/update/branch/schedule.spec.ts b/lib/workers/repository/update/branch/schedule.spec.ts index f70dd7e9b508a8..c7ab2bb6de4575 100644 --- a/lib/workers/repository/update/branch/schedule.spec.ts +++ b/lib/workers/repository/update/branch/schedule.spec.ts @@ -1,4 +1,3 @@ -import mockDate from 'mockdate'; import type { RenovateConfig } from '../../../../config/types'; import * as schedule from './schedule'; @@ -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 = {}; }); @@ -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 *', () => { @@ -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); }); }); @@ -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(); }); @@ -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); }); }); diff --git a/package.json b/package.json index 6639e361968062..56797ce3099efa 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/yarn.lock b/yarn.lock index ad452d4ea08bca..b6020b6362b240 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"