Skip to content

Commit 8ba8d99

Browse files
authored
refactor unenv e2e tests (#11354)
1 parent d162ddc commit 8ba8d99

File tree

1 file changed

+104
-61
lines changed
  • packages/wrangler/e2e/unenv-preset/worker

1 file changed

+104
-61
lines changed

packages/wrangler/e2e/unenv-preset/worker/index.ts

Lines changed: 104 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,12 @@ export const WorkerdTests: Record<string, () => void> = {
8181
assert.strictEqual(crypto.createCipher, undefined);
8282
assert.strictEqual(crypto.createDecipher, undefined);
8383
} else {
84-
assert.strictEqual(typeof crypto.Cipher, "function");
85-
assert.strictEqual(typeof crypto.Decipher, "function");
86-
assert.strictEqual(typeof crypto.createCipher, "function");
87-
assert.strictEqual(typeof crypto.createDecipher, "function");
84+
assertTypeOfProperties(crypto, {
85+
Cipher: "function",
86+
Decipher: "function",
87+
createCipher: "function",
88+
createDecipher: "function",
89+
});
8890
}
8991
},
9092

@@ -112,8 +114,10 @@ export const WorkerdTests: Record<string, () => void> = {
112114
}
113115
assert.ok(new buffer.File([], "file"));
114116
assert.ok(new buffer.Blob([]));
115-
assert.strictEqual(typeof buffer.INSPECT_MAX_BYTES, "number");
116-
assert.strictEqual(typeof buffer.resolveObjectURL, "function");
117+
assertTypeOfProperties(buffer, {
118+
INSPECT_MAX_BYTES: "number",
119+
resolveObjectURL: "function",
120+
});
117121
},
118122

119123
async testNodeCompatModules() {
@@ -262,20 +266,24 @@ export const WorkerdTests: Record<string, () => void> = {
262266
async testTls() {
263267
const tls = await import("node:tls");
264268
assert.strictEqual(typeof tls, "object");
265-
// @ts-expect-error Node types are wrong
266-
assert.strictEqual(typeof tls.convertALPNProtocols, "function");
267-
assert.strictEqual(typeof tls.createSecureContext, "function");
268-
assert.strictEqual(typeof tls.createServer, "function");
269-
assert.strictEqual(typeof tls.checkServerIdentity, "function");
270-
assert.strictEqual(typeof tls.getCiphers, "function");
269+
assertTypeOfProperties(tls, {
270+
convertALPNProtocols: "function",
271+
createSecureContext: "function",
272+
createServer: "function",
273+
checkServerIdentity: "function",
274+
getCiphers: "function",
275+
});
271276

272277
// Test constants
273-
assert.strictEqual(typeof tls.CLIENT_RENEG_LIMIT, "number");
274-
assert.strictEqual(typeof tls.CLIENT_RENEG_WINDOW, "number");
275-
assert.strictEqual(typeof tls.DEFAULT_ECDH_CURVE, "string");
276-
assert.strictEqual(typeof tls.DEFAULT_CIPHERS, "string");
277-
assert.strictEqual(typeof tls.DEFAULT_MIN_VERSION, "string");
278-
assert.strictEqual(typeof tls.DEFAULT_MAX_VERSION, "string");
278+
assertTypeOfProperties(tls, {
279+
CLIENT_RENEG_LIMIT: "number",
280+
CLIENT_RENEG_WINDOW: "number",
281+
DEFAULT_ECDH_CURVE: "string",
282+
DEFAULT_CIPHERS: "string",
283+
DEFAULT_MIN_VERSION: "string",
284+
DEFAULT_MAX_VERSION: "string",
285+
});
286+
279287
assert.ok(Array.isArray(tls.rootCertificates));
280288
},
281289

@@ -301,18 +309,22 @@ export const WorkerdTests: Record<string, () => void> = {
301309
}
302310

303311
assert.ok(http.METHODS.includes("GET"));
304-
assert.strictEqual(typeof http.get, "function");
305-
assert.strictEqual(typeof http.request, "function");
312+
assertTypeOfProperties(http, {
313+
get: "function",
314+
request: "function",
315+
});
306316
assert.deepEqual(http.STATUS_CODES[404], "Not Found");
307317
},
308318

309319
async testHttps() {
310320
const https = await import("node:https");
311321

312-
assert.strictEqual(typeof https.Agent, "function");
313-
assert.strictEqual(typeof https.get, "function");
314-
assert.strictEqual(typeof https.globalAgent, "object");
315-
assert.strictEqual(typeof https.request, "function");
322+
assertTypeOfProperties(https, {
323+
Agent: "function",
324+
get: "function",
325+
globalAgent: "object",
326+
request: "function",
327+
});
316328
},
317329

318330
async testHttpServer() {
@@ -366,9 +378,11 @@ export const WorkerdTests: Record<string, () => void> = {
366378
async testOs() {
367379
const os = await import("node:os");
368380

369-
assert.strictEqual(typeof os.arch(), "string");
370-
assert.strictEqual(typeof os.freemem(), "number");
371-
assert.strictEqual(typeof os.availableParallelism(), "number");
381+
assertTypeOfProperties(os, {
382+
arch: "function",
383+
freemem: "function",
384+
availableParallelism: "function",
385+
});
372386
},
373387

374388
async testAsyncHooks() {
@@ -385,8 +399,11 @@ export const WorkerdTests: Record<string, () => void> = {
385399

386400
assert.strictEqual(typeof asyncHooks.createHook, "function");
387401
const hook = asyncHooks.createHook({});
388-
assert.strictEqual(typeof hook.enable, "function");
389-
assert.strictEqual(typeof hook.disable, "function");
402+
403+
assertTypeOfProperties(hook, {
404+
enable: "function",
405+
disable: "function",
406+
});
390407

391408
assert.strictEqual(typeof asyncHooks.executionAsyncId(), "number");
392409
assert.strictEqual(typeof asyncHooks.executionAsyncResource(), "object");
@@ -479,14 +496,12 @@ export const WorkerdTests: Record<string, () => void> = {
479496
// @ts-expect-error TS2339 Invalid node/types.
480497
assert.ok(Array.isArray(module.globalPaths));
481498
assert.ok(Array.isArray(module.builtinModules));
482-
// @ts-expect-error TS2339 Invalid node/types.
483-
assert.strictEqual(typeof module.constants, "object");
484-
// @ts-expect-error TS2339 Invalid node/types.
485-
assert.strictEqual(typeof module._cache, "object");
486-
// @ts-expect-error TS2339 Invalid node/types.
487-
assert.strictEqual(typeof module._extensions, "object");
488-
// @ts-expect-error TS2339 Invalid node/types.
489-
assert.strictEqual(typeof module._pathCache, "object");
499+
assertTypeOfProperties(module, {
500+
constants: "object",
501+
_cache: "object",
502+
_extensions: "object",
503+
_pathCache: "object",
504+
});
490505
},
491506

492507
async testConstants() {
@@ -500,8 +515,11 @@ export const WorkerdTests: Record<string, () => void> = {
500515
async testHttp2() {
501516
const http2 = await import("node:http2");
502517

503-
assert.strictEqual(typeof http2.createSecureServer, "function");
504-
assert.strictEqual(typeof http2.connect, "function");
518+
assertTypeOfProperties(http2, {
519+
createSecureServer: "function",
520+
connect: "function",
521+
});
522+
505523
assert.strictEqual(http2.constants.HTTP2_HEADER_STATUS, ":status");
506524
},
507525

@@ -528,29 +546,34 @@ export const WorkerdTests: Record<string, () => void> = {
528546
}
529547

530548
// Event APIs are only available on global process
531-
assert.equal(typeof gProcess.addListener, "function");
532-
assert.equal(typeof gProcess.eventNames, "function");
533-
assert.equal(typeof gProcess.getMaxListeners, "function");
534-
assert.equal(typeof gProcess.listenerCount, "function");
535-
assert.equal(typeof gProcess.listeners, "function");
536-
assert.equal(typeof gProcess.off, "function");
537-
assert.equal(typeof gProcess.on, "function");
538-
assert.equal(typeof gProcess.once, "function");
539-
assert.equal(typeof gProcess.prependListener, "function");
540-
assert.equal(typeof gProcess.prependOnceListener, "function");
541-
assert.equal(typeof gProcess.rawListeners, "function");
542-
assert.equal(typeof gProcess.removeAllListeners, "function");
543-
assert.equal(typeof gProcess.removeListener, "function");
544-
assert.equal(typeof gProcess.setMaxListeners, "function");
549+
assertTypeOfProperties(gProcess, {
550+
addListener: "function",
551+
eventNames: "function",
552+
getMaxListeners: "function",
553+
listenerCount: "function",
554+
listeners: "function",
555+
off: "function",
556+
on: "function",
557+
once: "function",
558+
prependListener: "function",
559+
prependOnceListener: "function",
560+
rawListeners: "function",
561+
removeAllListeners: "function",
562+
removeListener: "function",
563+
setMaxListeners: "function",
564+
});
545565
},
546566

547567
async testPunycode() {
548568
const punycode = await import("node:punycode");
549569

550-
assert.strictEqual(typeof punycode.decode, "function");
551-
assert.strictEqual(typeof punycode.encode, "function");
552-
assert.strictEqual(typeof punycode.toASCII, "function");
553-
assert.strictEqual(typeof punycode.toUnicode, "function");
570+
assertTypeOfProperties(punycode, {
571+
decode: "function",
572+
encode: "function",
573+
toASCII: "function",
574+
toUnicode: "function",
575+
});
576+
554577
assert.strictEqual(
555578
punycode.toASCII("Bücher@日本語.com"),
556579
"Bücher@xn--wgv71a119e.com"
@@ -591,10 +614,13 @@ export const WorkerdTests: Record<string, () => void> = {
591614
const tracing = traceEvents.createTracing({
592615
categories: ["node.async_hooks"],
593616
});
594-
assertTypeOf(tracing, "enable", "function");
595-
assertTypeOf(tracing, "disable", "function");
596-
assertTypeOf(tracing, "enabled", "boolean");
597-
assertTypeOf(tracing, "categories", "string");
617+
618+
assertTypeOfProperties(tracing, {
619+
enable: "function",
620+
disable: "function",
621+
enabled: "boolean",
622+
categories: "string",
623+
});
598624
},
599625

600626
async testDomain() {
@@ -621,6 +647,9 @@ export const WorkerdTests: Record<string, () => void> = {
621647
},
622648
};
623649

650+
/**
651+
* Asserts that `target[property]` is of type `expectType`.
652+
*/
624653
function assertTypeOf(target: unknown, property: string, expectType: string) {
625654
const actualType = typeof (target as any)[property];
626655
assert.strictEqual(
@@ -629,3 +658,17 @@ function assertTypeOf(target: unknown, property: string, expectType: string) {
629658
`${property} should be of type ${expectType}, got ${actualType}`
630659
);
631660
}
661+
662+
/**
663+
* Asserts that multiple `properties` of `target` are of the expected types.
664+
* @param target the object to test
665+
* @param properties a record of property names to expected types
666+
*/
667+
function assertTypeOfProperties(
668+
target: unknown,
669+
properties: Record<string, string>
670+
) {
671+
for (const [property, expectType] of Object.entries(properties)) {
672+
assertTypeOf(target, property, expectType);
673+
}
674+
}

0 commit comments

Comments
 (0)