|
| 1 | +/** |
| 2 | + * Test suite to verify ESM and CommonJS import compatibility |
| 3 | + * This prevents regression of issue #158 (ESM import failure) |
| 4 | + */ |
| 5 | + |
| 6 | +const { describe, it } = require('mocha'); |
| 7 | +const { expect } = require('chai'); |
| 8 | +const path = require('path'); |
| 9 | +const fs = require('fs'); |
| 10 | + |
| 11 | +describe('Module Import Compatibility Tests', function() { |
| 12 | + const projectRoot = path.resolve(__dirname, '..'); |
| 13 | + const cjsDistPath = path.join(projectRoot, 'dist', 'cjs'); |
| 14 | + const esmDistPath = path.join(projectRoot, 'dist', 'esm'); |
| 15 | + |
| 16 | + before(function() { |
| 17 | + // Ensure dist directories exist |
| 18 | + if (!fs.existsSync(cjsDistPath)) { |
| 19 | + throw new Error('CommonJS dist directory not found. Run npm run build first.'); |
| 20 | + } |
| 21 | + if (!fs.existsSync(esmDistPath)) { |
| 22 | + throw new Error('ESM dist directory not found. Run npm run build first.'); |
| 23 | + } |
| 24 | + }); |
| 25 | + |
| 26 | + describe('Dual Package Structure', function() { |
| 27 | + it('should have separate CJS and ESM dist directories', function() { |
| 28 | + expect(fs.existsSync(cjsDistPath)).to.be.true; |
| 29 | + expect(fs.existsSync(esmDistPath)).to.be.true; |
| 30 | + }); |
| 31 | + |
| 32 | + it('should have package.json in CJS dist with type: commonjs', function() { |
| 33 | + const cjsPackageJsonPath = path.join(cjsDistPath, 'package.json'); |
| 34 | + expect(fs.existsSync(cjsPackageJsonPath)).to.be.true; |
| 35 | + |
| 36 | + const cjsPackageJson = JSON.parse(fs.readFileSync(cjsPackageJsonPath, 'utf8')); |
| 37 | + expect(cjsPackageJson.type).to.equal('commonjs'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should have package.json in ESM dist with type: module', function() { |
| 41 | + const esmPackageJsonPath = path.join(esmDistPath, 'package.json'); |
| 42 | + expect(fs.existsSync(esmPackageJsonPath)).to.be.true; |
| 43 | + |
| 44 | + const esmPackageJson = JSON.parse(fs.readFileSync(esmPackageJsonPath, 'utf8')); |
| 45 | + expect(esmPackageJson.type).to.equal('module'); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('CommonJS Import Tests', function() { |
| 50 | + it('should successfully require the CJS build', function() { |
| 51 | + const cjsIndexPath = path.join(cjsDistPath, 'index.js'); |
| 52 | + expect(fs.existsSync(cjsIndexPath)).to.be.true; |
| 53 | + |
| 54 | + // This should not throw |
| 55 | + const cjsModule = require(cjsIndexPath); |
| 56 | + expect(cjsModule).to.be.an('object'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should successfully require the CJS index-return-cons build', function() { |
| 60 | + const cjsReturnConsPath = path.join(cjsDistPath, 'index-return-cons.js'); |
| 61 | + if (fs.existsSync(cjsReturnConsPath)) { |
| 62 | + // This should not throw |
| 63 | + const cjsReturnConsModule = require(cjsReturnConsPath); |
| 64 | + expect(cjsReturnConsModule).to.be.an('object'); |
| 65 | + } |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + describe('ESM Import Tests', function() { |
| 70 | + it('should have .js extensions in ESM imports', async function() { |
| 71 | + const esmIndexPath = path.join(esmDistPath, 'index.js'); |
| 72 | + expect(fs.existsSync(esmIndexPath)).to.be.true; |
| 73 | + |
| 74 | + const esmIndexContent = fs.readFileSync(esmIndexPath, 'utf8'); |
| 75 | + |
| 76 | + // Check that relative imports have .js extensions |
| 77 | + const relativeImportRegex = /from\s+['"]\.\/[^'"]*(?<!\.js)['"]/g; |
| 78 | + const invalidImports = esmIndexContent.match(relativeImportRegex); |
| 79 | + |
| 80 | + if (invalidImports) { |
| 81 | + throw new Error(`ESM file has relative imports without .js extensions: ${invalidImports.join(', ')}`); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + it('should successfully import the ESM build dynamically', async function() { |
| 86 | + const esmIndexPath = path.join(esmDistPath, 'index.js'); |
| 87 | + |
| 88 | + // Use dynamic import to test ESM compatibility |
| 89 | + const esmModule = await import('file://' + esmIndexPath.replace(/\\/g, '/')); |
| 90 | + expect(typeof esmModule).to.equal('object'); |
| 91 | + expect(esmModule).to.not.be.null; |
| 92 | + }); |
| 93 | + |
| 94 | + it('should successfully import the ESM index-return-cons build dynamically', async function() { |
| 95 | + const esmReturnConsPath = path.join(esmDistPath, 'index-return-cons.js'); |
| 96 | + if (fs.existsSync(esmReturnConsPath)) { |
| 97 | + // Use dynamic import to test ESM compatibility |
| 98 | + const esmReturnConsModule = await import('file://' + esmReturnConsPath.replace(/\\/g, '/')); |
| 99 | + expect(typeof esmReturnConsModule).to.equal('object'); |
| 100 | + expect(esmReturnConsModule).to.not.be.null; |
| 101 | + } |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + describe('Package.json Exports Configuration', function() { |
| 106 | + it('should have correct exports field in main package.json', function() { |
| 107 | + const mainPackageJsonPath = path.join(projectRoot, 'package.json'); |
| 108 | + const mainPackageJson = JSON.parse(fs.readFileSync(mainPackageJsonPath, 'utf8')); |
| 109 | + |
| 110 | + expect(mainPackageJson.exports).to.be.an('object'); |
| 111 | + expect(mainPackageJson.exports['.']).to.be.an('object'); |
| 112 | + expect(mainPackageJson.exports['.'].require).to.equal('./dist/cjs/index.js'); |
| 113 | + expect(mainPackageJson.exports['.'].import).to.equal('./dist/esm/index.js'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should have main field pointing to CJS build', function() { |
| 117 | + const mainPackageJsonPath = path.join(projectRoot, 'package.json'); |
| 118 | + const mainPackageJson = JSON.parse(fs.readFileSync(mainPackageJsonPath, 'utf8')); |
| 119 | + |
| 120 | + expect(mainPackageJson.main).to.equal('dist/cjs/index.js'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should have module field pointing to ESM build', function() { |
| 124 | + const mainPackageJsonPath = path.join(projectRoot, 'package.json'); |
| 125 | + const mainPackageJson = JSON.parse(fs.readFileSync(mainPackageJsonPath, 'utf8')); |
| 126 | + |
| 127 | + expect(mainPackageJson.module).to.equal('dist/esm/index.js'); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + describe('File Extension Tests', function() { |
| 132 | + it('should have all relative imports in ESM files ending with .js', function() { |
| 133 | + const checkDirectory = (dirPath) => { |
| 134 | + const files = fs.readdirSync(dirPath, { withFileTypes: true }); |
| 135 | + |
| 136 | + for (const file of files) { |
| 137 | + const fullPath = path.join(dirPath, file.name); |
| 138 | + |
| 139 | + if (file.isDirectory()) { |
| 140 | + checkDirectory(fullPath); |
| 141 | + } else if (file.name.endsWith('.js') && !file.name.endsWith('.d.ts')) { |
| 142 | + const content = fs.readFileSync(fullPath, 'utf8'); |
| 143 | + |
| 144 | + // Check for relative imports without .js extension |
| 145 | + const relativeImportRegex = /(?:import|export).*?from\s+['"]\.\/[^'"]*(?<!\.js)['"];?/g; |
| 146 | + const invalidImports = content.match(relativeImportRegex); |
| 147 | + |
| 148 | + if (invalidImports) { |
| 149 | + throw new Error(`File ${fullPath} has relative imports without .js extensions: ${invalidImports.join(', ')}`); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + }; |
| 154 | + |
| 155 | + checkDirectory(esmDistPath); |
| 156 | + }); |
| 157 | + }); |
| 158 | +}); |
0 commit comments