Skip to content

Commit 15fc103

Browse files
committed
feat: improve unit test reports on esm loader tests
1 parent 7ca43ef commit 15fc103

File tree

1 file changed

+53
-18
lines changed

1 file changed

+53
-18
lines changed

TestRunner/app/tests/HttpEsmLoaderTests.js

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,47 @@
33

44
describe("HTTP ESM Loader", function() {
55

6+
function formatError(e) {
7+
try {
8+
if (!e) return "(no error)";
9+
if (e instanceof Error) return e.message;
10+
if (typeof e === "string") return e;
11+
if (e && typeof e.message === "string") return e.message;
12+
return JSON.stringify(e);
13+
} catch (_) {
14+
return String(e);
15+
}
16+
}
17+
18+
function withTimeout(promise, ms, label) {
19+
return new Promise(function(resolve, reject) {
20+
var timer = setTimeout(function() {
21+
reject(new Error("Timeout after " + ms + "ms" + (label ? ": " + label : "")));
22+
}, ms);
23+
24+
promise.then(function(value) {
25+
clearTimeout(timer);
26+
resolve(value);
27+
}).catch(function(err) {
28+
clearTimeout(timer);
29+
reject(err);
30+
});
31+
});
32+
}
33+
634
function getHostOrigin() {
735
try {
836
var reportUrl = NSProcessInfo.processInfo.environment.objectForKey("REPORT_BASEURL");
937
if (!reportUrl) return null;
1038
// REPORT_BASEURL is like: http://[::1]:63846/junit_report
11-
return new URL(String(reportUrl)).origin;
39+
// In CI the host may be bound to IPv6 loopback; normalize to IPv4 loopback to avoid
40+
// simulator connectivity issues/timeouts when importing HTTP modules.
41+
var u = new URL(String(reportUrl));
42+
var host = String(u.hostname);
43+
if (host === "::1" || host === "localhost") {
44+
u.hostname = "127.0.0.1";
45+
}
46+
return u.origin;
1247
} catch (e) {
1348
return null;
1449
}
@@ -206,7 +241,7 @@ describe("HTTP ESM Loader", function() {
206241
? [origin + "/esm/hmr/hot-data-ext.mjs", origin + "/esm/hmr/hot-data-ext.js"]
207242
: ["~/tests/esm/hmr/hot-data-ext.mjs"];
208243

209-
Promise.all(specs.map(function (s) { return import(s); }))
244+
withTimeout(Promise.all(specs.map(function (s) { return import(s); })), 5000, "import hot-data test modules")
210245
.then(function (mods) {
211246
var mjs = mods[0];
212247
var apiMjs = mjs && typeof mjs.testHotApi === "function" ? mjs.testHotApi() : null;
@@ -227,7 +262,7 @@ describe("HTTP ESM Loader", function() {
227262
done();
228263
})
229264
.catch(function (error) {
230-
fail("Expected hot-data test modules to import: " + (error && error.message ? error.message : String(error)));
265+
fail(new Error("Expected hot-data test modules to import: " + formatError(error)));
231266
done();
232267
});
233268
});
@@ -240,10 +275,10 @@ describe("HTTP ESM Loader", function() {
240275
return;
241276
}
242277

243-
Promise.all([
278+
withTimeout(Promise.all([
244279
import(origin + "/esm/hmr/hot-data-ext.mjs"),
245280
import(origin + "/esm/hmr/hot-data-ext.js"),
246-
])
281+
]), 5000, "import .mjs/.js hot-data modules")
247282
.then(function (mods) {
248283
var mjs = mods[0];
249284
var js = mods[1];
@@ -270,7 +305,7 @@ describe("HTTP ESM Loader", function() {
270305
done();
271306
})
272307
.catch(function (error) {
273-
fail("Expected hot.data sharing assertions to succeed: " + (error && error.message ? error.message : String(error)));
308+
fail(new Error("Expected hot.data sharing assertions to succeed: " + formatError(error)));
274309
done();
275310
});
276311
});
@@ -288,17 +323,17 @@ describe("HTTP ESM Loader", function() {
288323
var u1 = origin + "/esm/query.mjs?v=1";
289324
var u2 = origin + "/esm/query.mjs?v=2";
290325

291-
import(u1)
326+
withTimeout(import(u1), 5000, "import " + u1)
292327
.then(function (m1) {
293-
return import(u2).then(function (m2) {
328+
return withTimeout(import(u2), 5000, "import " + u2).then(function (m2) {
294329
expect(m1.query).toContain("v=1");
295330
expect(m2.query).toContain("v=2");
296331
expect(m1.query).not.toBe(m2.query);
297332
done();
298333
});
299334
})
300335
.catch(function (error) {
301-
fail("Expected host HTTP module imports to succeed: " + (error && error.message ? error.message : String(error)));
336+
fail(new Error("Expected host HTTP module imports to succeed: " + formatError(error)));
302337
done();
303338
});
304339
});
@@ -314,9 +349,9 @@ describe("HTTP ESM Loader", function() {
314349
var u1 = origin + "/ns/m/query.mjs?v=1";
315350
var u2 = origin + "/ns/m/query.mjs?v=2";
316351

317-
import(u1)
352+
withTimeout(import(u1), 5000, "import " + u1)
318353
.then(function (m1) {
319-
return import(u2).then(function (m2) {
354+
return withTimeout(import(u2), 5000, "import " + u2).then(function (m2) {
320355
// With cache-buster normalization, both imports should map to the same cache key.
321356
// The second import should reuse the first evaluated module.
322357
expect(m2.evaluatedAt).toBe(m1.evaluatedAt);
@@ -325,7 +360,7 @@ describe("HTTP ESM Loader", function() {
325360
});
326361
})
327362
.catch(function (error) {
328-
fail("Expected dev-endpoint HTTP module imports to succeed: " + (error && error.message ? error.message : String(error)));
363+
fail(new Error("Expected dev-endpoint HTTP module imports to succeed: " + formatError(error)));
329364
done();
330365
});
331366
});
@@ -341,16 +376,16 @@ describe("HTTP ESM Loader", function() {
341376
var u1 = origin + "/ns/m/query.mjs?b=2&a=1";
342377
var u2 = origin + "/ns/m/query.mjs?a=1&b=2";
343378

344-
import(u1)
379+
withTimeout(import(u1), 5000, "import " + u1)
345380
.then(function (m1) {
346-
return import(u2).then(function (m2) {
381+
return withTimeout(import(u2), 5000, "import " + u2).then(function (m2) {
347382
expect(m2.evaluatedAt).toBe(m1.evaluatedAt);
348383
expect(m2.query).toBe(m1.query);
349384
done();
350385
});
351386
})
352387
.catch(function (error) {
353-
fail("Expected dev-endpoint HTTP module imports to succeed: " + (error && error.message ? error.message : String(error)));
388+
fail(new Error("Expected dev-endpoint HTTP module imports to succeed: " + formatError(error)));
354389
done();
355390
});
356391
});
@@ -366,15 +401,15 @@ describe("HTTP ESM Loader", function() {
366401
var u1 = origin + "/esm/query.mjs#one";
367402
var u2 = origin + "/esm/query.mjs#two";
368403

369-
import(u1)
404+
withTimeout(import(u1), 5000, "import " + u1)
370405
.then(function (m1) {
371-
return import(u2).then(function (m2) {
406+
return withTimeout(import(u2), 5000, "import " + u2).then(function (m2) {
372407
expect(m2.evaluatedAt).toBe(m1.evaluatedAt);
373408
done();
374409
});
375410
})
376411
.catch(function (error) {
377-
fail("Expected fragment HTTP module imports to succeed: " + (error && error.message ? error.message : String(error)));
412+
fail(new Error("Expected fragment HTTP module imports to succeed: " + formatError(error)));
378413
done();
379414
});
380415
});

0 commit comments

Comments
 (0)