Skip to content

Commit e8a41f0

Browse files
committed
add tests
1 parent fbeb554 commit e8a41f0

File tree

4 files changed

+114
-47
lines changed

4 files changed

+114
-47
lines changed

packages/wrangler/e2e/provision.test.ts

Lines changed: 104 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import assert from "node:assert";
22
import dedent from "ts-dedent";
33
import { fetch } from "undici";
4-
import { afterAll, beforeAll, describe, expect, it } from "vitest";
4+
import { afterAll, beforeEach, describe, expect, it } from "vitest";
55
import { CLOUDFLARE_ACCOUNT_ID } from "./helpers/account-id";
66
import { WranglerE2ETestHelper } from "./helpers/e2e-wrangler-test";
77
import { fetchText } from "./helpers/fetch-text";
@@ -38,23 +38,27 @@ describe.skipIf(!CLOUDFLARE_ACCOUNT_ID)(
3838
expect(text).toMatchInlineSnapshot(`"Hello World!"`);
3939
});
4040

41-
beforeAll(async () => {
41+
beforeEach(async () => {
4242
await helper.seed({
4343
"wrangler.toml": dedent`
4444
name = "${workerName}"
4545
main = "src/index.ts"
4646
compatibility_date = "2023-01-01"
4747
workers_dev = true
4848
49-
[[kv_namespaces]]
50-
binding = "KV"
49+
[[kv_namespaces]]
50+
binding = "KV"
5151
52-
[[r2_buckets]]
53-
binding = "R2"
52+
[[r2_buckets]]
53+
binding = "R2"
5454
55-
[[d1_databases]]
56-
binding = "D1"
57-
`,
55+
[[r2_buckets]]
56+
binding = "R2_WITH_NAME"
57+
bucket_name = "does-not-exist"
58+
59+
[[d1_databases]]
60+
binding = "D1"
61+
`,
5862
"src/index.ts": dedent`
5963
export default {
6064
fetch(request) {
@@ -76,32 +80,33 @@ describe.skipIf(!CLOUDFLARE_ACCOUNT_ID)(
7680
await worker.exitCode;
7781
const output = await worker.output;
7882
expect(normalize(output)).toMatchInlineSnapshot(`
79-
"Total Upload: xx KiB / gzip: xx KiB
80-
The following bindings need to be provisioned:
81-
Binding Resource
82-
env.KV KV Namespace
83-
env.D1 D1 Database
84-
env.R2 R2 Bucket
85-
Provisioning KV (KV Namespace)...
86-
🌀 Creating new KV Namespace "tmp-e2e-worker-00000000-0000-0000-0000-000000000000-kv"...
87-
✨ KV provisioned 🎉
88-
Provisioning D1 (D1 Database)...
89-
🌀 Creating new D1 Database "tmp-e2e-worker-00000000-0000-0000-0000-000000000000-d1"...
90-
✨ D1 provisioned 🎉
91-
Provisioning R2 (R2 Bucket)...
92-
🌀 Creating new R2 Bucket "tmp-e2e-worker-00000000-0000-0000-0000-000000000000-r2"...
93-
✨ R2 provisioned 🎉
94-
🎉 All resources provisioned, continuing with deployment...
95-
Your Worker has access to the following bindings:
96-
Binding Resource
97-
env.KV (00000000000000000000000000000000) KV Namespace
98-
env.D1 (00000000-0000-0000-0000-000000000000) D1 Database
99-
env.R2 (tmp-e2e-worker-00000000-0000-0000-0000-000000000000-r2) R2 Bucket
100-
Uploaded tmp-e2e-worker-00000000-0000-0000-0000-000000000000 (TIMINGS)
101-
Deployed tmp-e2e-worker-00000000-0000-0000-0000-000000000000 triggers (TIMINGS)
102-
https://tmp-e2e-worker-00000000-0000-0000-0000-000000000000.SUBDOMAIN.workers.dev
103-
Current Version ID: 00000000-0000-0000-0000-000000000000"
104-
`);
83+
"Total Upload: xx KiB / gzip: xx KiB
84+
The following bindings need to be provisioned:
85+
Binding Resource
86+
env.KV KV Namespace
87+
env.D1 D1 Database
88+
env.R2 R2 Bucket
89+
Provisioning KV (KV Namespace)...
90+
🌀 Creating new KV Namespace "tmp-e2e-worker-00000000-0000-0000-0000-000000000000-kv"...
91+
✨ KV provisioned 🎉
92+
Provisioning D1 (D1 Database)...
93+
🌀 Creating new D1 Database "tmp-e2e-worker-00000000-0000-0000-0000-000000000000-d1"...
94+
✨ D1 provisioned 🎉
95+
Provisioning R2 (R2 Bucket)...
96+
🌀 Creating new R2 Bucket "tmp-e2e-worker-00000000-0000-0000-0000-000000000000-r2"...
97+
✨ R2 provisioned 🎉
98+
🎉 All resources provisioned, continuing with deployment...
99+
Your Worker has access to the following bindings:
100+
Binding Resource
101+
env.KV (00000000000000000000000000000000) KV Namespace
102+
env.D1 (00000000-0000-0000-0000-000000000000) D1 Database
103+
env.R2 (tmp-e2e-worker-00000000-0000-0000-0000-000000000000-r2) R2 Bucket
104+
env.R2_WITH_NAME (does-not-exist) R2 Bucket
105+
Uploaded tmp-e2e-worker-00000000-0000-0000-0000-000000000000 (TIMINGS)
106+
Deployed tmp-e2e-worker-00000000-0000-0000-0000-000000000000 triggers (TIMINGS)
107+
https://tmp-e2e-worker-00000000-0000-0000-0000-000000000000.SUBDOMAIN.workers.dev
108+
Current Version ID: 00000000-0000-0000-0000-000000000000"
109+
`);
105110
const urlMatch = output.match(
106111
/(?<url>https:\/\/tmp-e2e-.+?\..+?\.workers\.dev)/
107112
);
@@ -130,25 +135,78 @@ describe.skipIf(!CLOUDFLARE_ACCOUNT_ID)(
130135
await worker.exitCode;
131136
const output = await worker.output;
132137
expect(normalize(output)).toMatchInlineSnapshot(`
133-
"Total Upload: xx KiB / gzip: xx KiB
134-
Your Worker has access to the following bindings:
135-
Binding Resource
136-
env.KV (inherited) KV Namespace
137-
env.D1 (inherited) D1 Database
138-
env.R2 (inherited) R2 Bucket
139-
Uploaded tmp-e2e-worker-00000000-0000-0000-0000-000000000000 (TIMINGS)
140-
Deployed tmp-e2e-worker-00000000-0000-0000-0000-000000000000 triggers (TIMINGS)
141-
https://tmp-e2e-worker-00000000-0000-0000-0000-000000000000.SUBDOMAIN.workers.dev
142-
Current Version ID: 00000000-0000-0000-0000-000000000000"
143-
`);
138+
"Total Upload: xx KiB / gzip: xx KiB
139+
Your Worker has access to the following bindings:
140+
Binding Resource
141+
env.KV (inherited) KV Namespace
142+
env.D1 (inherited) D1 Database
143+
env.R2 (inherited) R2 Bucket
144+
env.R2_WITH_NAME (does-not-exist) R2 Bucket
145+
Uploaded tmp-e2e-worker-00000000-0000-0000-0000-000000000000 (TIMINGS)
146+
Deployed tmp-e2e-worker-00000000-0000-0000-0000-000000000000 triggers (TIMINGS)
147+
https://tmp-e2e-worker-00000000-0000-0000-0000-000000000000.SUBDOMAIN.workers.dev
148+
Current Version ID: 00000000-0000-0000-0000-000000000000"
149+
`);
144150

145151
const response = await retry(
146152
(resp) => !resp.ok,
147153
async () => await fetch(deployedUrl)
148154
);
149155
await expect(response.text()).resolves.toEqual("Hello World!");
150156
});
157+
it("can inspect current bindings", async () => {
158+
const versionsRaw = await helper.run(
159+
`wrangler versions list --json --x-provision`
160+
);
151161

162+
const versions = JSON.parse(versionsRaw.stdout) as unknown[];
163+
164+
const latest = versions.at(-1) as { id: string };
165+
166+
const versionView = await helper.run(
167+
`wrangler versions view ${latest.id} --x-provision`
168+
);
169+
170+
expect(normalizeOutput(versionView.output)).toMatchInlineSnapshot(`
171+
"Version ID: 00000000-0000-0000-0000-000000000000
172+
Created: TIMESTAMP
173+
Author:
174+
Source: Unknown (version_upload)
175+
Tag: -
176+
Message: -
177+
Handlers: fetch
178+
Compatibility Date: 2023-01-01
179+
[
180+
{
181+
"database_id": "00000000-0000-0000-0000-000000000000",
182+
"id": "00000000-0000-0000-0000-000000000000",
183+
"name": "D1",
184+
"type": "d1"
185+
},
186+
{
187+
"name": "KV",
188+
"namespace_id": "00000000000000000000000000000000",
189+
"type": "kv_namespace"
190+
},
191+
{
192+
"bucket_name": "tmp-e2e-worker-00000000-0000-0000-0000-000000000000-r2",
193+
"name": "R2",
194+
"type": "r2_bucket"
195+
},
196+
{
197+
"bucket_name": "does-not-exist",
198+
"name": "R2_WITH_NAME",
199+
"type": "r2_bucket"
200+
}
201+
]
202+
Your Worker has access to the following bindings:
203+
Binding Resource
204+
env.KV (00000000000000000000000000000000) KV Namespace
205+
env.D1 (00000000-0000-0000-0000-000000000000) D1 Database
206+
env.R2 (tmp-e2e-worker-00000000-0000-0000-0000-000000000000-r2) R2 Bucket
207+
env.R2_WITH_NAME (does-not-exist) R2 Bucket"
208+
`);
209+
});
152210
it("can inherit and provision resources on version upload", async () => {
153211
await helper.seed({
154212
"wrangler.toml": dedent`

packages/wrangler/src/__tests__/provision.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { readFile } from "node:fs/promises";
12
import { http, HttpResponse } from "msw";
23
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
34
import { mockConsoleMethods } from "./helpers/mock-console";
@@ -207,6 +208,9 @@ describe("--x-provision", () => {
207208
`);
208209
expect(std.err).toMatchInlineSnapshot(`""`);
209210
expect(std.warn).toMatchInlineSnapshot(`""`);
211+
212+
// IDs should be written back to the config file
213+
expect(await readFile("wrangler.toml", "utf-8")).toMatchInlineSnapshot();
210214
});
211215

212216
it("can provision KV, R2 and D1 bindings with existing resources, and lets you search when there are too many to list", async () => {

packages/wrangler/src/api/startDevWorker/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,11 @@ export async function convertBindingsToCfWorkerInitBindings(
379379
bindings.vars[name] = "value" in binding ? binding.value : binding.json;
380380
} else if (binding.type === "kv_namespace") {
381381
bindings.kv_namespaces ??= [];
382-
bindings.kv_namespaces.push({ ...binding, binding: name });
382+
bindings.kv_namespaces.push({
383+
...binding,
384+
binding: name,
385+
id: "namespace_id" in binding ? binding.namespace_id : binding.id,
386+
});
383387
} else if (binding.type === "send_email") {
384388
bindings.send_email ??= [];
385389
bindings.send_email.push({ ...binding, name: name });

packages/wrangler/src/versions/view.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ export const versionsViewCommand = createCommand({
114114
const bindings = version.resources.bindings.filter(
115115
(binding) => binding.type !== "secret_text"
116116
);
117+
logRaw(JSON.stringify(bindings, null, 2));
117118
if (bindings.length > 0) {
118119
printBindings(
119120
(await convertBindingsToCfWorkerInitBindings(bindings)).bindings

0 commit comments

Comments
 (0)