|
| 1 | +async function runESModuleTests() { |
| 2 | + let passed = 0; |
| 3 | + let failed = 0; |
| 4 | + const failureDetails = []; |
| 5 | + |
| 6 | + const recordPass = (message, ...args) => { |
| 7 | + console.log(`✅ PASS: ${message}`, ...args); |
| 8 | + passed++; |
| 9 | + }; |
| 10 | + |
| 11 | + const recordFailure = (message, options = {}) => { |
| 12 | + const { error, details = [] } = options; |
| 13 | + const fullMessage = error?.message |
| 14 | + ? `${message}: ${error.message}` |
| 15 | + : message; |
| 16 | + console.log(`❌ FAIL: ${fullMessage}`); |
| 17 | + details.forEach((detail) => console.log(detail)); |
| 18 | + if (error?.stack) { |
| 19 | + console.log("Stack trace:", error.stack); |
| 20 | + } |
| 21 | + failed++; |
| 22 | + failureDetails.push(fullMessage); |
| 23 | + }; |
| 24 | + |
| 25 | + const logFinalSummary = () => { |
| 26 | + console.log("\n=== ES MODULE TEST RESULTS ==="); |
| 27 | + console.log("Tests passed:", passed); |
| 28 | + console.log("Tests failed:", failed); |
| 29 | + console.log("Total tests:", passed + failed); |
| 30 | + |
| 31 | + if (failed === 0) { |
| 32 | + console.log("ALL ES MODULE TESTS PASSED!"); |
| 33 | + } else { |
| 34 | + console.log("SOME ES MODULE TESTS FAILED!"); |
| 35 | + console.log("FAILURE DETECTED: Starting failure logging"); |
| 36 | + failureDetails.forEach((detail) => { |
| 37 | + console.log(` ❌ ${detail}`); |
| 38 | + }); |
| 39 | + } |
| 40 | + }; |
| 41 | + |
| 42 | + try { |
| 43 | + // Test 1: Load .mjs files as ES modules |
| 44 | + console.log("\n--- Test 1: Loading .mjs files as ES modules ---"); |
| 45 | + try { |
| 46 | + const moduleExports = await import("~/testSimpleESModule.mjs"); |
| 47 | + if (moduleExports) { |
| 48 | + recordPass("Module exports:", JSON.stringify(moduleExports)); |
| 49 | + } else { |
| 50 | + recordFailure("ES Module loaded but exports are null"); |
| 51 | + } |
| 52 | + |
| 53 | + if (moduleExports?.moduleType === "ES Module") { |
| 54 | + recordPass("moduleType check passed"); |
| 55 | + } else { |
| 56 | + recordFailure("moduleType check failed"); |
| 57 | + } |
| 58 | + } catch (e) { |
| 59 | + recordFailure("Error loading ES module", { error: e }); |
| 60 | + } |
| 61 | + |
| 62 | + // Test 2: Test import.meta functionality |
| 63 | + console.log("\n--- Test 2: Testing import.meta functionality ---"); |
| 64 | + try { |
| 65 | + const importMetaModule = await import("~/testImportMeta.mjs"); |
| 66 | + if ( |
| 67 | + importMetaModule && |
| 68 | + importMetaModule.default && |
| 69 | + typeof importMetaModule.default === "function" |
| 70 | + ) { |
| 71 | + const metaResults = importMetaModule.default(); |
| 72 | + console.log( |
| 73 | + "import.meta test results:", |
| 74 | + JSON.stringify(metaResults, null, 2) |
| 75 | + ); |
| 76 | + |
| 77 | + if ( |
| 78 | + metaResults && |
| 79 | + metaResults.hasImportMeta && |
| 80 | + metaResults.hasUrl && |
| 81 | + metaResults.hasDirname |
| 82 | + ) { |
| 83 | + recordPass("import.meta properties present"); |
| 84 | + console.log(" - import.meta.url:", metaResults.url); |
| 85 | + console.log(" - import.meta.dirname:", metaResults.dirname); |
| 86 | + } else { |
| 87 | + recordFailure("import.meta properties missing", { |
| 88 | + details: [ |
| 89 | + ` - hasImportMeta: ${metaResults?.hasImportMeta}`, |
| 90 | + ` - hasUrl: ${metaResults?.hasUrl}`, |
| 91 | + ` - hasDirname: ${metaResults?.hasDirname}`, |
| 92 | + ], |
| 93 | + }); |
| 94 | + } |
| 95 | + } else { |
| 96 | + recordFailure("import.meta module has no default export function"); |
| 97 | + } |
| 98 | + } catch (e) { |
| 99 | + recordFailure("Error testing import.meta", { error: e }); |
| 100 | + } |
| 101 | + |
| 102 | + // Test 3: Test Worker enhancements |
| 103 | + console.log("\n--- Test 3: Testing Worker enhancements ---"); |
| 104 | + try { |
| 105 | + const workerModule = await import("~/testWorkerFeatures.mjs"); |
| 106 | + if ( |
| 107 | + workerModule && |
| 108 | + workerModule.testWorkerFeatures && |
| 109 | + typeof workerModule.testWorkerFeatures === "function" |
| 110 | + ) { |
| 111 | + const workerResults = workerModule.testWorkerFeatures(); |
| 112 | + console.log( |
| 113 | + "Worker features test results:", |
| 114 | + JSON.stringify(workerResults, null, 2) |
| 115 | + ); |
| 116 | + |
| 117 | + if ( |
| 118 | + workerResults && |
| 119 | + workerResults.stringPathSupported && |
| 120 | + workerResults.urlObjectSupported && |
| 121 | + workerResults.tildePathSupported |
| 122 | + ) { |
| 123 | + recordPass("Worker enhancement features present"); |
| 124 | + console.log( |
| 125 | + " - String path support:", |
| 126 | + workerResults.stringPathSupported |
| 127 | + ); |
| 128 | + console.log( |
| 129 | + " - URL object support:", |
| 130 | + workerResults.urlObjectSupported |
| 131 | + ); |
| 132 | + console.log( |
| 133 | + " - Tilde path support:", |
| 134 | + workerResults.tildePathSupported |
| 135 | + ); |
| 136 | + } else { |
| 137 | + recordFailure("Worker enhancement features missing", { |
| 138 | + details: [ |
| 139 | + ` - stringPathSupported: ${workerResults?.stringPathSupported}`, |
| 140 | + ` - urlObjectSupported: ${workerResults?.urlObjectSupported}`, |
| 141 | + ` - tildePathSupported: ${workerResults?.tildePathSupported}`, |
| 142 | + ], |
| 143 | + }); |
| 144 | + } |
| 145 | + } else { |
| 146 | + recordFailure( |
| 147 | + "Worker features module has no testWorkerFeatures function" |
| 148 | + ); |
| 149 | + } |
| 150 | + } catch (e) { |
| 151 | + recordFailure("Error testing Worker features", { error: e }); |
| 152 | + } |
| 153 | + } catch (unexpectedError) { |
| 154 | + recordFailure("Unexpected ES module test harness failure", { |
| 155 | + error: unexpectedError, |
| 156 | + }); |
| 157 | + } finally { |
| 158 | + logFinalSummary(); |
| 159 | + } |
| 160 | + |
| 161 | + return { passed, failed }; |
| 162 | +} |
| 163 | + |
| 164 | +// Run the tests immediately (avoid top-level await for broader runtime support) |
| 165 | +runESModuleTests().catch((e) => { |
| 166 | + console.error("ES Module top-level failure:", e?.message ?? e); |
| 167 | +}); |
0 commit comments