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

Bigint support. #4090

Closed
4 tasks done
MicahZoltu opened this issue Nov 9, 2019 · 7 comments · Fixed by #4112
Closed
4 tasks done

Bigint support. #4090

MicahZoltu opened this issue Nov 9, 2019 · 7 comments · Fixed by #4112
Labels
type: bug a defect, confirmed by a maintainer

Comments

@MicahZoltu
Copy link

Prerequisites

  • Checked that your issue hasn't already been filed by cross-referencing issues with the faq label
  • Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn't just a feature that actually isn't supported in the environment in question or a bug in your code.
  • 'Smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
  • Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with: node node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend that you not install Mocha globally.

Description

bigint is now a Stage 4 proposal but if you assert on a bigint Mocha fails during serialization of the error. I don't know how to reproduce this issue without Chai, but the error context suggests that the problem is with Mocha's error serializer, not Chai.

Steps to Reproduce

const expect =  require('chai').expect

describe('test', () => {
	it('test', async () => {
		expect(1n).equals(0n)
	})
})

Expected behavior: [What you expect to happen]
Chai doesn't natively support bigint either (separate issue), but it at least doesn't error on reporting. For now, I would expect to see Chai's worthless error message that at least pointed to the right test/line.

Actual behavior: [What actually happens]

  test
(node:14544) UnhandledPromiseRejectionWarning: TypeError: Do not know how to serialize a BigInt
    at JSON.stringify (<anonymous>)
    at _stringify (C:\Users\micah\Source\test\node_modules\mocha\lib\utils.js:409:20)
    at jsonStringify (C:\Users\micah\Source\test\node_modules\mocha\lib\utils.js:360:12)
    at Object.exports.stringify (C:\Users\micah\Source\test\node_modules\mocha\lib\utils.js:332:14)
    at stringifyDiffObjs (C:\Users\micah\Source\test\node_modules\mocha\lib\reporters\base.js:168:24)
    at Runner.<anonymous> (C:\Users\micah\Source\test\node_modules\mocha\lib\reporters\base.js:309:7)
    at Runner.emit (events.js:205:15)
    at Runner.fail (C:\Users\micah\Source\test\node_modules\mocha\lib\runner.js:304:8)
    at C:\Users\micah\Source\test\node_modules\mocha\lib\runner.js:673:18
    at done (C:\Users\micah\Source\test\node_modules\mocha\lib\runnable.js:334:5)
    at C:\Users\micah\Source\test\node_modules\mocha\lib\runnable.js:398:11
(node:14544) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:14544) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Reproduces how often: [What percentage of the time does it reproduce?]
100%

Versions

  • The output of mocha --version and node node_modules/.bin/mocha --version: 6.2.2
  • The output of node --version: v12.3.0
  • Your operating system
    • name and version: Windows 10
    • architecture (32 or 64-bit): 64-bit
  • Your shell (e.g., bash, zsh, PowerShell, cmd): pwsh
  • Your browser and version (if running browser tests): N/A
  • Any third-party Mocha-related modules (and their versions): Chai 4.2.0
  • Any code transpiler (e.g., TypeScript, CoffeeScript, Babel) being used (and its version): No.
@boneskull
Copy link
Contributor

I mean, it looks like JSON.stringify() doesn't work with BigInt, right? I really don't know how to get around that other than to e.g., coerce it into a string before handoff.

@boneskull
Copy link
Contributor

searching around, coercing to string looks like it's the workaround.

since we're using JSON.stringify() for "human-readable" diff display, I think taking a BigInt and turning it into something like: "BigInt<278934623894723>" might be our best bet. @MicahZoltu
What do you think?

@boneskull boneskull added type: bug a defect, confirmed by a maintainer status: accepting prs Mocha can use your help with this one! and removed unconfirmed-bug labels Nov 22, 2019
@MicahZoltu
Copy link
Author

Anything is better than the current situation. 😄 JSON.stringify accepts a function that will give you an opportunity to deserialize/stringify each value before it is passed along to the native stringifier. You can handle bigints in there to avoid the exception.

As for how to render, what you suggested is fine. I would also be fine with 123456n (how they are displayed in the console, just inside a string). I have a minor preference towards 123456n just because it is the JS standard way to present bigints. Since this is just for test failure output, I don't think it really matters much.

@jjsfernandez
Copy link

@boneskull i made a pull request to solve this bug, can you review it?

@chharvey
Copy link

chharvey commented Dec 17, 2020

I’m getting this issue with Node.js’s built-in assert.strictEqual function only when using with Mocha.

When running Node (v14.15.0) on the command line,

assert.strictEqual(0n, 1n);

I get the error:

Uncaught AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:

0n !== 1n

This is as expected, as the error reports what it should report.

But when running in Mocha (with gulp and ts-node):

import * as assert from 'assert'; // Node.js

describe('module', () => {
   it('test', () => {
      assert.strictEqual(0n, 1n);
   });
});

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

Any idea why assert.strictEqual runs differently in Mocha than in the Node.js REPL?

Configurations: (click to expand) Gulp file:
function test() {
	return gulp.src('./test/**/*.ts')
		.pipe(mocha({
			require: 'ts-node/register',
		}))
}
NPM dependencies:
  "devDependencies": {
    "@types/mocha": "^8.0.0",
    "@types/node": "^14.0.1",
    "gulp": "^4.0.2",
    "gulp-mocha": "^7.0.2",
    "gulp-typescript": "^5.0.1",
    "ts-node": "^9.0.0",
    "typescript": "~4.0.3"
  },

@juergba
Copy link
Contributor

juergba commented Feb 2, 2021

@chharvey If you use --no-diff to suppress Mocha’s own diff output, there should be no TypeError being thrown.

@juergba juergba removed the status: accepting prs Mocha can use your help with this one! label Feb 3, 2021
@chharvey
Copy link

chharvey commented Feb 9, 2021

@juergba Thank you for providing a fix for this. But for the record, --no-diff didn’t work for me. I’m using gulp-mocha and passed in {noDiff: true} for the options object. This suppressed the diff output, but I still got the TypeError instead of the expected AssertionError.

However, by wrapping the bigints in arrays,

assert.deepStrictEqual([0n], [1n]);

this did give me the correct error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug a defect, confirmed by a maintainer
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants