Skip to content

Commit 2b724b7

Browse files
Jake ChampionJakeChampion
authored andcommitted
Add test which confirms builtins can be extended from
1 parent c7f07bf commit 2b724b7

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/// <reference types="@fastly/js-compute" />
2+
3+
const builtins = [
4+
TransformStream,
5+
// CompressionStream,
6+
// Request,
7+
// Response,
8+
// Dictionary,
9+
// Headers,
10+
// CacheOverride,
11+
// TextEncoder,
12+
// TextDecoder,
13+
// URL,
14+
// URLSearchParams,
15+
]
16+
17+
addEventListener("fetch", event => {
18+
for (const builtin of builtins) {
19+
class customClass extends builtin {
20+
constructor() {
21+
switch (builtin.name) {
22+
case "CacheOverride": {
23+
super('none');
24+
break;
25+
}
26+
case "CompressionStream": {
27+
super("gzip")
28+
break;
29+
}
30+
case "Dictionary": {
31+
super("example")
32+
break
33+
}
34+
case "Request":
35+
case "URL": {
36+
super("http://example.com")
37+
break;
38+
}
39+
default: {
40+
super()
41+
}
42+
}
43+
}
44+
shrimp() { return 'shrimp'; }
45+
}
46+
const instance = new customClass();
47+
if (Reflect.getPrototypeOf(instance) !== customClass.prototype) {
48+
throw new Error(
49+
"Extending from `"+builtin.name+"`: Expected `Reflect.getPrototypeOf(instance) === customClass.prototype` to be `true`, instead found: `false`"
50+
);
51+
}
52+
if ((instance instanceof customClass) !== true) {
53+
throw new Error(
54+
"Extending from `"+builtin.name+"`: Expected `instance instanceof customClass` to be `true`, instead found: `false`"
55+
);
56+
}
57+
if (Reflect.has(instance, "shrimp") !== true) {
58+
throw new Error(
59+
"Extending from `"+builtin.name+"`: Expected `Reflect.has(instance, \"shrimp\")` to be `true`, instead found: `false`"
60+
);
61+
}
62+
if (typeof instance.shrimp !== "function") {
63+
throw new Error(
64+
"Extending from `"+builtin.name+"`: Expected `typeof instance.shrimp` to be `function`, instead found: `" + typeof instance.shrimp + "`"
65+
);
66+
}
67+
68+
console.log(`Tests for extending from ${builtin.name} all passed`)
69+
}
70+
event.respondWith(new Response);
71+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# This file describes a Fastly Compute@Edge package. To learn more visit:
2+
# https://developer.fastly.com/reference/fastly-toml/
3+
4+
authors = [""]
5+
description = ""
6+
language = "javascript"
7+
manifest_version = 2
8+
name = "extend-from-builtins"
9+
service_id = ""

integration-tests/js-compute/sdk-test-config.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,26 @@
328328
}
329329
}
330330
}
331+
},
332+
333+
"extend-from-builtins" : {
334+
"build": "(cd integration-tests/js-compute && npm install && npm run build:test --test=extend-from-builtins) && (cd ./integration-tests/js-compute/fixtures/extend-from-builtins && fastly compute pack --verbose --wasm-binary ./extend-from-builtins.wasm)",
335+
"fastly_toml_path": "./integration-tests/js-compute/fixtures/extend-from-builtins/fastly.toml",
336+
"wasm_path": "./integration-tests/js-compute/fixtures/extend-from-builtins/extend-from-builtins.wasm",
337+
"pkg_path": "./integration-tests/js-compute/fixtures/extend-from-builtins/pkg/extend-from-builtins.tar.gz",
338+
"tests": {
339+
"GET /": {
340+
"environments": ["viceroy"],
341+
"downstream_request": {
342+
"method": "GET",
343+
"pathname": "/"
344+
},
345+
"downstream_response": {
346+
"status": 200,
347+
"body": ""
348+
}
349+
}
350+
}
331351
}
332352

333353
}

0 commit comments

Comments
 (0)