Skip to content

Commit

Permalink
Add support for bigint to pretty-format (#8138)
Browse files Browse the repository at this point in the history
* Add support for bigint to pretty-format

* Address code review comments
  • Loading branch information
msafi authored and thymikee committed Mar 17, 2019
1 parent 13b4412 commit 76782b8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
23 changes: 23 additions & 0 deletions packages/pretty-format/src/__tests__/prettyFormat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,29 @@ describe('prettyFormat()', () => {
expect(prettyFormat(val)).toEqual('-0');
});

/* 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);
expect(prettyFormat(val)).toEqual('2001-09-09T01:46:40.000Z');
Expand Down
7 changes: 7 additions & 0 deletions packages/pretty-format/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]';
Expand Down Expand Up @@ -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, '\\$&') + '"';
Expand Down

0 comments on commit 76782b8

Please sign in to comment.