Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for bigint to pretty-format #8138

Merged
merged 2 commits into from Mar 17, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
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
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
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