bigint-stringify-test.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. var mocha = require('mocha')
  2. , assert = require('chai').assert
  3. , expect = require('chai').expect
  4. , BigNumber = require('bignumber.js')
  5. ;
  6. describe("Testing native BigInt support: stringify", function () {
  7. if (typeof (BigInt) === 'undefined') {
  8. console.log('No native BigInt');
  9. return;
  10. }
  11. it("Should show JSONbig can stringify native BigInt", function (done) {
  12. var JSONbig = require('../index');
  13. var obj = {
  14. // We cannot use n-literals - otherwise older NodeJS versions fail on this test
  15. big: eval("123456789012345678901234567890n"),
  16. small: -42,
  17. bigConstructed: BigInt(1),
  18. smallConstructed: Number(2),
  19. };
  20. expect(obj.small.toString(), "string from small int").to.equal("-42");
  21. expect(obj.big.toString(), "string from big int").to.equal("123456789012345678901234567890");
  22. expect(typeof obj.big, "typeof big int").to.equal('bigint');
  23. var output = JSONbig.stringify(obj);
  24. expect(output).to.equal(
  25. '{' +
  26. '"big":123456789012345678901234567890,' +
  27. '"small":-42,' +
  28. '"bigConstructed":1,' +
  29. '"smallConstructed":2' +
  30. '}'
  31. );
  32. done();
  33. });
  34. });