|
| 1 | +import { generate } from '../src/generator'; |
| 2 | +import { Project, ModuleKind, ModuleResolutionKind } from 'ts-morph'; |
| 3 | +import * as fs from 'fs'; |
| 4 | +import * as path from 'path'; |
| 5 | + |
| 6 | +describe('ESM support in generator', () => { |
| 7 | + const outputDir = path.join(__dirname, 'test-output'); |
| 8 | + const inputFile = path.join(__dirname, 'test-service.edmx'); |
| 9 | + |
| 10 | + jest.setTimeout(60000); // Increase timeout to 60 seconds for all tests in this describe block |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + if (fs.existsSync(outputDir)) { |
| 14 | + fs.rmSync(outputDir, { recursive: true }); |
| 15 | + } |
| 16 | + fs.mkdirSync(outputDir); |
| 17 | + }); |
| 18 | + |
| 19 | + afterEach(() => { |
| 20 | + fs.rmSync(outputDir, { recursive: true }); |
| 21 | + }); |
| 22 | + |
| 23 | + test('project options are set correctly for ESM', async () => { |
| 24 | + console.time('project options test'); |
| 25 | + console.log('Starting project options test'); |
| 26 | + const options = { |
| 27 | + input: inputFile, |
| 28 | + outputDir, |
| 29 | + generateESM: true, |
| 30 | + forceOverwrite: true, |
| 31 | + packageJson: true, |
| 32 | + tsconfig: path.join(__dirname, 'tsconfig.json') |
| 33 | + }; |
| 34 | + |
| 35 | + await generate(options); |
| 36 | + |
| 37 | + const serviceDir = fs.readdirSync(outputDir)[0]; // Assuming only one service is generated |
| 38 | + const servicePath = path.join(outputDir, serviceDir); |
| 39 | + |
| 40 | + console.log('Service directory contents:', fs.readdirSync(servicePath)); |
| 41 | + |
| 42 | + const tsConfigPath = path.join(servicePath, 'tsconfig.json'); |
| 43 | + if (fs.existsSync(tsConfigPath)) { |
| 44 | + const tsConfig = JSON.parse(fs.readFileSync(tsConfigPath, 'utf-8')); |
| 45 | + console.log('tsconfig.json:', tsConfig); |
| 46 | + expect(tsConfig.compilerOptions.module).toBe('esnext'); |
| 47 | + expect(tsConfig.compilerOptions.moduleResolution).toBe('nodenext'); |
| 48 | + } else { |
| 49 | + console.log('tsconfig.json not found'); |
| 50 | + } |
| 51 | + console.timeEnd('project options test'); |
| 52 | + }); |
| 53 | + |
| 54 | + test('generated files are compatible with ESM', async () => { |
| 55 | + console.time('generated files test'); |
| 56 | + console.log('Starting generated files test'); |
| 57 | + const options = { |
| 58 | + input: inputFile, |
| 59 | + outputDir, |
| 60 | + generateESM: true, |
| 61 | + forceOverwrite: true, |
| 62 | + packageJson: true, |
| 63 | + tsconfig: path.join(__dirname, 'tsconfig.json') |
| 64 | + }; |
| 65 | + |
| 66 | + await generate(options); |
| 67 | + |
| 68 | + const serviceDir = fs.readdirSync(outputDir)[0]; // Assuming only one service is generated |
| 69 | + const servicePath = path.join(outputDir, serviceDir); |
| 70 | + |
| 71 | + console.log('Service directory contents:', fs.readdirSync(servicePath)); |
| 72 | + |
| 73 | + const indexPath = path.join(servicePath, 'index.ts'); |
| 74 | + if (fs.existsSync(indexPath)) { |
| 75 | + const indexFile = fs.readFileSync(indexPath, 'utf-8'); |
| 76 | + console.log('index.ts content:', indexFile); |
| 77 | + expect(indexFile).toContain('export * from'); |
| 78 | + expect(indexFile).not.toContain('require('); |
| 79 | + } else { |
| 80 | + console.log('index.ts not found in service directory'); |
| 81 | + } |
| 82 | + |
| 83 | + const packageJsonPath = path.join(servicePath, 'package.json'); |
| 84 | + if (fs.existsSync(packageJsonPath)) { |
| 85 | + const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf-8'); |
| 86 | + const packageJson = JSON.parse(packageJsonContent); |
| 87 | + console.log('package.json content:', packageJson); |
| 88 | + expect(packageJson.type).toBe('module'); |
| 89 | + } else { |
| 90 | + console.log('package.json not found in service directory'); |
| 91 | + } |
| 92 | + console.timeEnd('generated files test'); |
| 93 | + }); |
| 94 | +}); |
0 commit comments