Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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