Skip to content

Commit 3845580

Browse files
committed
add tests
1 parent cbf1de2 commit 3845580

File tree

4 files changed

+149
-71
lines changed

4 files changed

+149
-71
lines changed

packages/wrangler/e2e/provision.test.ts

Lines changed: 139 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import assert from "node:assert";
2+
import { readFile } from "node:fs/promises";
23
import dedent from "ts-dedent";
34
import { fetch } from "undici";
4-
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
5+
import {
6+
afterAll,
7+
beforeAll,
8+
beforeEach,
9+
describe,
10+
expect,
11+
it,
12+
vi,
13+
} from "vitest";
514
import { CLOUDFLARE_ACCOUNT_ID } from "./helpers/account-id";
615
import { WranglerE2ETestHelper } from "./helpers/e2e-wrangler-test";
716
import { fetchText } from "./helpers/fetch-text";
@@ -38,22 +47,26 @@ describe.skipIf(!CLOUDFLARE_ACCOUNT_ID)(
3847
expect(text).toMatchInlineSnapshot(`"Hello World!"`);
3948
});
4049

41-
beforeAll(async () => {
50+
beforeEach(async () => {
4251
await helper.seed({
4352
"wrangler.toml": dedent`
44-
name = "${workerName}"
45-
main = "src/index.ts"
46-
compatibility_date = "2023-01-01"
53+
name = "${workerName}"
54+
main = "src/index.ts"
55+
compatibility_date = "2023-01-01"
4756
48-
[[kv_namespaces]]
49-
binding = "KV"
57+
[[kv_namespaces]]
58+
binding = "KV"
5059
51-
[[r2_buckets]]
52-
binding = "R2"
60+
[[r2_buckets]]
61+
binding = "R2"
5362
54-
[[d1_databases]]
55-
binding = "D1"
56-
`,
63+
[[r2_buckets]]
64+
binding = "R2_WITH_NAME"
65+
bucket_name = "does-not-exist"
66+
67+
[[d1_databases]]
68+
binding = "D1"
69+
`,
5770
"src/index.ts": dedent`
5871
export default {
5972
fetch(request) {
@@ -75,32 +88,33 @@ describe.skipIf(!CLOUDFLARE_ACCOUNT_ID)(
7588
await worker.exitCode;
7689
const output = await worker.output;
7790
expect(normalize(output)).toMatchInlineSnapshot(`
78-
"Total Upload: xx KiB / gzip: xx KiB
79-
The following bindings need to be provisioned:
80-
Binding Resource
81-
env.KV KV Namespace
82-
env.D1 D1 Database
83-
env.R2 R2 Bucket
84-
Provisioning KV (KV Namespace)...
85-
🌀 Creating new KV Namespace "tmp-e2e-worker-00000000-0000-0000-0000-000000000000-kv"...
86-
✨ KV provisioned 🎉
87-
Provisioning D1 (D1 Database)...
88-
🌀 Creating new D1 Database "tmp-e2e-worker-00000000-0000-0000-0000-000000000000-d1"...
89-
✨ D1 provisioned 🎉
90-
Provisioning R2 (R2 Bucket)...
91-
🌀 Creating new R2 Bucket "tmp-e2e-worker-00000000-0000-0000-0000-000000000000-r2"...
92-
✨ R2 provisioned 🎉
93-
🎉 All resources provisioned, continuing with deployment...
94-
Your Worker has access to the following bindings:
95-
Binding Resource
96-
env.KV (00000000000000000000000000000000) KV Namespace
97-
env.D1 (00000000-0000-0000-0000-000000000000) D1 Database
98-
env.R2 (tmp-e2e-worker-00000000-0000-0000-0000-000000000000-r2) R2 Bucket
99-
Uploaded tmp-e2e-worker-00000000-0000-0000-0000-000000000000 (TIMINGS)
100-
Deployed tmp-e2e-worker-00000000-0000-0000-0000-000000000000 triggers (TIMINGS)
101-
https://tmp-e2e-worker-00000000-0000-0000-0000-000000000000.SUBDOMAIN.workers.dev
102-
Current Version ID: 00000000-0000-0000-0000-000000000000"
103-
`);
91+
"Total Upload: xx KiB / gzip: xx KiB
92+
The following bindings need to be provisioned:
93+
Binding Resource
94+
env.KV KV Namespace
95+
env.D1 D1 Database
96+
env.R2 R2 Bucket
97+
Provisioning KV (KV Namespace)...
98+
🌀 Creating new KV Namespace "tmp-e2e-worker-00000000-0000-0000-0000-000000000000-kv"...
99+
✨ KV provisioned 🎉
100+
Provisioning D1 (D1 Database)...
101+
🌀 Creating new D1 Database "tmp-e2e-worker-00000000-0000-0000-0000-000000000000-d1"...
102+
✨ D1 provisioned 🎉
103+
Provisioning R2 (R2 Bucket)...
104+
🌀 Creating new R2 Bucket "tmp-e2e-worker-00000000-0000-0000-0000-000000000000-r2"...
105+
✨ R2 provisioned 🎉
106+
🎉 All resources provisioned, continuing with deployment...
107+
Your Worker has access to the following bindings:
108+
Binding Resource
109+
env.KV (00000000000000000000000000000000) KV Namespace
110+
env.D1 (00000000-0000-0000-0000-000000000000) D1 Database
111+
env.R2 (tmp-e2e-worker-00000000-0000-0000-0000-000000000000-r2) R2 Bucket
112+
env.R2_WITH_NAME (does-not-exist) R2 Bucket
113+
Uploaded tmp-e2e-worker-00000000-0000-0000-0000-000000000000 (TIMINGS)
114+
Deployed tmp-e2e-worker-00000000-0000-0000-0000-000000000000 triggers (TIMINGS)
115+
https://tmp-e2e-worker-00000000-0000-0000-0000-000000000000.SUBDOMAIN.workers.dev
116+
Current Version ID: 00000000-0000-0000-0000-000000000000"
117+
`);
104118
const urlMatch = output.match(
105119
/(?<url>https:\/\/tmp-e2e-.+?\..+?\.workers\.dev)/
106120
);
@@ -129,25 +143,78 @@ describe.skipIf(!CLOUDFLARE_ACCOUNT_ID)(
129143
await worker.exitCode;
130144
const output = await worker.output;
131145
expect(normalize(output)).toMatchInlineSnapshot(`
132-
"Total Upload: xx KiB / gzip: xx KiB
133-
Your Worker has access to the following bindings:
134-
Binding Resource
135-
env.KV (inherited) KV Namespace
136-
env.D1 (inherited) D1 Database
137-
env.R2 (inherited) R2 Bucket
138-
Uploaded tmp-e2e-worker-00000000-0000-0000-0000-000000000000 (TIMINGS)
139-
Deployed tmp-e2e-worker-00000000-0000-0000-0000-000000000000 triggers (TIMINGS)
140-
https://tmp-e2e-worker-00000000-0000-0000-0000-000000000000.SUBDOMAIN.workers.dev
141-
Current Version ID: 00000000-0000-0000-0000-000000000000"
142-
`);
146+
"Total Upload: xx KiB / gzip: xx KiB
147+
Your Worker has access to the following bindings:
148+
Binding Resource
149+
env.KV (inherited) KV Namespace
150+
env.D1 (inherited) D1 Database
151+
env.R2 (inherited) R2 Bucket
152+
env.R2_WITH_NAME (does-not-exist) R2 Bucket
153+
Uploaded tmp-e2e-worker-00000000-0000-0000-0000-000000000000 (TIMINGS)
154+
Deployed tmp-e2e-worker-00000000-0000-0000-0000-000000000000 triggers (TIMINGS)
155+
https://tmp-e2e-worker-00000000-0000-0000-0000-000000000000.SUBDOMAIN.workers.dev
156+
Current Version ID: 00000000-0000-0000-0000-000000000000"
157+
`);
143158

144159
const response = await retry(
145160
(resp) => !resp.ok,
146161
async () => await fetch(deployedUrl)
147162
);
148163
await expect(response.text()).resolves.toEqual("Hello World!");
149164
});
165+
it("can inspect current bindings", async () => {
166+
const versionsRaw = await helper.run(
167+
`wrangler versions list --json --x-provision`
168+
);
169+
170+
const versions = JSON.parse(versionsRaw.stdout) as unknown[];
150171

172+
const latest = versions.at(-1) as { id: string };
173+
174+
const versionView = await helper.run(
175+
`wrangler versions view ${latest.id} --x-provision`
176+
);
177+
178+
expect(normalizeOutput(versionView.output)).toMatchInlineSnapshot(`
179+
"Version ID: 00000000-0000-0000-0000-000000000000
180+
Created: TIMESTAMP
181+
Author:
182+
Source: Unknown (version_upload)
183+
Tag: -
184+
Message: -
185+
Handlers: fetch
186+
Compatibility Date: 2023-01-01
187+
[
188+
{
189+
"database_id": "00000000-0000-0000-0000-000000000000",
190+
"id": "00000000-0000-0000-0000-000000000000",
191+
"name": "D1",
192+
"type": "d1"
193+
},
194+
{
195+
"name": "KV",
196+
"namespace_id": "00000000000000000000000000000000",
197+
"type": "kv_namespace"
198+
},
199+
{
200+
"bucket_name": "tmp-e2e-worker-00000000-0000-0000-0000-000000000000-r2",
201+
"name": "R2",
202+
"type": "r2_bucket"
203+
},
204+
{
205+
"bucket_name": "does-not-exist",
206+
"name": "R2_WITH_NAME",
207+
"type": "r2_bucket"
208+
}
209+
]
210+
Your Worker has access to the following bindings:
211+
Binding Resource
212+
env.KV (00000000000000000000000000000000) KV Namespace
213+
env.D1 (00000000-0000-0000-0000-000000000000) D1 Database
214+
env.R2 (tmp-e2e-worker-00000000-0000-0000-0000-000000000000-r2) R2 Bucket
215+
env.R2_WITH_NAME (does-not-exist) R2 Bucket"
216+
`);
217+
});
151218
it("can inherit and provision resources on version upload", async () => {
152219
await helper.seed({
153220
"wrangler.toml": dedent`
@@ -167,27 +234,29 @@ describe.skipIf(!CLOUDFLARE_ACCOUNT_ID)(
167234
);
168235
await worker.exitCode;
169236
const output = await worker.output;
170-
expect(normalize(output)).toMatchInlineSnapshot(`
171-
"Total Upload: xx KiB / gzip: xx KiB
172-
The following bindings need to be provisioned:
173-
Binding Resource
174-
env.KV2 KV Namespace
175-
Provisioning KV2 (KV Namespace)...
176-
🌀 Creating new KV Namespace "tmp-e2e-worker-00000000-0000-0000-0000-000000000000-kv2"...
177-
✨ KV2 provisioned 🎉
178-
🎉 All resources provisioned, continuing with deployment...
179-
Worker Startup Time: (TIMINGS)
180-
Your Worker has access to the following bindings:
181-
Binding Resource
182-
env.KV2 (00000000000000000000000000000000) KV Namespace
183-
env.R2 (inherited) R2 Bucket
184-
Uploaded tmp-e2e-worker-00000000-0000-0000-0000-000000000000 (TIMINGS)
185-
Worker Version ID: 00000000-0000-0000-0000-000000000000
186-
Version Preview URL: https://tmp-e2e-worker-PREVIEW-URL.SUBDOMAIN.workers.dev
187-
To deploy this version to production traffic use the command wrangler versions deploy
188-
Changes to non-versioned settings (config properties 'logpush' or 'tail_consumers') take effect after your next deployment using the command wrangler versions deploy
189-
Changes to triggers (routes, custom domains, cron schedules, etc) must be applied with the command wrangler triggers deploy"
190-
`);
237+
expect(normalize(output)).toMatchInlineSnapshot(
238+
`
239+
"Total Upload: xx KiB / gzip: xx KiB
240+
The following bindings need to be provisioned:
241+
Binding Resource
242+
env.KV2 KV Namespace
243+
Provisioning KV2 (KV Namespace)...
244+
🌀 Creating new KV Namespace "tmp-e2e-worker-00000000-0000-0000-0000-000000000000-kv2"...
245+
✨ KV2 provisioned 🎉
246+
🎉 All resources provisioned, continuing with deployment...
247+
Worker Startup Time: (TIMINGS)
248+
Your Worker has access to the following bindings:
249+
Binding Resource
250+
env.KV2 (00000000000000000000000000000000) KV Namespace
251+
env.R2 (inherited) R2 Bucket
252+
Uploaded tmp-e2e-worker-00000000-0000-0000-0000-000000000000 (TIMINGS)
253+
Worker Version ID: 00000000-0000-0000-0000-000000000000
254+
Version Preview URL: https://tmp-e2e-worker-PREVIEW-URL.SUBDOMAIN.workers.dev
255+
To deploy this version to production traffic use the command wrangler versions deploy
256+
Changes to non-versioned settings (config properties 'logpush' or 'tail_consumers') take effect after your next deployment using the command wrangler versions deploy
257+
Changes to triggers (routes, custom domains, cron schedules, etc) must be applied with the command wrangler triggers deploy"
258+
`
259+
);
191260
const kvMatch = output.match(/env.KV2 \((?<kv>[0-9a-f]{32})/);
192261
assert(kvMatch?.groups);
193262
kvId2 = kvMatch.groups.kv;

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
@@ -347,7 +347,11 @@ export async function convertBindingsToCfWorkerInitBindings(
347347
bindings.vars[name] = "value" in binding ? binding.value : binding.json;
348348
} else if (binding.type === "kv_namespace") {
349349
bindings.kv_namespaces ??= [];
350-
bindings.kv_namespaces.push({ ...binding, binding: name });
350+
bindings.kv_namespaces.push({
351+
...binding,
352+
binding: name,
353+
id: "namespace_id" in binding ? binding.namespace_id : binding.id,
354+
});
351355
} else if (binding.type === "send_email") {
352356
bindings.send_email ??= [];
353357
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)