/
bigint_test.js
59 lines (51 loc) · 1.61 KB
/
bigint_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* globals BigInt */
'use strict';
const createBSON = require('../utils');
const BSON = require('../..');
const bson = createBSON();
try {
BigInt(0);
// will throw on the line above if BigInt is not supported in the runtime
exports['Should error on serialize bigint'] = function (test) {
const testDoc = { b: BigInt(32) };
try {
bson.serialize(testDoc)
test.ok(false);
} catch (error) {
test.ok(error instanceof TypeError);
test.ok(error.message === 'Unsupported type BigInt, please use Decimal128');
}
test.done();
};
exports['Should error on serialize bigint inside array'] = function (test) {
const testDoc = { b: [0, 1, BigInt(0x1ffffffff)] };
try {
bson.serialize(testDoc)
test.ok(false);
} catch (error) {
test.ok(error instanceof TypeError);
test.ok(error.message === 'Unsupported type BigInt, please use Decimal128');
}
test.done();
};
exports['Should error on serialize bigint inside subdocument'] = function (test) {
const testDoc = { b: { a: BigInt(0x1ffffffff) } };
try {
bson.serialize(testDoc)
test.ok(false);
} catch (error) {
test.ok(error instanceof TypeError);
test.ok(error.message === 'Unsupported type BigInt, please use Decimal128');
}
test.done();
};
exports['Should support conversion on Long type'] = function (test) {
const long = BSON.Long.fromBigInt(BigInt(200));
test.ok(long._bsontype === 'Long');
test.ok(long.toNumber() === 200);
test.ok(long.toBigInt() === BigInt(200));
test.done();
}
} catch (_) {
// 'JS VM does not support BigInt'
}