From b5b7367c907126dcef33fb24e50c1a7b3253ed8f Mon Sep 17 00:00:00 2001 From: "M.K. Safi" Date: Sat, 16 Mar 2019 16:59:11 -0700 Subject: [PATCH 1/2] Add support for bigint to pretty-format --- .../src/__tests__/prettyFormat.test.ts | 20 +++++++++++++++++++ packages/pretty-format/src/index.ts | 7 +++++++ 2 files changed, 27 insertions(+) diff --git a/packages/pretty-format/src/__tests__/prettyFormat.test.ts b/packages/pretty-format/src/__tests__/prettyFormat.test.ts index bcfca99f7c3d..8365d3df7d94 100644 --- a/packages/pretty-format/src/__tests__/prettyFormat.test.ts +++ b/packages/pretty-format/src/__tests__/prettyFormat.test.ts @@ -209,21 +209,41 @@ describe('prettyFormat()', () => { expect(prettyFormat(val)).toEqual('123'); }); + it('prints a positive bigint', () => { + const val = BigInt(123); + expect(prettyFormat(val)).toEqual('123n'); + }); + it('prints a negative number', () => { const val = -123; expect(prettyFormat(val)).toEqual('-123'); }); + it('prints a negative bigint', () => { + const val = BigInt(-123); + expect(prettyFormat(val)).toEqual('-123n'); + }); + it('prints zero', () => { const val = 0; expect(prettyFormat(val)).toEqual('0'); }); + it('prints zero bigint', () => { + const val = BigInt(0); + expect(prettyFormat(val)).toEqual('0n'); + }); + it('prints negative zero', () => { const val = -0; expect(prettyFormat(val)).toEqual('-0'); }); + it('prints negative zero bigint', () => { + const val = BigInt(-0); + expect(prettyFormat(val)).toEqual('0n'); + }); + it('prints a date', () => { const val = new Date(10e11); expect(prettyFormat(val)).toEqual('2001-09-09T01:46:40.000Z'); diff --git a/packages/pretty-format/src/index.ts b/packages/pretty-format/src/index.ts index b4daddc17114..aafe7cfc08d4 100644 --- a/packages/pretty-format/src/index.ts +++ b/packages/pretty-format/src/index.ts @@ -72,6 +72,10 @@ function printNumber(val: number): string { return Object.is(val, -0) ? '-0' : String(val); } +function printBigInt(val: bigint): string { + return String(`${val}n`); +} + function printFunction(val: Function, printFunctionName: boolean): string { if (!printFunctionName) { return '[Function]'; @@ -112,6 +116,9 @@ function printBasicValue( if (typeOf === 'number') { return printNumber(val); } + if (typeOf === 'bigint') { + return printBigInt(val); + } if (typeOf === 'string') { if (escapeString) { return '"' + val.replace(/"|\\/g, '\\$&') + '"'; From 84ac4f69fd2c0e2e5231942921f6ad5dc3b25495 Mon Sep 17 00:00:00 2001 From: "M.K. Safi" Date: Sun, 17 Mar 2019 06:50:57 -0700 Subject: [PATCH 2/2] Address code review comments --- CHANGELOG.md | 2 + .../src/__tests__/prettyFormat.test.ts | 41 ++++++++++--------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 786c91a3a910..b6091eb2283a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Fixes +- `[pretty-format]` Print `BigInt` as a readable number instead of `{}` [#8138](https://github.com/facebook/jest/pull/8138) + ### Chore & Maintenance - `[*]` Remove flow from code base ([#8061](https://github.com/facebook/jest/pull/8061)) diff --git a/packages/pretty-format/src/__tests__/prettyFormat.test.ts b/packages/pretty-format/src/__tests__/prettyFormat.test.ts index 8365d3df7d94..a9275ad50b49 100644 --- a/packages/pretty-format/src/__tests__/prettyFormat.test.ts +++ b/packages/pretty-format/src/__tests__/prettyFormat.test.ts @@ -209,40 +209,43 @@ describe('prettyFormat()', () => { expect(prettyFormat(val)).toEqual('123'); }); - it('prints a positive bigint', () => { - const val = BigInt(123); - expect(prettyFormat(val)).toEqual('123n'); - }); - it('prints a negative number', () => { const val = -123; expect(prettyFormat(val)).toEqual('-123'); }); - it('prints a negative bigint', () => { - const val = BigInt(-123); - expect(prettyFormat(val)).toEqual('-123n'); - }); - it('prints zero', () => { const val = 0; expect(prettyFormat(val)).toEqual('0'); }); - it('prints zero bigint', () => { - const val = BigInt(0); - expect(prettyFormat(val)).toEqual('0n'); - }); - it('prints negative zero', () => { const val = -0; expect(prettyFormat(val)).toEqual('-0'); }); - it('prints negative zero bigint', () => { - const val = BigInt(-0); - expect(prettyFormat(val)).toEqual('0n'); - }); + /* global BigInt */ + if (typeof BigInt === 'function') { + it('prints a positive bigint', () => { + const val = BigInt(123); + expect(prettyFormat(val)).toEqual('123n'); + }); + + it('prints a negative bigint', () => { + const val = BigInt(-123); + expect(prettyFormat(val)).toEqual('-123n'); + }); + + it('prints zero bigint', () => { + const val = BigInt(0); + expect(prettyFormat(val)).toEqual('0n'); + }); + + it('prints negative zero bigint', () => { + const val = BigInt(-0); + expect(prettyFormat(val)).toEqual('0n'); + }); + } it('prints a date', () => { const val = new Date(10e11);