File tree Expand file tree Collapse file tree
packages/pretty-format/src Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44
55### Fixes
66
7+ - ` [pretty-format] ` Print ` BigInt ` as a readable number instead of ` {} ` [ #8138 ] ( https://github.com/facebook/jest/pull/8138 )
8+
79### Chore & Maintenance
810
911- ` [*] ` Remove flow from code base ([ #8061 ] ( https://github.com/facebook/jest/pull/8061 ) )
Original file line number Diff line number Diff line change @@ -224,6 +224,29 @@ describe('prettyFormat()', () => {
224224 expect ( prettyFormat ( val ) ) . toEqual ( '-0' ) ;
225225 } ) ;
226226
227+ /* global BigInt */
228+ if ( typeof BigInt === 'function' ) {
229+ it ( 'prints a positive bigint' , ( ) => {
230+ const val = BigInt ( 123 ) ;
231+ expect ( prettyFormat ( val ) ) . toEqual ( '123n' ) ;
232+ } ) ;
233+
234+ it ( 'prints a negative bigint' , ( ) => {
235+ const val = BigInt ( - 123 ) ;
236+ expect ( prettyFormat ( val ) ) . toEqual ( '-123n' ) ;
237+ } ) ;
238+
239+ it ( 'prints zero bigint' , ( ) => {
240+ const val = BigInt ( 0 ) ;
241+ expect ( prettyFormat ( val ) ) . toEqual ( '0n' ) ;
242+ } ) ;
243+
244+ it ( 'prints negative zero bigint' , ( ) => {
245+ const val = BigInt ( - 0 ) ;
246+ expect ( prettyFormat ( val ) ) . toEqual ( '0n' ) ;
247+ } ) ;
248+ }
249+
227250 it ( 'prints a date' , ( ) => {
228251 const val = new Date ( 10e11 ) ;
229252 expect ( prettyFormat ( val ) ) . toEqual ( '2001-09-09T01:46:40.000Z' ) ;
Original file line number Diff line number Diff line change @@ -72,6 +72,10 @@ function printNumber(val: number): string {
7272 return Object . is ( val , - 0 ) ? '-0' : String ( val ) ;
7373}
7474
75+ function printBigInt ( val : bigint ) : string {
76+ return String ( `${ val } n` ) ;
77+ }
78+
7579function printFunction ( val : Function , printFunctionName : boolean ) : string {
7680 if ( ! printFunctionName ) {
7781 return '[Function]' ;
@@ -112,6 +116,9 @@ function printBasicValue(
112116 if ( typeOf === 'number' ) {
113117 return printNumber ( val ) ;
114118 }
119+ if ( typeOf === 'bigint' ) {
120+ return printBigInt ( val ) ;
121+ }
115122 if ( typeOf === 'string' ) {
116123 if ( escapeString ) {
117124 return '"' + val . replace ( / " | \\ / g, '\\$&' ) + '"' ;
You can’t perform that action at this time.
0 commit comments