From 8b32f387eca29fe2deab94b4196fa71c492c00b8 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 28 Sep 2021 13:25:08 +0200 Subject: [PATCH] fix(cli-repl): print Date objects with invalid value as such MONGOSH-1004 --- packages/cli-repl/src/format-output.spec.ts | 7 +++++++ packages/cli-repl/src/format-output.ts | 5 ++++- 2 files changed, 11 insertions(+), 1 deletion(-) 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()}")`; }