diff --git a/packages/cli-repl/src/format-output.spec.ts b/packages/cli-repl/src/format-output.spec.ts index 30d475f31d..7f956fa9a3 100644 --- a/packages/cli-repl/src/format-output.spec.ts +++ b/packages/cli-repl/src/format-output.spec.ts @@ -27,6 +27,13 @@ for (const colors of [ false, true ]) { }); }); + context('when the result is a date', () => { + it('returns the inspection', () => { + expect(format({ value: new Date(1234567890000) })).to.include('ISODate("2009-02-13T23:31:30.000Z")'); + expect(format({ value: new Date(NaN) })).to.include('Invalid Date'); + }); + }); + context('when the result is a Cursor', () => { context('when the Cursor is not empty', () => { it('returns the inspection', () => { diff --git a/packages/cli-repl/src/format-output.ts b/packages/cli-repl/src/format-output.ts index eca50dadae..31567ad33d 100644 --- a/packages/cli-repl/src/format-output.ts +++ b/packages/cli-repl/src/format-output.ts @@ -214,7 +214,10 @@ function removeUndefinedValues(obj: T) { return Object.fromEntries(Object.entries(obj).filter(keyValue => keyValue[1] !== undefined)); } -function dateInspect(this: Date): string { +function dateInspect(this: Date, depth: number, options: any): string { + if (isNaN(this.valueOf())) { + return options.stylize('Invalid Date', 'date'); + } return `ISODate("${this.toISOString()}")`; }