11import { writeFileSync } from "node:fs" ;
22import { http , HttpResponse } from "msw" ;
3+ import dedent from "ts-dedent" ;
34import { endEventLoop } from "./helpers/end-event-loop" ;
45import { mockAccountId , mockApiToken } from "./helpers/mock-account-id" ;
56import { mockConsoleMethods } from "./helpers/mock-console" ;
@@ -9,6 +10,10 @@ import { mockProcess } from "./helpers/mock-process";
910import { msw } from "./helpers/msw" ;
1011import { runInTempDir } from "./helpers/run-in-tmp" ;
1112import { runWrangler } from "./helpers/run-wrangler" ;
13+ import {
14+ writeWranglerJson ,
15+ writeWranglerToml ,
16+ } from "./helpers/write-wrangler-toml" ;
1217import type {
1318 KeyValue ,
1419 KVNamespaceInfo ,
@@ -190,60 +195,109 @@ describe("wrangler", () => {
190195 ` ) ;
191196 } ) ;
192197
193- it ( "should create a namespace" , async ( ) => {
194- mockCreateRequest ( "worker-UnitTestNamespace" ) ;
195- await runWrangler ( "kv namespace create UnitTestNamespace" ) ;
196- expect ( std . out ) . toMatchInlineSnapshot ( `
197- "🌀 Creating namespace with title \\"worker-UnitTestNamespace\\"
198- ✨ Success!
199- Add the following to your configuration file in your kv_namespaces array:
200- [[kv_namespaces]]
201- binding = \\"UnitTestNamespace\\"
202- id = \\"some-namespace-id\\""
203- ` ) ;
204- } ) ;
205-
206- it ( "should create a preview namespace if configured to do so" , async ( ) => {
207- mockCreateRequest ( "worker-UnitTestNamespace_preview" ) ;
208- await runWrangler ( "kv namespace create UnitTestNamespace --preview" ) ;
209- expect ( std . out ) . toMatchInlineSnapshot ( `
210- "🌀 Creating namespace with title \\"worker-UnitTestNamespace_preview\\"
211- ✨ Success!
212- Add the following to your configuration file in your kv_namespaces array:
213- [[kv_namespaces]]
214- binding = \\"UnitTestNamespace\\"
215- preview_id = \\"some-namespace-id\\""
216- ` ) ;
217- } ) ;
198+ it . each ( [ "toml" , "jsonc" ] ) (
199+ "should create a namespace" ,
200+ async ( format ) => {
201+ format === "toml"
202+ ? writeWranglerToml ( { name : "worker" } )
203+ : writeWranglerJson ( { name : "worker" } ) ;
218204
219- it ( "should create a namespace using configured worker name" , async ( ) => {
220- writeFileSync ( "./wrangler.toml" , 'name = "other-worker"' , "utf-8" ) ;
221- mockCreateRequest ( "other-worker-UnitTestNamespace" ) ;
222- await runWrangler ( "kv namespace create UnitTestNamespace" ) ;
223- expect ( std . out ) . toMatchInlineSnapshot ( `
224- "🌀 Creating namespace with title \\"other-worker-UnitTestNamespace\\"
225- ✨ Success!
226- Add the following to your configuration file in your kv_namespaces array:
227- [[kv_namespaces]]
228- binding = \\"UnitTestNamespace\\"
229- id = \\"some-namespace-id\\""
230- ` ) ;
231- } ) ;
205+ mockCreateRequest ( "worker-UnitTestNamespace" ) ;
206+ await runWrangler ( "kv namespace create UnitTestNamespace" ) ;
207+ expect ( std . out ) . toContain ( dedent `
208+ 🌀 Creating namespace with title "worker-UnitTestNamespace"
209+ ✨ Success!
210+ Add the following to your configuration file in your kv_namespaces array:
211+ ` ) ;
212+ if ( format === "toml" ) {
213+ expect ( std . out ) . toContain ( "[[kv_namespaces]]" ) ;
214+ } else {
215+ expect ( std . out ) . toContain ( `"kv_namespaces": [` ) ;
216+ }
217+ }
218+ ) ;
219+
220+ it . each ( [ "toml" , "jsonc" ] ) (
221+ "should create a preview namespace if configured to do so" ,
222+ async ( format ) => {
223+ format === "toml"
224+ ? writeWranglerToml ( { name : "worker" } )
225+ : writeWranglerJson ( { name : "worker" } ) ;
226+
227+ mockCreateRequest ( "worker-UnitTestNamespace_preview" ) ;
228+ await runWrangler ( "kv namespace create UnitTestNamespace --preview" ) ;
229+ expect ( std . out ) . toContain ( dedent `
230+ 🌀 Creating namespace with title "worker-UnitTestNamespace_preview"
231+ ✨ Success!
232+ Add the following to your configuration file in your kv_namespaces array:
233+ ` ) ;
234+ if ( format === "toml" ) {
235+ expect ( std . out ) . toContain ( "[[kv_namespaces]]" ) ;
236+ } else {
237+ expect ( std . out ) . toContain ( `"kv_namespaces": [` ) ;
238+ }
239+ }
240+ ) ;
241+
242+ it . each ( [ "toml" , "jsonc" ] ) (
243+ "should create a namespace using configured worker name" ,
244+ async ( format ) => {
245+ format === "toml"
246+ ? writeWranglerToml ( { name : "other-worker" } )
247+ : writeWranglerJson ( { name : "other-worker" } ) ;
248+
249+ mockCreateRequest ( "other-worker-UnitTestNamespace" ) ;
250+ await runWrangler ( "kv namespace create UnitTestNamespace" ) ;
251+ expect ( std . out ) . toContain ( dedent `
252+ 🌀 Creating namespace with title "other-worker-UnitTestNamespace"
253+ ✨ Success!
254+ Add the following to your configuration file in your kv_namespaces array:
255+ ` ) ;
256+ if ( format === "toml" ) {
257+ expect ( std . out ) . toContain ( "[[kv_namespaces]]" ) ;
258+ } else {
259+ expect ( std . out ) . toContain ( `"kv_namespaces": [` ) ;
260+ }
261+ }
262+ ) ;
263+
264+ it . each ( [ "jsonc" , "toml" ] ) (
265+ "should create a namespace in an environment if configured to do so" ,
266+ async ( format ) => {
267+ format === "toml"
268+ ? writeWranglerToml ( {
269+ name : "worker" ,
270+ env : {
271+ customEnv : {
272+ name : "worker" ,
273+ } ,
274+ } ,
275+ } )
276+ : writeWranglerJson ( {
277+ name : "worker" ,
278+ env : {
279+ customEnv : {
280+ name : "worker" ,
281+ } ,
282+ } ,
283+ } ) ;
232284
233- it ( "should create a namespace in an environment if configured to do so" , async ( ) => {
234- mockCreateRequest ( "worker-customEnv-UnitTestNamespace" ) ;
235- await runWrangler (
236- "kv namespace create UnitTestNamespace --env customEnv"
237- ) ;
238- expect ( std . out ) . toMatchInlineSnapshot ( `
239- "🌀 Creating namespace with title \\"worker-customEnv-UnitTestNamespace\\"
240- ✨ Success!
241- Add the following to your configuration file in your kv_namespaces array under [env.customEnv]:
242- [[kv_namespaces]]
243- binding = \\"UnitTestNamespace\\"
244- id = \\"some-namespace-id\\""
245- ` ) ;
246- } ) ;
285+ mockCreateRequest ( "worker-customEnv-UnitTestNamespace" ) ;
286+ await runWrangler (
287+ "kv namespace create UnitTestNamespace --env customEnv"
288+ ) ;
289+ expect ( std . out ) . toContain ( dedent `
290+ 🌀 Creating namespace with title "worker-customEnv-UnitTestNamespace"
291+ ✨ Success!
292+ Add the following to your configuration file in your kv_namespaces array under [env.customEnv]:
293+ ` ) ;
294+ if ( format === "toml" ) {
295+ expect ( std . out ) . toContain ( "[[kv_namespaces]]" ) ;
296+ } else {
297+ expect ( std . out ) . toContain ( `"kv_namespaces": [` ) ;
298+ }
299+ }
300+ ) ;
247301 } ) ;
248302
249303 describe ( "list" , ( ) => {
0 commit comments