Skip to content

Commit 1d6c6e7

Browse files
ns476penalosa
andauthored
IMAGES-1525: Add Images binding support to vitest-pool-workers (#8362)
* Add Images vitest test * Wire up * Changeset * Fixup typescript issues --------- Co-authored-by: Samuel Macleod <[email protected]>
1 parent d891e8c commit 1d6c6e7

File tree

13 files changed

+174
-6
lines changed

13 files changed

+174
-6
lines changed

.changeset/evil-tools-accept.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/vitest-pool-workers": minor
3+
---
4+
5+
Add support for Images binding
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 🖼️ images
2+
3+
This Worker returns information about an image that is POSTed to it.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface Env {
2+
IMAGES: ImagesBinding;
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default {
2+
async fetch(request, env, _ctx) {
3+
return Response.json(await env.IMAGES.info(request.body!));
4+
},
5+
} satisfies ExportedHandler<Env>;
6+
// ^ Using `satisfies` provides type checking/completions for `ExportedHandler`
7+
// whilst still allowing us to call `worker.fetch()` and `worker.queue()` in
8+
// tests without asserting they're defined.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "../../tsconfig.workerd.json",
3+
"include": ["./**/*.ts"]
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module "cloudflare:test" {
2+
// Controls the type of `import("cloudflare:test").env`
3+
interface ProvidedEnv extends Env {}
4+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { SELF } from "cloudflare:test";
2+
import { expect, it } from "vitest";
3+
4+
const TINY_PNG = new Uint8Array([
5+
137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 18, 0,
6+
0, 0, 18, 8, 6, 0, 0, 0, 86, 206, 142, 87, 0, 0, 0, 4, 103, 65, 77, 65, 0, 0,
7+
177, 143, 11, 252, 97, 5, 0, 0, 0, 32, 99, 72, 82, 77, 0, 0, 122, 38, 0, 0,
8+
128, 132, 0, 0, 250, 0, 0, 0, 128, 232, 0, 0, 117, 48, 0, 0, 234, 96, 0, 0,
9+
58, 152, 0, 0, 23, 112, 156, 186, 81, 60, 0, 0, 0, 120, 101, 88, 73, 102, 77,
10+
77, 0, 42, 0, 0, 0, 8, 0, 4, 1, 18, 0, 3, 0, 0, 0, 1, 0, 1, 0, 0, 1, 26, 0, 5,
11+
0, 0, 0, 1, 0, 0, 0, 62, 1, 27, 0, 5, 0, 0, 0, 1, 0, 0, 0, 70, 135, 105, 0, 4,
12+
0, 0, 0, 1, 0, 0, 0, 78, 0, 0, 0, 0, 0, 0, 0, 72, 0, 0, 0, 1, 0, 0, 0, 72, 0,
13+
0, 0, 1, 0, 3, 160, 1, 0, 3, 0, 0, 0, 1, 0, 1, 0, 0, 160, 2, 0, 4, 0, 0, 0, 1,
14+
0, 0, 0, 18, 160, 3, 0, 4, 0, 0, 0, 1, 0, 0, 0, 18, 0, 0, 0, 0, 117, 55, 28,
15+
226, 0, 0, 0, 9, 112, 72, 89, 115, 0, 0, 11, 19, 0, 0, 11, 19, 1, 0, 154, 156,
16+
24, 0, 0, 2, 146, 105, 84, 88, 116, 88, 77, 76, 58, 99, 111, 109, 46, 97, 100,
17+
111, 98, 101, 46, 120, 109, 112, 0, 0, 0, 0, 0, 60, 120, 58, 120, 109, 112,
18+
109, 101, 116, 97, 32, 120, 109, 108, 110, 115, 58, 120, 61, 34, 97, 100, 111,
19+
98, 101, 58, 110, 115, 58, 109, 101, 116, 97, 47, 34, 32, 120, 58, 120, 109,
20+
112, 116, 107, 61, 34, 88, 77, 80, 32, 67, 111, 114, 101, 32, 54, 46, 48, 46,
21+
48, 34, 62, 10, 32, 32, 32, 60, 114, 100, 102, 58, 82, 68, 70, 32, 120, 109,
22+
108, 110, 115, 58, 114, 100, 102, 61, 34, 104, 116, 116, 112, 58, 47, 47, 119,
23+
119, 119, 46, 119, 51, 46, 111, 114, 103, 47, 49, 57, 57, 57, 47, 48, 50, 47,
24+
50, 50, 45, 114, 100, 102, 45, 115, 121, 110, 116, 97, 120, 45, 110, 115, 35,
25+
34, 62, 10, 32, 32, 32, 32, 32, 32, 60, 114, 100, 102, 58, 68, 101, 115, 99,
26+
114, 105, 112, 116, 105, 111, 110, 32, 114, 100, 102, 58, 97, 98, 111, 117,
27+
116, 61, 34, 34, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 120, 109,
28+
108, 110, 115, 58, 116, 105, 102, 102, 61, 34, 104, 116, 116, 112, 58, 47, 47,
29+
110, 115, 46, 97, 100, 111, 98, 101, 46, 99, 111, 109, 47, 116, 105, 102, 102,
30+
47, 49, 46, 48, 47, 34, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
31+
120, 109, 108, 110, 115, 58, 101, 120, 105, 102, 61, 34, 104, 116, 116, 112,
32+
58, 47, 47, 110, 115, 46, 97, 100, 111, 98, 101, 46, 99, 111, 109, 47, 101,
33+
120, 105, 102, 47, 49, 46, 48, 47, 34, 62, 10, 32, 32, 32, 32, 32, 32, 32, 32,
34+
32, 60, 116, 105, 102, 102, 58, 89, 82, 101, 115, 111, 108, 117, 116, 105,
35+
111, 110, 62, 55, 50, 60, 47, 116, 105, 102, 102, 58, 89, 82, 101, 115, 111,
36+
108, 117, 116, 105, 111, 110, 62, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 60,
37+
116, 105, 102, 102, 58, 88, 82, 101, 115, 111, 108, 117, 116, 105, 111, 110,
38+
62, 55, 50, 60, 47, 116, 105, 102, 102, 58, 88, 82, 101, 115, 111, 108, 117,
39+
116, 105, 111, 110, 62, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 60, 116, 105,
40+
102, 102, 58, 79, 114, 105, 101, 110, 116, 97, 116, 105, 111, 110, 62, 49, 60,
41+
47, 116, 105, 102, 102, 58, 79, 114, 105, 101, 110, 116, 97, 116, 105, 111,
42+
110, 62, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 60, 101, 120, 105, 102, 58,
43+
80, 105, 120, 101, 108, 88, 68, 105, 109, 101, 110, 115, 105, 111, 110, 62,
44+
54, 52, 60, 47, 101, 120, 105, 102, 58, 80, 105, 120, 101, 108, 88, 68, 105,
45+
109, 101, 110, 115, 105, 111, 110, 62, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32,
46+
60, 101, 120, 105, 102, 58, 67, 111, 108, 111, 114, 83, 112, 97, 99, 101, 62,
47+
49, 60, 47, 101, 120, 105, 102, 58, 67, 111, 108, 111, 114, 83, 112, 97, 99,
48+
101, 62, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 60, 101, 120, 105, 102, 58,
49+
80, 105, 120, 101, 108, 89, 68, 105, 109, 101, 110, 115, 105, 111, 110, 62,
50+
54, 52, 60, 47, 101, 120, 105, 102, 58, 80, 105, 120, 101, 108, 89, 68, 105,
51+
109, 101, 110, 115, 105, 111, 110, 62, 10, 32, 32, 32, 32, 32, 32, 60, 47,
52+
114, 100, 102, 58, 68, 101, 115, 99, 114, 105, 112, 116, 105, 111, 110, 62,
53+
10, 32, 32, 32, 60, 47, 114, 100, 102, 58, 82, 68, 70, 62, 10, 60, 47, 120,
54+
58, 120, 109, 112, 109, 101, 116, 97, 62, 10, 104, 194, 171, 3, 0, 0, 2, 38,
55+
73, 68, 65, 84, 56, 17, 221, 83, 75, 104, 19, 81, 20, 61, 239, 51, 153, 73,
56+
172, 197, 46, 84, 208, 69, 141, 165, 65, 4, 65, 232, 78, 193, 79, 93, 73, 169,
57+
136, 216, 224, 70, 16, 92, 21, 4, 17, 177, 59, 237, 168, 117, 229, 66, 5, 247,
58+
130, 203, 180, 43, 215, 74, 179, 113, 227, 194, 141, 32, 130, 24, 20, 162, 96,
59+
45, 90, 219, 152, 100, 38, 243, 222, 243, 188, 244, 131, 17, 68, 196, 149, 94,
60+
114, 185, 111, 114, 207, 59, 247, 158, 123, 103, 128, 127, 202, 220, 4, 148,
61+
171, 208, 29, 68, 247, 28, 67, 255, 177, 0, 79, 240, 171, 75, 158, 248, 231,
62+
92, 79, 133, 56, 134, 156, 222, 75, 208, 75, 8, 81, 70, 150, 222, 24, 26, 113,
63+
210, 92, 200, 12, 74, 82, 98, 73, 65, 206, 229, 174, 214, 30, 8, 1, 114, 17,
64+
3, 48, 172, 218, 6, 179, 35, 137, 136, 97, 215, 19, 233, 76, 241, 44, 145, 15,
65+
181, 18, 176, 214, 49, 201, 76, 32, 209, 106, 102, 115, 133, 107, 239, 38, 92,
66+
165, 162, 102, 81, 70, 185, 12, 227, 239, 116, 137, 214, 73, 86, 226, 161,
67+
109, 188, 182, 95, 106, 219, 128, 21, 79, 36, 16, 117, 44, 82, 226, 40, 211,
68+
55, 1, 211, 215, 159, 15, 147, 47, 175, 46, 69, 55, 113, 215, 19, 116, 71, 80,
69+
38, 218, 15, 81, 204, 194, 52, 103, 74, 59, 133, 75, 159, 70, 129, 28, 252,
70+
150, 202, 140, 215, 188, 108, 118, 40, 200, 183, 106, 66, 230, 76, 96, 63, 40,
71+
51, 48, 254, 58, 127, 108, 116, 50, 121, 113, 238, 109, 116, 6, 111, 124, 86,
72+
163, 66, 48, 251, 210, 162, 51, 22, 68, 122, 112, 101, 185, 157, 8, 91, 15,
73+
169, 222, 119, 176, 65, 226, 193, 78, 66, 181, 23, 224, 242, 71, 199, 134,
74+
177, 88, 125, 172, 228, 102, 99, 30, 141, 220, 81, 39, 170, 87, 132, 139, 119,
75+
69, 137, 20, 183, 89, 250, 180, 177, 217, 118, 200, 126, 136, 190, 97, 54,
76+
162, 5, 4, 21, 117, 157, 124, 66, 195, 101, 45, 232, 61, 199, 17, 30, 58, 233,
77+
204, 179, 123, 214, 53, 107, 50, 232, 111, 136, 236, 243, 194, 148, 78, 148,
78+
184, 31, 22, 228, 249, 70, 131, 29, 216, 101, 131, 168, 36, 100, 113, 148, 83,
79+
9, 233, 84, 167, 2, 138, 99, 244, 59, 202, 69, 8, 15, 142, 243, 153, 53, 118,
80+
28, 0, 26, 187, 59, 230, 227, 124, 78, 186, 250, 164, 200, 110, 21, 157, 234,
81+
174, 196, 47, 148, 149, 93, 27, 72, 222, 123, 37, 60, 175, 185, 63, 123, 145,
82+
75, 244, 139, 207, 25, 231, 129, 218, 101, 32, 220, 199, 77, 14, 192, 180,
83+
117, 71, 167, 153, 61, 165, 148, 242, 189, 19, 149, 209, 11, 252, 177, 90,
84+
143, 249, 119, 147, 203, 43, 108, 129, 90, 172, 65, 213, 167, 128, 77, 220,
85+
187, 225, 127, 73, 139, 111, 126, 250, 181, 7, 254, 55, 15, 28, 246, 239, 190,
86+
159, 35, 107, 252, 85, 70, 158, 15, 79, 51, 94, 167, 255, 96, 159, 182, 250,
87+
33, 252, 175, 246, 29, 136, 209, 199, 77, 51, 27, 182, 48, 0, 0, 0, 0, 73, 69,
88+
78, 68, 174, 66, 96, 130,
89+
]);
90+
91+
it("can return image info", async () => {
92+
const resp = (await (
93+
await SELF.fetch("https://example.com/", {
94+
method: "POST",
95+
body: new Blob([TINY_PNG]).stream(),
96+
})
97+
).json()) as ImageInfoResponse;
98+
99+
expect(resp.format).toEqual("image/png");
100+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "../../tsconfig.workerd-test.json",
3+
"include": ["./**/*.ts", "../src/env.d.ts"]
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "../tsconfig.node.json",
3+
"include": ["./*.ts"]
4+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { defineWorkersProject } from "@cloudflare/vitest-pool-workers/config";
2+
3+
export default defineWorkersProject({
4+
test: {
5+
poolOptions: {
6+
workers: {
7+
singleWorker: true,
8+
wrangler: {
9+
configPath: "./wrangler.toml",
10+
},
11+
},
12+
},
13+
},
14+
});

0 commit comments

Comments
 (0)