|
| 1 | +// Karma configuration for Compose WASM UI tests |
| 2 | +// |
| 3 | +// Key issues this config solves: |
| 4 | +// 1. WASM file serving: webpack bundles JS but not .wasm files. The bundled code |
| 5 | +// tries to fetch .wasm relative to import.meta.url (a webpack temp path). |
| 6 | +// We add a middleware to redirect .wasm requests to the correct location. |
| 7 | +// 2. Timeouts: WasmGC module (~30 MB) compilation blocks the main thread during init. |
| 8 | +// 3. Source-map performance: skipping source-map-loader for large WASM glue files. |
| 9 | + |
| 10 | +const path = require('path'); |
| 11 | +const fs = require('fs'); |
| 12 | + |
| 13 | +function findRepoRoot(startDir) { |
| 14 | + let current = startDir; |
| 15 | + while (current !== path.dirname(current)) { |
| 16 | + if ( |
| 17 | + fs.existsSync(path.join(current, 'settings.gradle.kts')) || |
| 18 | + fs.existsSync(path.join(current, 'settings.gradle')) |
| 19 | + ) { |
| 20 | + return current; |
| 21 | + } |
| 22 | + current = path.dirname(current); |
| 23 | + } |
| 24 | + return startDir; |
| 25 | +} |
| 26 | + |
| 27 | +const repoRoot = findRepoRoot(process.cwd()); |
| 28 | +const composeResourcesAbsolutePath = path.resolve( |
| 29 | + repoRoot, |
| 30 | + 'app', |
| 31 | + 'web', |
| 32 | + 'build', |
| 33 | + 'processedResources', |
| 34 | + 'wasmJs', |
| 35 | + 'main', |
| 36 | + 'composeResources' |
| 37 | +).replace(/\\/g, '/'); |
| 38 | + |
| 39 | +// Middleware to serve .wasm files from the correct location |
| 40 | +// Webpack sets import.meta.url to a temp directory, so fetch('x.wasm') goes to the |
| 41 | +// wrong path. This middleware intercepts .wasm requests and serves them from kotlin/. |
| 42 | +function wasmFileMiddleware(config) { |
| 43 | + const basePath = config.basePath; |
| 44 | + const composeResourcesRoot = path.resolve( |
| 45 | + repoRoot, |
| 46 | + 'app', |
| 47 | + 'web', |
| 48 | + 'build', |
| 49 | + 'processedResources', |
| 50 | + 'wasmJs', |
| 51 | + 'main' |
| 52 | + ); |
| 53 | + return function(req, res, next) { |
| 54 | + const cleanUrl = req.url.split('?')[0]; |
| 55 | + |
| 56 | + if (cleanUrl.includes('/composeResources/')) { |
| 57 | + const composeIndex = cleanUrl.indexOf('/composeResources/'); |
| 58 | + const relativePath = cleanUrl.substring(composeIndex + 1); |
| 59 | + const resourcePath = path.join(composeResourcesRoot, relativePath); |
| 60 | + if (fs.existsSync(resourcePath)) { |
| 61 | + res.setHeader('Content-Type', 'application/octet-stream'); |
| 62 | + fs.createReadStream(resourcePath).pipe(res); |
| 63 | + return; |
| 64 | + } |
| 65 | + } |
| 66 | + if (cleanUrl.endsWith('.wasm') && !cleanUrl.endsWith('sql-wasm.wasm')) { |
| 67 | + const filename = path.basename(cleanUrl); |
| 68 | + const wasmPath = path.join(basePath, 'kotlin', filename); |
| 69 | + if (fs.existsSync(wasmPath)) { |
| 70 | + res.setHeader('Content-Type', 'application/wasm'); |
| 71 | + res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp'); |
| 72 | + res.setHeader('Cross-Origin-Opener-Policy', 'same-origin'); |
| 73 | + fs.createReadStream(wasmPath).pipe(res); |
| 74 | + return; |
| 75 | + } |
| 76 | + } |
| 77 | + next(); |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +// Register middleware as an inline Karma plugin |
| 82 | +config.plugins = config.plugins || []; |
| 83 | +config.plugins.push({ |
| 84 | + 'middleware:wasmFileServer': ['factory', function() { |
| 85 | + return wasmFileMiddleware(config); |
| 86 | + }] |
| 87 | +}); |
| 88 | + |
| 89 | +config.set({ |
| 90 | + beforeMiddleware: ['wasmFileServer'], |
| 91 | + middleware: ['wasmFileServer'], |
| 92 | + proxies: Object.assign({}, config.proxies, { |
| 93 | + '/composeResources/': `/absolute/${composeResourcesAbsolutePath}/` |
| 94 | + }), |
| 95 | + |
| 96 | + // WasmGC compilation blocks the main thread for ~15-18 min on the first run. |
| 97 | + // pingTimeout must exceed compilation time so the socket stays alive. |
| 98 | + pingTimeout: 1200000, // 20 min |
| 99 | + browserSocketTimeout: 1200000, // 20 min |
| 100 | + browserNoActivityTimeout: 1500000, // 25 min |
| 101 | + browserDisconnectTimeout: 60000, // 1 min |
| 102 | + browserDisconnectTolerance: 1, |
| 103 | + captureTimeout: 60000, // 1 min |
| 104 | + processKillTimeout: 5000, // 5 sec |
| 105 | + |
| 106 | + client: { |
| 107 | + mocha: { |
| 108 | + timeout: 60000 // 1 min |
| 109 | + } |
| 110 | + }, |
| 111 | + |
| 112 | + customLaunchers: { |
| 113 | + ChromeHeadlessForWasm: { |
| 114 | + base: 'ChromeHeadless', |
| 115 | + flags: [ |
| 116 | + '--no-sandbox', |
| 117 | + '--enable-features=SharedArrayBuffer' |
| 118 | + ] |
| 119 | + } |
| 120 | + }, |
| 121 | + browsers: ['ChromeHeadlessForWasm'] |
| 122 | +}); |
| 123 | + |
| 124 | +// Speed up webpack compilation by skipping source-map-loader for large WASM glue files. |
| 125 | +if (config.webpack && config.webpack.module && config.webpack.module.rules) { |
| 126 | + config.webpack.module.rules.forEach(function(rule) { |
| 127 | + if (rule.use && rule.use.indexOf('source-map-loader') !== -1) { |
| 128 | + rule.exclude = [/skiko\.m?js$/, /\.uninstantiated\.m?js$/]; |
| 129 | + } |
| 130 | + }); |
| 131 | +} |
0 commit comments