|
1 | 1 | import { expect } from 'chai';
|
2 | 2 | import * as BSON from '../register-bson';
|
3 | 3 | import { sorted, byStrings } from './tools/utils';
|
| 4 | +import { readFile } from 'fs/promises'; |
| 5 | +import { resolve } from 'path'; |
4 | 6 |
|
5 | 7 | const EXPECTED_EXPORTS = [
|
6 | 8 | // This is our added web indicator not a real export but a small exception for this test.
|
@@ -53,4 +55,43 @@ describe('bson entrypoint', () => {
|
53 | 55 | expect(BSON.EJSON).to.be.frozen;
|
54 | 56 | expect(Object.getPrototypeOf(BSON.EJSON)).to.be.null;
|
55 | 57 | });
|
| 58 | + |
| 59 | + context('package.json entrypoint', () => { |
| 60 | + let pkg: typeof import('../../package.json'); |
| 61 | + before(async () => { |
| 62 | + pkg = await readFile(resolve(__dirname, '../../package.json'), { |
| 63 | + encoding: 'utf8' |
| 64 | + // JSON.parse will preserve key order |
| 65 | + }).then(c => JSON.parse(c)); |
| 66 | + }); |
| 67 | + |
| 68 | + it('maintains the order of keys in exports conditions', async () => { |
| 69 | + expect(pkg).property('exports').is.a('object'); |
| 70 | + expect(pkg).nested.property('exports.import').is.a('object'); |
| 71 | + expect(pkg).nested.property('exports.require').is.a('object'); |
| 72 | + |
| 73 | + expect( |
| 74 | + Object.keys(pkg.exports), |
| 75 | + 'Order matters in the exports fields. import/require need to proceed the "bundler" targets (RN/browser) and react-native MUST proceed browser' |
| 76 | + ).to.deep.equal(['import', 'require', 'react-native', 'browser']); |
| 77 | + |
| 78 | + // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-7.html#packagejson-exports-imports-and-self-referencing |
| 79 | + expect( |
| 80 | + Object.keys(pkg.exports.import), |
| 81 | + 'TS docs say that `types` should ALWAYS proceed `default`' |
| 82 | + ).to.deep.equal(['types', 'default']); |
| 83 | + expect( |
| 84 | + Object.keys(pkg.exports.require), |
| 85 | + 'TS docs say that `types` should ALWAYS proceed `default`' |
| 86 | + ).to.deep.equal(['types', 'default']); |
| 87 | + |
| 88 | + expect(Object.keys(pkg['compass:exports'])).to.deep.equal(['import', 'require']); |
| 89 | + }); |
| 90 | + |
| 91 | + it('has the equivalent "bson.d.ts" value for all "types" specifiers', () => { |
| 92 | + expect(pkg).property('types', 'bson.d.ts'); |
| 93 | + expect(pkg).nested.property('exports.import.types', './bson.d.ts'); |
| 94 | + expect(pkg).nested.property('exports.require.types', './bson.d.ts'); |
| 95 | + }); |
| 96 | + }); |
56 | 97 | });
|
0 commit comments