|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +const path = require('path'); |
| 6 | +const assert = require('assert'); |
| 7 | +const utils = require('../utils.js'); |
| 8 | + |
| 9 | +assert(!module.parent); |
| 10 | +assert(__dirname === process.cwd()); |
| 11 | + |
| 12 | +const target = process.argv[2] || 'host'; |
| 13 | + |
| 14 | +console.log('Testing unsupported ESM features detection...'); |
| 15 | + |
| 16 | +// Test 1: import.meta detection |
| 17 | +console.log('\n=== Test 1: import.meta ==='); |
| 18 | +{ |
| 19 | + const input = './test-import-meta.mjs'; |
| 20 | + const output = './run-time/test-import-meta.exe'; |
| 21 | + const newcomers = ['run-time/test-import-meta.exe']; |
| 22 | + |
| 23 | + const before = utils.filesBefore(newcomers); |
| 24 | + utils.mkdirp.sync(path.dirname(output)); |
| 25 | + |
| 26 | + // Capture stdout to check for warnings |
| 27 | + const result = utils.pkg.sync( |
| 28 | + ['--target', target, '--output', output, input], |
| 29 | + ['inherit', 'pipe', 'inherit'], |
| 30 | + ); |
| 31 | + |
| 32 | + // Verify warning was emitted |
| 33 | + assert( |
| 34 | + result.includes('import.meta') || |
| 35 | + result.includes('Cannot transform ESM module'), |
| 36 | + 'Should warn about import.meta usage', |
| 37 | + ); |
| 38 | + console.log('✓ import.meta detection working'); |
| 39 | + |
| 40 | + // Cleanup |
| 41 | + utils.filesAfter(before, newcomers); |
| 42 | +} |
| 43 | + |
| 44 | +// Test 2: top-level await detection |
| 45 | +console.log('\n=== Test 2: top-level await ==='); |
| 46 | +{ |
| 47 | + const input = './test-top-level-await.mjs'; |
| 48 | + const output = './run-time/test-top-level-await.exe'; |
| 49 | + const newcomers = ['run-time/test-top-level-await.exe']; |
| 50 | + |
| 51 | + const before = utils.filesBefore(newcomers); |
| 52 | + utils.mkdirp.sync(path.dirname(output)); |
| 53 | + |
| 54 | + const result = utils.pkg.sync( |
| 55 | + ['--target', target, '--output', output, input], |
| 56 | + ['inherit', 'pipe', 'inherit'], |
| 57 | + ); |
| 58 | + |
| 59 | + // Verify warning was emitted |
| 60 | + assert( |
| 61 | + result.includes('top-level await') || |
| 62 | + result.includes('Cannot transform ESM module'), |
| 63 | + 'Should warn about top-level await usage', |
| 64 | + ); |
| 65 | + console.log('✓ top-level await detection working'); |
| 66 | + |
| 67 | + // Cleanup |
| 68 | + utils.filesAfter(before, newcomers); |
| 69 | +} |
| 70 | + |
| 71 | +// Test 3: top-level for-await-of detection |
| 72 | +console.log('\n=== Test 3: top-level for-await-of ==='); |
| 73 | +{ |
| 74 | + const input = './test-for-await-of.mjs'; |
| 75 | + const output = './run-time/test-for-await-of.exe'; |
| 76 | + const newcomers = ['run-time/test-for-await-of.exe']; |
| 77 | + |
| 78 | + const before = utils.filesBefore(newcomers); |
| 79 | + utils.mkdirp.sync(path.dirname(output)); |
| 80 | + |
| 81 | + const result = utils.pkg.sync( |
| 82 | + ['--target', target, '--output', output, input], |
| 83 | + ['inherit', 'pipe', 'inherit'], |
| 84 | + ); |
| 85 | + |
| 86 | + // Verify warning was emitted |
| 87 | + assert( |
| 88 | + result.includes('for-await-of') || |
| 89 | + result.includes('Cannot transform ESM module'), |
| 90 | + 'Should warn about top-level for-await-of usage', |
| 91 | + ); |
| 92 | + console.log('✓ top-level for-await-of detection working'); |
| 93 | + |
| 94 | + // Cleanup |
| 95 | + utils.filesAfter(before, newcomers); |
| 96 | +} |
| 97 | + |
| 98 | +// Test 4: multiple unsupported features detection |
| 99 | +console.log('\n=== Test 4: multiple unsupported features ==='); |
| 100 | +{ |
| 101 | + const input = './test-multiple-features.mjs'; |
| 102 | + const output = './run-time/test-multiple.exe'; |
| 103 | + const newcomers = ['run-time/test-multiple.exe']; |
| 104 | + |
| 105 | + const before = utils.filesBefore(newcomers); |
| 106 | + utils.mkdirp.sync(path.dirname(output)); |
| 107 | + |
| 108 | + const result = utils.pkg.sync( |
| 109 | + ['--target', target, '--output', output, input], |
| 110 | + ['inherit', 'pipe', 'inherit'], |
| 111 | + ); |
| 112 | + |
| 113 | + // Verify multiple warnings were emitted |
| 114 | + const hasImportMeta = result.includes('import.meta'); |
| 115 | + const hasTopLevelAwait = result.includes('top-level await'); |
| 116 | + const hasForAwaitOf = result.includes('for-await-of'); |
| 117 | + const hasGeneralWarning = result.includes('Cannot transform ESM module'); |
| 118 | + |
| 119 | + assert( |
| 120 | + hasImportMeta || hasTopLevelAwait || hasForAwaitOf || hasGeneralWarning, |
| 121 | + 'Should warn about multiple unsupported features', |
| 122 | + ); |
| 123 | + |
| 124 | + console.log('✓ Multiple features detection working'); |
| 125 | + console.log(' - import.meta detected:', hasImportMeta); |
| 126 | + console.log(' - top-level await detected:', hasTopLevelAwait); |
| 127 | + console.log(' - top-level for-await-of detected:', hasForAwaitOf); |
| 128 | + |
| 129 | + // Cleanup |
| 130 | + utils.filesAfter(before, newcomers); |
| 131 | +} |
| 132 | + |
| 133 | +console.log('\n✅ All unsupported ESM features correctly detected!'); |
0 commit comments