noctcore-code-quality/no-bare-date-now
Rule id
noctcore-code-quality/no-bare-date-now
- Package
- code-quality v1.0.0
- Preset
- strict
- Severity
- error
- Autofix
- No
- Suggestions
- No
- Options
- Optional
- Type info
- Not needed
Read wall-clock time through a mockable clock util, not bare Date.now() / new Date().
Business logic that reads Date.now() or new Date() directly is hard to test: every time-dependent
branch depends on the real clock. Routing wall-clock reads through a shared clock util
(nowMs() / now()) gives time-dependent code one seam you can freeze or advance in tests.
What it flags
Section titled “What it flags”Date.now()→ suggestsnowMs().- Zero-argument
new Date()→ suggestsnow(). Date()called as a function, which returns the current time as a string.- The same reads through the global object:
globalThis.Date.now(),new window.Date().
// bare clock reads in business logicconst start = Date.now();const created = new Date();import { now, nowMs } from './clock';
// through the clock seamconst start = nowMs();const created = now();
// parsing an explicit instantconst at = new Date('2026-01-01T00:00:00Z');What it does not flag
Section titled “What it does not flag”new Date(value)with an argument: that is a parse of an explicit instant, not a bare clock read.- Files covered by
allowIn, which by default is the clock util itself (**/clock.ts,**/clock/**) and test files (**/*.{spec,test}.*,**/__tests__/**), which fake the clock rather than inject it.
Options
Section titled “Options”| Option | Type | Default | Meaning |
|---|---|---|---|
allowIn |
string[] (globs) |
["**/clock.ts", "**/clock/**", "**/*.{spec,test}.{ts,tsx,mts,cts,js,jsx,mjs,cjs}", "**/__tests__/**"] |
File-path globs (infra, the clock util itself, tests) where bare Date is allowed. Setting it replaces the default, so list test globs again if you want them. |
'noctcore-code-quality/no-bare-date-now': ['error', { allowIn: ['**/clock.ts', '**/*.timing.ts'] }]When not to use it
Section titled “When not to use it”If your project has no clock abstraction and does not intend to add one.