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 are not supported in .deep.equal() #1623

Open
SmashingQuasar opened this issue Jun 6, 2024 · 3 comments
Open

BigInt are not supported in .deep.equal() #1623

SmashingQuasar opened this issue Jun 6, 2024 · 3 comments
Labels
waiting-on-reporter Issues where we are waiting to hear back from the original reporter

Comments

@SmashingQuasar
Copy link

SmashingQuasar commented Jun 6, 2024

Hey!

I am using the latest version of Chai and it appears if you try to deep equal an object that has a property of type BigInt, Chai will throw an Error.

Here is a code sample that should reproduce the issue (TypeScript):

import { describe, it } from "mocha";
import { expect } from "chai";

class Foo {
  private bar: BigInt = 0n;
}

describe("BigInt are not supported", (): void => {
  it("should work", (): void => {
    const instance: Foo = new Foo();
    const secondInstance: Foo = new Foo();

    expect(instance).to.deep.equal(secondInstance); // This will throw
  });
});

Support for BigInt was apparently added a while back but it seems it no longer works or deep.equal did not work.

@SmashingQuasar
Copy link
Author

SmashingQuasar commented Jun 6, 2024

As a workaround for anyone encountering this issue, you add this to your runner pre-run script:

	// eslint-disable-next-line no-extend-native -- Chai does not support BigInt serialisation.
	Object.defineProperty(
		BigInt.prototype,
		"toJSON",
		{
			writable: false,
			enumerable: false,
			configurable: false,
			value: function(): string
			{
				// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/restrict-template-expressions
				return `${this.toString()}n`;
			},
		}
	);

It's unclean but at least it works.

@43081j
Copy link
Contributor

43081j commented Jun 8, 2024

i tried the exact code in the first post and it seems to work fine for me

could you give some more info, like how you're building and running this test?

e.g. do you use ts-node or do you run the built JS? do you output commonjs or esm? etc etc

@SmashingQuasar
Copy link
Author

i tried the exact code in the first post and it seems to work fine for me

could you give some more info, like how you're building and running this test?

e.g. do you use ts-node or do you run the built JS? do you output commonjs or esm? etc etc

Hey!

Thanks for your answer. To help understand this problem, I will create a repository aiming to replicate the issue with a few dependencies and code as possible and post it here. That way I will either find my own mistake or save you time instead of making you blindly look for the problem.

@koddsson koddsson added the waiting-on-reporter Issues where we are waiting to hear back from the original reporter label Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting-on-reporter Issues where we are waiting to hear back from the original reporter
Projects
None yet
Development

No branches or pull requests

3 participants