|
| 1 | +<p align="center"> |
| 2 | + <image src="nestjstools-logo.png" width="400"> |
| 3 | +</p> |
| 4 | + |
| 5 | +# @nestjstools/clock |
| 6 | + |
| 7 | +> ⏰ A minimal, flexible, and testable time abstraction layer for NestJS. |
| 8 | +
|
| 9 | +## Introduction |
| 10 | + |
| 11 | +In most applications, time plays a crucial role-whether it's timestamps, expiration checks, scheduling, or logging. However, directly calling `new Date()` throughout your code can make testing difficult and introduce unpredictable behavior in your services. |
| 12 | + |
| 13 | +`@nestjstools/clock` provides a clean abstraction over system time using the `IClock` interface. It enables dependency injection of time providers (`SystemClock`, `FixedClock`) in your NestJS app, improving testability, readability, and flexibility. |
| 14 | + |
| 15 | +This utility is especially useful in domain-driven design and hexagonal architecture, where you want infrastructure concerns (like the system clock) abstracted from your core business logic. |
| 16 | + |
| 17 | +## Features |
| 18 | + |
| 19 | +- **Abstraction of time handling** via the `IClock` interface. |
| 20 | +- **Swappable implementations**: `SystemClock` for real-time usage, `FixedClock` for predictable testing. |
| 21 | +- **Perfect for unit testing** with consistent and controlled time behavior. |
| 22 | +- **Seamless NestJS integration** via dependency injection. |
| 23 | + |
| 24 | +## Installation |
| 25 | + |
| 26 | +```bash |
| 27 | +npm install @nestjstools/clock |
| 28 | +#or |
| 29 | +yarn add @nestjstools/clock |
| 30 | +``` |
| 31 | + |
| 32 | +## Usage |
| 33 | + |
| 34 | +### SystemClock |
| 35 | + |
| 36 | +Returns the actual current system time. |
| 37 | + |
| 38 | +```ts |
| 39 | +import { SystemClock } from '@nestjstools/clock'; |
| 40 | + |
| 41 | +const clock = new SystemClock(); |
| 42 | +console.log(clock.now()); // → current system date/time |
| 43 | +``` |
| 44 | + |
| 45 | +### FixedClock |
| 46 | + |
| 47 | +Returns a fixed date/time—ideal for deterministic tests. |
| 48 | + |
| 49 | +```ts |
| 50 | +import { FixedClock } from '@nestjstools/clock'; |
| 51 | + |
| 52 | +const fixedDate = new Date('2023-01-01T00:00:00Z'); |
| 53 | +const clock = new FixedClock(fixedDate); |
| 54 | + |
| 55 | +console.log(clock.now()); // → always returns 2023-01-01T00:00:00Z - helpful in tests |
| 56 | +``` |
| 57 | + |
| 58 | +## NestJS Integration |
| 59 | + |
| 60 | +You can easily register the clock as a provider in your modules: |
| 61 | + |
| 62 | +```ts |
| 63 | +import { Module } from '@nestjs/common'; |
| 64 | +import { SystemClock, IClock } from '@nestjstools/clock'; |
| 65 | + |
| 66 | +@Module({ |
| 67 | + imports: [ |
| 68 | + ClockModule.forRoot(), //By default global |
| 69 | + ] |
| 70 | +}) |
| 71 | +export class ClockModule {} |
| 72 | +``` |
| 73 | + |
| 74 | +Inject the clock into your services: |
| 75 | + |
| 76 | +```ts |
| 77 | +import { Injectable, Inject } from '@nestjs/common'; |
| 78 | +import { IClock } from '@nestjstools/clock'; |
| 79 | + |
| 80 | +@Injectable() |
| 81 | +export class MyService { |
| 82 | + constructor(@Clock() private readonly clock: IClock) {} |
| 83 | + |
| 84 | + getCurrentTime(): Date { |
| 85 | + return this.clock.now(); |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +### Testing Example |
| 91 | + |
| 92 | +Swap out the system clock with a fixed one in your test setup: |
| 93 | + |
| 94 | +```ts |
| 95 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 96 | +import { INestApplication } from '@nestjs/common'; |
| 97 | +import * as request from 'supertest'; |
| 98 | +import { AppModule } from './../src/app.module'; |
| 99 | +import { FixedClock, Service } from '@nestjstools/clock'; |
| 100 | + |
| 101 | +describe('AppController (e2e)', () => { |
| 102 | + let app: INestApplication; |
| 103 | + |
| 104 | + beforeEach(async () => { |
| 105 | + const moduleFixture: TestingModule = await Test.createTestingModule({ |
| 106 | + imports: [AppModule], |
| 107 | + }) |
| 108 | + .overrideProvider(Service.CLOCK_SERVICE) // or .overrideProvider('CLOCK_SERVICE') |
| 109 | + .useValue(new FixedClock(new Date('2020-10-10'))) // now the Date returned by .now() will be static |
| 110 | + .compile(); |
| 111 | + |
| 112 | + app = moduleFixture.createNestApplication(); |
| 113 | + await app.init(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('/ (GET)', () => { |
| 117 | + return request(app.getHttpServer()) |
| 118 | + .get('/') |
| 119 | + .expect(200) |
| 120 | + .expect('Hello World!'); |
| 121 | + }); |
| 122 | +}); |
| 123 | +``` |
| 124 | + |
| 125 | +## Why Use This Library? |
| 126 | + |
| 127 | +* 🚫 Avoid scattered use of `new Date()` in your business logic |
| 128 | +* 📦 Lightweight and dependency-free |
| 129 | +* ⚙️ Improve the testability and maintainability of your time-dependent logic |
| 130 | +* 🧩 Fits well in clean architecture and DDD practices |
0 commit comments