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

JS snippet to handle JSON.stringify bug with BigInt builtins #257

Closed
Blind4Basics opened this issue Jan 25, 2021 · 6 comments
Closed

JS snippet to handle JSON.stringify bug with BigInt builtins #257

Blind4Basics opened this issue Jan 25, 2021 · 6 comments
Labels
documentation Improvements or additions to documentation kind/snippet New Snippet language/javascript Articles related to JavaScript

Comments

@Blind4Basics
Copy link
Contributor

see here

@Blind4Basics Blind4Basics added kind/snippet New Snippet language/javascript Articles related to JavaScript labels Jan 25, 2021
@Blind4Basics Blind4Basics changed the title JS snippet to hande JSON.stringify bug with BigInt builtins JS snippet to handle JSON.stringify bug with BigInt builtins Jan 25, 2021
@hobovsky
Copy link
Contributor

hobovsky commented Feb 10, 2021

Quoting the original post:

One problem with BigInt is that JSON.stringify doesn't support it. Failure messages will throw a TypeError.

To make them look like this:

expected { BigInt: '123252' } to equal { BigInt: '123251' }

insert these two lines in Preloaded:

Object.defineProperty( BigInt.prototype, "toJSON", { value: String } );
Object.defineProperty( BigInt.prototype, "BigInt", { get() { return String(this); }, enumerable: true } ) ;

With that, you can use assert.strictEqual normally.

( I just now figured this out. In my own kata, I used to use something much more involved. )

Exception thrown:

TypeError: Do not know how to serialize a BigInt
    at JSON.stringify (<anonymous>)
    at processImmediate (internal/timers.js:461:21)

image

image

@hobovsky
Copy link
Contributor

It seems this error has been fixed in Mocha a couple of days ago: mochajs/mocha#4112

@kazk do you think the snippet proposed in the initial post would be useful, or it would be possible to update Mocha?

@kazk
Copy link
Member

kazk commented Feb 10, 2021

It's possible to update Mocha for Node v12 when it's released, as long as it's Mocha v8.x (no breaking change) or if we're sure any breaking changes won't affect Codewars and Qualified users.

@hobovsky
Copy link
Contributor

It just got released: https://github.com/mochajs/mocha/releases/tag/v8.3.0

Should I create an issue in runner repo for update of Mocha?

@kazk
Copy link
Member

kazk commented Feb 11, 2021

Yeah, I'll try to update soon, but having an issue for it is a good idea.

@hobovsky hobovsky added the documentation Improvements or additions to documentation label Feb 17, 2021
@hobovsky
Copy link
Contributor

Workaround is no longer needed as the issue got fixed directly in Chai: codewars/runner#146

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation kind/snippet New Snippet language/javascript Articles related to JavaScript
Projects
None yet
Development

No branches or pull requests

3 participants