@@ -10,6 +10,7 @@ import { http, HttpResponse } from "msw";
1010import dedent from "ts-dedent" ;
1111import { File } from "undici" ;
1212import { vi } from "vitest" ;
13+ import * as checkCommand from "../check/commands" ;
1314import {
1415 printBundleSize ,
1516 printOffendingDependencies ,
@@ -64,6 +65,14 @@ import type { FormData } from "undici";
6465import type { Mock } from "vitest" ;
6566
6667vi . mock ( "command-exists" ) ;
68+ vi . mock ( "../check/commands" , async ( importOriginal ) => {
69+ return {
70+ ...( await importOriginal ( ) ) ,
71+ analyseBundle ( ) {
72+ return `{}` ;
73+ } ,
74+ } ;
75+ } ) ;
6776
6877describe ( "deploy" , ( ) => {
6978 mockAccountId ( ) ;
@@ -10301,6 +10310,214 @@ export default{
1030110310 } ) ;
1030210311 } ) ;
1030310312
10313+ describe ( "--outfile" , ( ) => {
10314+ it ( "should generate worker bundle at --outfile if specified" , async ( ) => {
10315+ writeWranglerConfig ( ) ;
10316+ writeWorkerSource ( ) ;
10317+ mockSubDomainRequest ( ) ;
10318+ mockUploadWorkerRequest ( ) ;
10319+ await runWrangler ( "deploy index.js --outfile some-dir/worker.bundle" ) ;
10320+ expect ( fs . existsSync ( "some-dir/worker.bundle" ) ) . toBe ( true ) ;
10321+ expect ( std ) . toMatchInlineSnapshot ( `
10322+ Object {
10323+ "debug": "",
10324+ "err": "",
10325+ "info": "",
10326+ "out": "Total Upload: xx KiB / gzip: xx KiB
10327+ Worker Startup Time: 100 ms
10328+ Uploaded test-name (TIMINGS)
10329+ Deployed test-name triggers (TIMINGS)
10330+ https://test-name.test-sub-domain.workers.dev
10331+ Current Version ID: Galaxy-Class",
10332+ "warn": "",
10333+ }
10334+ ` ) ;
10335+ } ) ;
10336+
10337+ it ( "should include any module imports related assets in the worker bundle" , async ( ) => {
10338+ writeWranglerConfig ( ) ;
10339+ fs . writeFileSync (
10340+ "./index.js" ,
10341+ `
10342+ import txt from './textfile.txt';
10343+ import hello from './hello.wasm';
10344+ export default{
10345+ async fetch(){
10346+ const module = await WebAssembly.instantiate(hello);
10347+ return new Response(txt + module.exports.hello);
10348+ }
10349+ }
10350+ `
10351+ ) ;
10352+ fs . writeFileSync ( "./textfile.txt" , "Hello, World!" ) ;
10353+ fs . writeFileSync ( "./hello.wasm" , "Hello wasm World!" ) ;
10354+ mockSubDomainRequest ( ) ;
10355+ mockUploadWorkerRequest ( {
10356+ expectedModules : {
10357+ "./0a0a9f2a6772942557ab5355d76af442f8f65e01-textfile.txt" :
10358+ "Hello, World!" ,
10359+ "./d025a03cd31e98e96fb5bd5bce87f9bca4e8ce2c-hello.wasm" :
10360+ "Hello wasm World!" ,
10361+ } ,
10362+ } ) ;
10363+ await runWrangler ( "deploy index.js --outfile some-dir/worker.bundle" ) ;
10364+
10365+ expect ( fs . existsSync ( "some-dir/worker.bundle" ) ) . toBe ( true ) ;
10366+ expect (
10367+ fs
10368+ . readFileSync ( "some-dir/worker.bundle" , "utf8" )
10369+ . replace (
10370+ / - - - - - - f o r m d a t a - u n d i c i - 0 .[ 0 - 9 ] * / g,
10371+ "------formdata-undici-0.test"
10372+ )
10373+ . replace ( / w r a n g l e r _ ( .+ ?) _ d e f a u l t / g, "wrangler_default" )
10374+ ) . toMatchInlineSnapshot ( `
10375+ "------formdata-undici-0.test
10376+ Content-Disposition: form-data; name=\\"metadata\\"
10377+
10378+ {\\"main_module\\":\\"index.js\\",\\"bindings\\":[],\\"compatibility_date\\":\\"2022-01-12\\",\\"compatibility_flags\\":[]}
10379+ ------formdata-undici-0.test
10380+ Content-Disposition: form-data; name=\\"index.js\\"; filename=\\"index.js\\"
10381+ Content-Type: application/javascript+module
10382+
10383+ // index.js
10384+ import txt from \\"./0a0a9f2a6772942557ab5355d76af442f8f65e01-textfile.txt\\";
10385+ import hello from \\"./d025a03cd31e98e96fb5bd5bce87f9bca4e8ce2c-hello.wasm\\";
10386+ var wrangler_default = {
10387+ async fetch() {
10388+ const module = await WebAssembly.instantiate(hello);
10389+ return new Response(txt + module.exports.hello);
10390+ }
10391+ };
10392+ export {
10393+ wrangler_default as default
10394+ };
10395+ //# sourceMappingURL=index.js.map
10396+
10397+ ------formdata-undici-0.test
10398+ Content-Disposition: form-data; name=\\"./0a0a9f2a6772942557ab5355d76af442f8f65e01-textfile.txt\\"; filename=\\"./0a0a9f2a6772942557ab5355d76af442f8f65e01-textfile.txt\\"
10399+ Content-Type: text/plain
10400+
10401+ Hello, World!
10402+ ------formdata-undici-0.test
10403+ Content-Disposition: form-data; name=\\"./d025a03cd31e98e96fb5bd5bce87f9bca4e8ce2c-hello.wasm\\"; filename=\\"./d025a03cd31e98e96fb5bd5bce87f9bca4e8ce2c-hello.wasm\\"
10404+ Content-Type: application/wasm
10405+
10406+ Hello wasm World!
10407+ ------formdata-undici-0.test--"
10408+ ` ) ;
10409+
10410+ expect ( std ) . toMatchInlineSnapshot ( `
10411+ Object {
10412+ "debug": "",
10413+ "err": "",
10414+ "info": "",
10415+ "out": "Total Upload: xx KiB / gzip: xx KiB
10416+ Worker Startup Time: 100 ms
10417+ Uploaded test-name (TIMINGS)
10418+ Deployed test-name triggers (TIMINGS)
10419+ https://test-name.test-sub-domain.workers.dev
10420+ Current Version ID: Galaxy-Class",
10421+ "warn": "",
10422+ }
10423+ ` ) ;
10424+ } ) ;
10425+
10426+ it ( "should include bindings in the worker bundle" , async ( ) => {
10427+ writeWranglerConfig ( {
10428+ kv_namespaces : [ { binding : "KV" , id : "kv-namespace-id" } ] ,
10429+ } ) ;
10430+ fs . writeFileSync (
10431+ "./index.js" ,
10432+ `
10433+ import txt from './textfile.txt';
10434+ import hello from './hello.wasm';
10435+ export default{
10436+ async fetch(){
10437+ const module = await WebAssembly.instantiate(hello);
10438+ return new Response(txt + module.exports.hello);
10439+ }
10440+ }
10441+ `
10442+ ) ;
10443+ fs . writeFileSync ( "./textfile.txt" , "Hello, World!" ) ;
10444+ fs . writeFileSync ( "./hello.wasm" , "Hello wasm World!" ) ;
10445+ mockSubDomainRequest ( ) ;
10446+ mockUploadWorkerRequest ( {
10447+ expectedModules : {
10448+ "./0a0a9f2a6772942557ab5355d76af442f8f65e01-textfile.txt" :
10449+ "Hello, World!" ,
10450+ "./d025a03cd31e98e96fb5bd5bce87f9bca4e8ce2c-hello.wasm" :
10451+ "Hello wasm World!" ,
10452+ } ,
10453+ } ) ;
10454+ await runWrangler ( "deploy index.js --outfile some-dir/worker.bundle" ) ;
10455+
10456+ expect ( fs . existsSync ( "some-dir/worker.bundle" ) ) . toBe ( true ) ;
10457+ expect (
10458+ fs
10459+ . readFileSync ( "some-dir/worker.bundle" , "utf8" )
10460+ . replace (
10461+ / - - - - - - f o r m d a t a - u n d i c i - 0 .[ 0 - 9 ] * / g,
10462+ "------formdata-undici-0.test"
10463+ )
10464+ . replace ( / w r a n g l e r _ ( .+ ?) _ d e f a u l t / g, "wrangler_default" )
10465+ ) . toMatchInlineSnapshot ( `
10466+ "------formdata-undici-0.test
10467+ Content-Disposition: form-data; name=\\"metadata\\"
10468+
10469+ {\\"main_module\\":\\"index.js\\",\\"bindings\\":[{\\"name\\":\\"KV\\",\\"type\\":\\"kv_namespace\\",\\"namespace_id\\":\\"kv-namespace-id\\"}],\\"compatibility_date\\":\\"2022-01-12\\",\\"compatibility_flags\\":[]}
10470+ ------formdata-undici-0.test
10471+ Content-Disposition: form-data; name=\\"index.js\\"; filename=\\"index.js\\"
10472+ Content-Type: application/javascript+module
10473+
10474+ // index.js
10475+ import txt from \\"./0a0a9f2a6772942557ab5355d76af442f8f65e01-textfile.txt\\";
10476+ import hello from \\"./d025a03cd31e98e96fb5bd5bce87f9bca4e8ce2c-hello.wasm\\";
10477+ var wrangler_default = {
10478+ async fetch() {
10479+ const module = await WebAssembly.instantiate(hello);
10480+ return new Response(txt + module.exports.hello);
10481+ }
10482+ };
10483+ export {
10484+ wrangler_default as default
10485+ };
10486+ //# sourceMappingURL=index.js.map
10487+
10488+ ------formdata-undici-0.test
10489+ Content-Disposition: form-data; name=\\"./0a0a9f2a6772942557ab5355d76af442f8f65e01-textfile.txt\\"; filename=\\"./0a0a9f2a6772942557ab5355d76af442f8f65e01-textfile.txt\\"
10490+ Content-Type: text/plain
10491+
10492+ Hello, World!
10493+ ------formdata-undici-0.test
10494+ Content-Disposition: form-data; name=\\"./d025a03cd31e98e96fb5bd5bce87f9bca4e8ce2c-hello.wasm\\"; filename=\\"./d025a03cd31e98e96fb5bd5bce87f9bca4e8ce2c-hello.wasm\\"
10495+ Content-Type: application/wasm
10496+
10497+ Hello wasm World!
10498+ ------formdata-undici-0.test--"
10499+ ` ) ;
10500+
10501+ expect ( std ) . toMatchInlineSnapshot ( `
10502+ Object {
10503+ "debug": "",
10504+ "err": "",
10505+ "info": "",
10506+ "out": "Total Upload: xx KiB / gzip: xx KiB
10507+ Worker Startup Time: 100 ms
10508+ Your worker has access to the following bindings:
10509+ - KV Namespaces:
10510+ - KV: kv-namespace-id
10511+ Uploaded test-name (TIMINGS)
10512+ Deployed test-name triggers (TIMINGS)
10513+ https://test-name.test-sub-domain.workers.dev
10514+ Current Version ID: Galaxy-Class",
10515+ "warn": "",
10516+ }
10517+ ` ) ;
10518+ } ) ;
10519+ } ) ;
10520+
1030410521 describe ( "--dry-run" , ( ) => {
1030510522 it ( "should not deploy the worker if --dry-run is specified" , async ( ) => {
1030610523 writeWranglerConfig ( {
@@ -10864,35 +11081,9 @@ export default{
1086411081 main : "index.js" ,
1086511082 } ) ;
1086611083
10867- await expect ( runWrangler ( "deploy" ) ) . rejects . toMatchInlineSnapshot (
10868- `[APIError: A request to the Cloudflare API (/accounts/some-account-id/workers/scripts/test-name/versions) failed.] `
11084+ await expect ( runWrangler ( "deploy" ) ) . rejects . toThrowError (
11085+ `Your Worker failed validation because it exceeded startup limits. `
1086911086 ) ;
10870- expect ( std ) . toMatchInlineSnapshot ( `
10871- Object {
10872- "debug": "",
10873- "err": "",
10874- "info": "",
10875- "out": "Total Upload: xx KiB / gzip: xx KiB
10876-
10877- [31mX [41;31m[[41;97mERROR[41;31m][0m [1mA request to the Cloudflare API (/accounts/some-account-id/workers/scripts/test-name/versions) failed.[0m
10878-
10879- Error: Script startup exceeded CPU time limit. [code: 10021]
10880-
10881- If you think this is a bug, please open an issue at:
10882- [4mhttps://github.com/cloudflare/workers-sdk/issues/new/choose[0m
10883-
10884- ",
10885- "warn": "[33m▲ [43;33m[[43;30mWARNING[43;33m][0m [1mYour Worker failed validation because it exceeded startup limits.[0m
10886-
10887- To ensure fast responses, we place constraints on Worker startup -- like how much CPU it can use,
10888- or how long it can take.
10889- Your Worker failed validation, which means it hit one of these startup limits.
10890- Try reducing the amount of work done during startup (outside the event handler), either by
10891- removing code or relocating it inside the event handler.
10892-
10893- ",
10894- }
10895- ` ) ;
1089611087 } ) ;
1089711088
1089811089 describe ( "unit tests" , ( ) => {
0 commit comments