From ba3c62fca76fe2dc9fd1c8c9292c5333bf2c10a7 Mon Sep 17 00:00:00 2001 From: Shinigami Date: Fri, 25 Mar 2022 16:45:16 +0100 Subject: refactor: deprecate time.recent (#661) --- src/time.ts | 13 ++++++++++++- test/time.spec.ts | 46 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/time.ts b/src/time.ts index 2d376839..06a32021 100644 --- a/src/time.ts +++ b/src/time.ts @@ -2,6 +2,8 @@ import type { LiteralUnion } from './faker'; /** * Module to generate time of dates in various formats. + * + * @deprecated You should stop using this module, as it will be removed in the future. */ export class Time { /** @@ -20,11 +22,20 @@ export class Time { * faker.time.recent('date') // 2022-03-01T20:35:47.402Z * faker.time.recent('wide') // '00:34:11 GMT+0100 (Central European Standard Time)' * faker.time.recent('unix') // 1643067231856 + * + * @deprecated You should stop using this function, as it will be removed in the future. Use the native `new Date()` with one of the wanted functions directly. */ recent( format: LiteralUnion<'abbr' | 'date' | 'wide' | 'unix'> = 'unix' ): string | number | Date { - // TODO ST-DDT 2022-03-01: Deprecate for removal - #557 + console.warn( + `Deprecation Warning: faker.time.recent() is deprecated. Use the native \`new Date()\` and call the function you want on it. + abbr => toLocaleTimeString() + wide => toTimeString() + unix => getTime() +` + ); + let date: string | number | Date = new Date(); switch (format) { diff --git a/test/time.spec.ts b/test/time.spec.ts index 1146bb08..90d4d2c4 100644 --- a/test/time.spec.ts +++ b/test/time.spec.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from 'vitest'; +import { describe, expect, it, vi } from 'vitest'; import { faker } from '../src'; const seededRuns = [ @@ -56,23 +56,67 @@ describe('time', () => { for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) { describe('recent()', () => { it('should return the recent timestamp in unix time format by default', () => { + const spy = vi.spyOn(console, 'warn'); + const date = faker.time.recent(); expect(date).toBeTypeOf('number'); + + expect(spy).toHaveBeenCalledWith( + `Deprecation Warning: faker.time.recent() is deprecated. Use the native \`new Date()\` and call the function you want on it. + abbr => toLocaleTimeString() + wide => toTimeString() + unix => getTime() +` + ); + spy.mockRestore(); }); it('should return the recent timestamp in full time string format', () => { + const spy = vi.spyOn(console, 'warn'); + const date = faker.time.recent('wide'); expect(date).toBeTypeOf('string'); + + expect(spy).toHaveBeenCalledWith( + `Deprecation Warning: faker.time.recent() is deprecated. Use the native \`new Date()\` and call the function you want on it. + abbr => toLocaleTimeString() + wide => toTimeString() + unix => getTime() +` + ); + spy.mockRestore(); }); it('should return the recent timestamp in abbreviated string format', () => { + const spy = vi.spyOn(console, 'warn'); + const date = faker.time.recent('abbr'); expect(date).toBeTypeOf('string'); + + expect(spy).toHaveBeenCalledWith( + `Deprecation Warning: faker.time.recent() is deprecated. Use the native \`new Date()\` and call the function you want on it. + abbr => toLocaleTimeString() + wide => toTimeString() + unix => getTime() +` + ); + spy.mockRestore(); }); it('should return the recent timestamp in unix time format', () => { + const spy = vi.spyOn(console, 'warn'); + const date = faker.time.recent('unix'); expect(date).toBeTypeOf('number'); + + expect(spy).toHaveBeenCalledWith( + `Deprecation Warning: faker.time.recent() is deprecated. Use the native \`new Date()\` and call the function you want on it. + abbr => toLocaleTimeString() + wide => toTimeString() + unix => getTime() +` + ); + spy.mockRestore(); }); }); } -- cgit v1.2.3