/
exports.test.ts
99 lines (87 loc) · 3.16 KB
/
exports.test.ts
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { expect } from 'chai';
import * as BSON from '../register-bson';
import { sorted, byStrings } from './tools/utils';
import { readFile } from 'fs/promises';
import { resolve } from 'path';
const EXPECTED_EXPORTS = [
// This is our added web indicator not a real export but a small exception for this test.
'__isWeb__',
'__noBigInt__',
'BSONType',
'BSONValue',
'BSONVersionError',
'EJSON',
'Code',
'BSONSymbol',
'DBRef',
'Binary',
'ObjectId',
'onDemand',
'UUID',
'Long',
'Timestamp',
'Double',
'Int32',
'MinKey',
'MaxKey',
'BSONRegExp',
'Decimal128',
'BSONError',
'BSONRuntimeError',
'BSONOffsetError',
'setInternalBufferSize',
'serialize',
'serializeWithBufferAndIndex',
'deserialize',
'calculateObjectSize',
'deserializeStream',
'BSON'
];
const EXPECTED_EJSON_EXPORTS = ['parse', 'stringify', 'serialize', 'deserialize'];
describe('bson entrypoint', () => {
it('should export all and only the expected keys in expected_exports', () => {
expect(sorted(Object.keys(BSON), byStrings)).to.deep.equal(sorted(EXPECTED_EXPORTS, byStrings));
});
it('should export all and only the expected keys in expected_ejson_exports', () => {
expect(sorted(Object.keys(BSON.EJSON), byStrings)).to.deep.equal(
sorted(EXPECTED_EJSON_EXPORTS, byStrings)
);
});
it('EJSON export is frozen and does not inherit the global prototype', () => {
expect(BSON.EJSON).to.be.frozen;
expect(Object.getPrototypeOf(BSON.EJSON)).to.be.null;
});
context('package.json entrypoint', () => {
let pkg: typeof import('../../package.json');
before(async () => {
pkg = await readFile(resolve(__dirname, '../../package.json'), {
encoding: 'utf8'
// JSON.parse will preserve key order
}).then(c => JSON.parse(c));
});
it('maintains the order of keys in exports conditions', async () => {
expect(pkg).property('exports').is.a('object');
expect(pkg).nested.property('exports.import').is.a('object');
expect(pkg).nested.property('exports.require').is.a('object');
expect(
Object.keys(pkg.exports),
'Order matters in the exports fields. import/require need to proceed the "bundler" targets (RN/browser) and react-native MUST proceed browser'
).to.deep.equal(['import', 'require', 'react-native', 'browser']);
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-7.html#packagejson-exports-imports-and-self-referencing
expect(
Object.keys(pkg.exports.import),
'TS docs say that `types` should ALWAYS proceed `default`'
).to.deep.equal(['types', 'default']);
expect(
Object.keys(pkg.exports.require),
'TS docs say that `types` should ALWAYS proceed `default`'
).to.deep.equal(['types', 'default']);
expect(Object.keys(pkg['compass:exports'])).to.deep.equal(['import', 'require']);
});
it('has the equivalent "bson.d.ts" value for all "types" specifiers', () => {
expect(pkg).property('types', 'bson.d.ts');
expect(pkg).nested.property('exports.import.types', './bson.d.ts');
expect(pkg).nested.property('exports.require.types', './bson.d.ts');
});
});
});