Mocha: Bigint support.

Created on 9 Nov 2019  路  5Comments  路  Source: mochajs/mocha

Prerequisites

  • [x] Checked that your issue hasn't already been filed by cross-referencing issues with the faq label
  • [x] 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.
  • [x] '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
  • [x] 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.
confirmed-bug help wanted

Most helpful comment

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

All 5 comments

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.

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?

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.

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

I鈥檓 getting this issue with Node.js鈥檚 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"
  },

Was this page helpful?
0 / 5 - 0 ratings