Skip to content

Commit 76782b8

Browse files
msafithymikee
authored andcommitted
Add support for bigint to pretty-format (#8138)
* Add support for bigint to pretty-format * Address code review comments
1 parent 13b4412 commit 76782b8

3 files changed

Lines changed: 32 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
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))

packages/pretty-format/src/__tests__/prettyFormat.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff 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');

packages/pretty-format/src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
7579
function 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, '\\$&') + '"';

0 commit comments

Comments
 (0)