|
| 1 | +/** |
| 2 | + * ESM Exports Test Suite |
| 3 | + * |
| 4 | + * Purpose: Verify that ESM and CJS builds export correctly and package.json |
| 5 | + * has proper exports field configuration for ESM compatibility. |
| 6 | + */ |
| 7 | + |
| 8 | +import * as fs from 'fs'; |
| 9 | +import * as path from 'path'; |
| 10 | + |
| 11 | +describe('ESM Exports Tests', () => { |
| 12 | + const distPath = path.join(__dirname, '..', 'dist'); |
| 13 | + const esmIndexPath = path.join(distPath, 'esm', 'src', 'index.js'); |
| 14 | + const cjsIndexPath = path.join(distPath, 'cjs', 'src', 'index.js'); |
| 15 | + const esmRequestPath = path.join(distPath, 'esm', 'src', 'lib', 'request.js'); |
| 16 | + const cjsRequestPath = path.join(distPath, 'cjs', 'src', 'lib', 'request.js'); |
| 17 | + |
| 18 | + describe('ESM Build Exports', () => { |
| 19 | + it('should have ESM build index.js file', () => { |
| 20 | + expect(fs.existsSync(esmIndexPath)).toBe(true); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should export getData from ESM request.js', () => { |
| 24 | + expect(fs.existsSync(esmRequestPath)).toBe(true); |
| 25 | + const requestContent = fs.readFileSync(esmRequestPath, 'utf-8'); |
| 26 | + expect(requestContent).toContain('export function getData'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should re-export getData from ESM index.js', () => { |
| 30 | + const indexContent = fs.readFileSync(esmIndexPath, 'utf-8'); |
| 31 | + expect(indexContent).toContain("export * from './lib/request'"); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should verify getData is a named export in ESM build', () => { |
| 35 | + const requestContent = fs.readFileSync(esmRequestPath, 'utf-8'); |
| 36 | + expect(requestContent).toMatch(/export\s+(async\s+)?function\s+getData/); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('CJS Build Exports', () => { |
| 41 | + it('should have CJS build index.js file', () => { |
| 42 | + expect(fs.existsSync(cjsIndexPath)).toBe(true); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should export getData from CJS request.js using exports.getData', () => { |
| 46 | + expect(fs.existsSync(cjsRequestPath)).toBe(true); |
| 47 | + const requestContent = fs.readFileSync(cjsRequestPath, 'utf-8'); |
| 48 | + expect(requestContent).toContain('exports.getData'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should re-export getData from CJS index.js using __exportStar', () => { |
| 52 | + const indexContent = fs.readFileSync(cjsIndexPath, 'utf-8'); |
| 53 | + expect(indexContent).toContain('__exportStar(require("./lib/request")'); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('Package.json Exports Configuration', () => { |
| 58 | + it('should have exports field in package.json', () => { |
| 59 | + const packageJsonPath = path.join(__dirname, '..', 'package.json'); |
| 60 | + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')); |
| 61 | + expect(packageJson.exports).toBeDefined(); |
| 62 | + expect(packageJson.exports['.']).toBeDefined(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should have import condition pointing to ESM build', () => { |
| 66 | + const packageJsonPath = path.join(__dirname, '..', 'package.json'); |
| 67 | + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')); |
| 68 | + expect(packageJson.exports['.'].import).toBeDefined(); |
| 69 | + const importPath = packageJson.exports['.'].import.default || packageJson.exports['.'].import; |
| 70 | + expect(importPath).toContain('esm'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should have require condition pointing to CJS build', () => { |
| 74 | + const packageJsonPath = path.join(__dirname, '..', 'package.json'); |
| 75 | + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')); |
| 76 | + expect(packageJson.exports['.'].require).toBeDefined(); |
| 77 | + const requirePath = packageJson.exports['.'].require.default || packageJson.exports['.'].require; |
| 78 | + expect(requirePath).toContain('cjs'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should have types specified for both import and require', () => { |
| 82 | + const packageJsonPath = path.join(__dirname, '..', 'package.json'); |
| 83 | + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')); |
| 84 | + const exports = packageJson.exports['.']; |
| 85 | + expect(exports.import.types).toBeDefined(); |
| 86 | + expect(exports.require.types).toBeDefined(); |
| 87 | + expect(exports.import.types).toContain('types'); |
| 88 | + expect(exports.require.types).toContain('types'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should verify ESM build file exists at exports path', () => { |
| 92 | + const packageJsonPath = path.join(__dirname, '..', 'package.json'); |
| 93 | + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')); |
| 94 | + const importPath = packageJson.exports['.'].import.default || packageJson.exports['.'].import; |
| 95 | + const fullPath = path.join(__dirname, '..', importPath); |
| 96 | + expect(fs.existsSync(fullPath)).toBe(true); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should verify CJS build file exists at exports path', () => { |
| 100 | + const packageJsonPath = path.join(__dirname, '..', 'package.json'); |
| 101 | + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')); |
| 102 | + const requirePath = packageJson.exports['.'].require.default || packageJson.exports['.'].require; |
| 103 | + const fullPath = path.join(__dirname, '..', requirePath); |
| 104 | + expect(fs.existsSync(fullPath)).toBe(true); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + describe('Source Code Imports', () => { |
| 109 | + it('should be able to import getData as named export from source', async () => { |
| 110 | + const { getData } = await import('../src'); |
| 111 | + expect(getData).toBeDefined(); |
| 112 | + expect(typeof getData).toBe('function'); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should verify getData is available in all exports from source', async () => { |
| 116 | + const allExports = await import('../src'); |
| 117 | + expect(allExports.getData).toBeDefined(); |
| 118 | + expect(typeof allExports.getData).toBe('function'); |
| 119 | + }); |
| 120 | + }); |
| 121 | +}); |
0 commit comments