From 1bd4885b5dcba981c0ccf13aa1228262b9101783 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Somhairle=20MacLe=C3=B2id?= Date: Tue, 5 Nov 2024 11:09:21 +0000 Subject: [PATCH] Fix `--test-scheduled` with custom builds & `--x-dev-env` (#7164) --- .changeset/sweet-toes-check.md | 5 ++ .../e2e/__snapshots__/dev.test.ts.snap | 24 +++++++ packages/wrangler/e2e/dev.test.ts | 70 +++++++++++++++++++ .../__tests__/navigator-user-agent.test.ts | 2 + .../api/startDevWorker/BundlerController.ts | 14 ++++ .../startDevWorker/LocalRuntimeController.ts | 1 + packages/wrangler/src/deploy/deploy.ts | 10 +++ .../wrangler/src/deployment-bundle/bundle.ts | 37 +++++----- packages/wrangler/src/dev/local.tsx | 1 + packages/wrangler/src/dev/miniflare.ts | 3 +- packages/wrangler/src/dev/start-server.ts | 10 +++ packages/wrangler/src/dev/use-esbuild.ts | 9 +++ .../src/pages/functions/buildPlugin.ts | 8 +++ .../src/pages/functions/buildWorker.ts | 18 ++++- packages/wrangler/src/versions/upload.ts | 12 ++++ 15 files changed, 201 insertions(+), 23 deletions(-) create mode 100644 .changeset/sweet-toes-check.md diff --git a/.changeset/sweet-toes-check.md b/.changeset/sweet-toes-check.md new file mode 100644 index 000000000000..193a401389b9 --- /dev/null +++ b/.changeset/sweet-toes-check.md @@ -0,0 +1,5 @@ +--- +"wrangler": patch +--- + +Fix `--test-scheduled` with custom builds & `--x-dev-env` diff --git a/packages/wrangler/e2e/__snapshots__/dev.test.ts.snap b/packages/wrangler/e2e/__snapshots__/dev.test.ts.snap index af2fc4fe0968..49478b0d1529 100644 --- a/packages/wrangler/e2e/__snapshots__/dev.test.ts.snap +++ b/packages/wrangler/e2e/__snapshots__/dev.test.ts.snap @@ -1,23 +1,47 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html +exports[`basic js dev: 'wrangler dev --no-x-dev-env' > --test-scheduled works with wrangler dev --no-x-dev-env > custom build 1`] = `"Ran scheduled event"`; + +exports[`basic js dev: 'wrangler dev --no-x-dev-env' > --test-scheduled works with wrangler dev --no-x-dev-env > no custom build 1`] = `"Ran scheduled event"`; + +exports[`basic js dev: 'wrangler dev --no-x-dev-env' > --test-scheduled works with wrangler dev --no-x-dev-env 1`] = `"Ran scheduled event"`; + exports[`basic js dev: 'wrangler dev --no-x-dev-env' > can modify worker during wrangler dev --no-x-dev-env 1`] = `"Hello World!"`; exports[`basic js dev: 'wrangler dev --no-x-dev-env' > can modify worker during wrangler dev --no-x-dev-env 2`] = `"Updated Worker! value"`; exports[`basic js dev: 'wrangler dev --no-x-dev-env' > hotkeys can be disabled with wrangler dev --no-x-dev-env 1`] = `"Hello World!"`; +exports[`basic js dev: 'wrangler dev --remote --no-x-dev-env' > --test-scheduled works with wrangler dev --remote --no-x-dev-env > custom build 1`] = `"Ran scheduled event"`; + +exports[`basic js dev: 'wrangler dev --remote --no-x-dev-env' > --test-scheduled works with wrangler dev --remote --no-x-dev-env > no custom build 1`] = `"Ran scheduled event"`; + +exports[`basic js dev: 'wrangler dev --remote --no-x-dev-env' > --test-scheduled works with wrangler dev --remote --no-x-dev-env 1`] = `"Ran scheduled event"`; + exports[`basic js dev: 'wrangler dev --remote --no-x-dev-env' > can modify worker during wrangler dev --remote --no-x-dev-env 1`] = `"Hello World!"`; exports[`basic js dev: 'wrangler dev --remote --no-x-dev-env' > can modify worker during wrangler dev --remote --no-x-dev-env 2`] = `"Updated Worker! value"`; exports[`basic js dev: 'wrangler dev --remote --no-x-dev-env' > hotkeys can be disabled with wrangler dev --remote --no-x-dev-env 1`] = `"Hello World!"`; +exports[`basic js dev: 'wrangler dev --remote --x-dev-env' > --test-scheduled works with wrangler dev --remote --x-dev-env > custom build 1`] = `"Ran scheduled event"`; + +exports[`basic js dev: 'wrangler dev --remote --x-dev-env' > --test-scheduled works with wrangler dev --remote --x-dev-env > no custom build 1`] = `"Ran scheduled event"`; + +exports[`basic js dev: 'wrangler dev --remote --x-dev-env' > --test-scheduled works with wrangler dev --remote --x-dev-env 1`] = `"Ran scheduled event"`; + exports[`basic js dev: 'wrangler dev --remote --x-dev-env' > can modify worker during wrangler dev --remote --x-dev-env 1`] = `"Hello World!"`; exports[`basic js dev: 'wrangler dev --remote --x-dev-env' > can modify worker during wrangler dev --remote --x-dev-env 2`] = `"Updated Worker! value"`; exports[`basic js dev: 'wrangler dev --remote --x-dev-env' > hotkeys can be disabled with wrangler dev --remote --x-dev-env 1`] = `"Hello World!"`; +exports[`basic js dev: 'wrangler dev --x-dev-env' > --test-scheduled works with wrangler dev --x-dev-env > custom build 1`] = `"Ran scheduled event"`; + +exports[`basic js dev: 'wrangler dev --x-dev-env' > --test-scheduled works with wrangler dev --x-dev-env > no custom build 1`] = `"Ran scheduled event"`; + +exports[`basic js dev: 'wrangler dev --x-dev-env' > --test-scheduled works with wrangler dev --x-dev-env 1`] = `"Ran scheduled event"`; + exports[`basic js dev: 'wrangler dev --x-dev-env' > can modify worker during wrangler dev --x-dev-env 1`] = `"Hello World!"`; exports[`basic js dev: 'wrangler dev --x-dev-env' > can modify worker during wrangler dev --x-dev-env 2`] = `"Updated Worker! value"`; diff --git a/packages/wrangler/e2e/dev.test.ts b/packages/wrangler/e2e/dev.test.ts index 0c4fa66b5633..3af2a1862c39 100644 --- a/packages/wrangler/e2e/dev.test.ts +++ b/packages/wrangler/e2e/dev.test.ts @@ -139,6 +139,76 @@ describe.each([ await expect(worker.currentOutput).not.toContain("[b] open a browser"); }); + + describe(`--test-scheduled works with ${cmd}`, async () => { + it("custom build", async () => { + const helper = new WranglerE2ETestHelper(); + await helper.seed({ + "wrangler.toml": dedent` + name = "${workerName}" + main = "src/index.ts" + compatibility_date = "2023-01-01" + [build] + command = "true" + `, + "src/index.ts": dedent` + export default { + scheduled(event) { + console.log("Event triggered") + } + }`, + "package.json": dedent` + { + "name": "worker", + "version": "0.0.0", + "private": true + } + `, + }); + const worker = helper.runLongLived(`${cmd} --test-scheduled`); + + const { url } = await worker.waitForReady(); + + await expect( + fetch(`${url}/__scheduled`).then((r) => r.text()) + ).resolves.toMatchSnapshot(); + + await worker.readUntil(/Event triggered/); + }); + + it("no custom build", async () => { + const helper = new WranglerE2ETestHelper(); + await helper.seed({ + "wrangler.toml": dedent` + name = "${workerName}" + main = "src/index.ts" + compatibility_date = "2023-01-01" + `, + "src/index.ts": dedent` + export default { + scheduled(event) { + console.log("Event triggered") + } + }`, + "package.json": dedent` + { + "name": "worker", + "version": "0.0.0", + "private": true + } + `, + }); + const worker = helper.runLongLived(`${cmd} --test-scheduled`); + + const { url } = await worker.waitForReady(); + + await expect( + fetch(`${url}/__scheduled`).then((r) => r.text()) + ).resolves.toMatchSnapshot(); + + await worker.readUntil(/Event triggered/); + }); + }); }); // Skipping remote python tests because they consistently flake with timeouts diff --git a/packages/wrangler/src/__tests__/navigator-user-agent.test.ts b/packages/wrangler/src/__tests__/navigator-user-agent.test.ts index 5c3bf0d30a65..1f3f3288e659 100644 --- a/packages/wrangler/src/__tests__/navigator-user-agent.test.ts +++ b/packages/wrangler/src/__tests__/navigator-user-agent.test.ts @@ -110,6 +110,7 @@ describe("defineNavigatorUserAgent is respected", () => { exports: [], }, path.resolve("dist"), + // @ts-expect-error Ignore the requirement for passing undefined values { bundle: true, additionalModules: [], @@ -172,6 +173,7 @@ describe("defineNavigatorUserAgent is respected", () => { exports: [], }, path.resolve("dist"), + // @ts-expect-error Ignore the requirement for passing undefined values { bundle: true, additionalModules: [], diff --git a/packages/wrangler/src/api/startDevWorker/BundlerController.ts b/packages/wrangler/src/api/startDevWorker/BundlerController.ts index 3d96f03c6e0a..3ff273579101 100644 --- a/packages/wrangler/src/api/startDevWorker/BundlerController.ts +++ b/packages/wrangler/src/api/startDevWorker/BundlerController.ts @@ -133,6 +133,20 @@ export class BundlerController extends Controller { config.compatibilityDate, config.compatibilityFlags ), + testScheduled: config.dev.testScheduled, + plugins: undefined, + + // Pages specific options used by wrangler pages commands + entryName: undefined, + inject: undefined, + isOutfile: undefined, + external: undefined, + + // We don't use esbuild watching for custom builds + watch: undefined, + + // sourcemap defaults to true in dev + sourcemap: undefined, }); if (buildAborter.signal.aborted) { return; diff --git a/packages/wrangler/src/api/startDevWorker/LocalRuntimeController.ts b/packages/wrangler/src/api/startDevWorker/LocalRuntimeController.ts index 6d74a4ae2fe1..339658f31c53 100644 --- a/packages/wrangler/src/api/startDevWorker/LocalRuntimeController.ts +++ b/packages/wrangler/src/api/startDevWorker/LocalRuntimeController.ts @@ -119,6 +119,7 @@ async function convertToConfigBundle( services: bindings.services, serviceBindings: fetchers, bindVectorizeToProd: event.config.dev?.bindVectorizeToProd ?? false, + testScheduled: !!event.config.dev.testScheduled, }; } diff --git a/packages/wrangler/src/deploy/deploy.ts b/packages/wrangler/src/deploy/deploy.ts index 5d9e44e214a8..5e43211558ec 100644 --- a/packages/wrangler/src/deploy/deploy.ts +++ b/packages/wrangler/src/deploy/deploy.ts @@ -605,6 +605,16 @@ See https://developers.cloudflare.com/workers/platform/compatibility-dates for m props.compatibilityFlags ?? config.compatibility_flags ), plugins: [logBuildOutput(nodejsCompatMode)], + + // Pages specific options used by wrangler pages commands + entryName: undefined, + inject: undefined, + isOutfile: undefined, + external: undefined, + + // These options are dev-only + testScheduled: undefined, + watch: undefined, } ); diff --git a/packages/wrangler/src/deployment-bundle/bundle.ts b/packages/wrangler/src/deployment-bundle/bundle.ts index 3727715c8f5f..8fafb78f0d1c 100644 --- a/packages/wrangler/src/deployment-bundle/bundle.ts +++ b/packages/wrangler/src/deployment-bundle/bundle.ts @@ -115,32 +115,31 @@ export type BundleOptions = { // A module collector enables you to observe what modules are in the Worker. moduleCollector: ModuleCollector; serveLegacyAssetsFromWorker: boolean; - legacyAssets?: Config["legacy_assets"]; - bypassAssetCache?: boolean; + legacyAssets: Config["legacy_assets"] | undefined; + bypassAssetCache: boolean | undefined; doBindings: DurableObjectBindings; workflowBindings: WorkflowBinding[]; - jsxFactory?: string; - jsxFragment?: string; - entryName?: string; - watch?: boolean; - tsconfig?: string; - minify?: boolean; - nodejsCompatMode?: NodeJSCompatMode; + jsxFactory: string | undefined; + jsxFragment: string | undefined; + entryName: string | undefined; + watch: boolean | undefined; + tsconfig: string | undefined; + minify: boolean | undefined; + nodejsCompatMode: NodeJSCompatMode | undefined; define: Config["define"]; alias: Config["alias"]; checkFetch: boolean; mockAnalyticsEngineDatasets: Config["analytics_engine_datasets"]; targetConsumer: "dev" | "deploy"; - testScheduled?: boolean; - inject?: string[]; - loader?: Record; - sourcemap?: esbuild.CommonOptions["sourcemap"]; - plugins?: esbuild.Plugin[]; - isOutfile?: boolean; + testScheduled: boolean | undefined; + inject: string[] | undefined; + sourcemap: esbuild.CommonOptions["sourcemap"] | undefined; + plugins: esbuild.Plugin[] | undefined; + isOutfile: boolean | undefined; local: boolean; projectRoot: string | undefined; defineNavigatorUserAgent: boolean; - external?: string[]; + external: string[] | undefined; }; /** @@ -172,7 +171,6 @@ export async function bundleWorker( targetConsumer, testScheduled, inject: injectOption, - loader, sourcemap, plugins, isOutfile, @@ -424,10 +422,7 @@ export async function bundleWorker( ...define, }, }), - loader: { - ...COMMON_ESBUILD_OPTIONS.loader, - ...(loader || {}), - }, + loader: COMMON_ESBUILD_OPTIONS.loader, plugins: [ aliasPlugin, moduleCollector.plugin, diff --git a/packages/wrangler/src/dev/local.tsx b/packages/wrangler/src/dev/local.tsx index fe920729ca78..05a2ea39af23 100644 --- a/packages/wrangler/src/dev/local.tsx +++ b/packages/wrangler/src/dev/local.tsx @@ -112,6 +112,7 @@ export async function localPropsToConfigBundle( services: props.services, serviceBindings, bindVectorizeToProd: props.bindVectorizeToProd, + testScheduled: !!props.testScheduled, }; } diff --git a/packages/wrangler/src/dev/miniflare.ts b/packages/wrangler/src/dev/miniflare.ts index 40f614da1989..7d31551539da 100644 --- a/packages/wrangler/src/dev/miniflare.ts +++ b/packages/wrangler/src/dev/miniflare.ts @@ -198,6 +198,7 @@ export interface ConfigBundle { services: Config["services"] | undefined; serviceBindings: Record; bindVectorizeToProd: boolean; + testScheduled: boolean; } export class WranglerLog extends Log { @@ -908,7 +909,7 @@ export async function buildMiniflareOptions( internalObjects: CfDurableObject[]; entrypointNames: string[]; }> { - if (config.crons.length > 0) { + if (config.crons.length > 0 && !config.testScheduled) { if (!didWarnMiniflareCronSupport) { didWarnMiniflareCronSupport = true; log.warn( diff --git a/packages/wrangler/src/dev/start-server.ts b/packages/wrangler/src/dev/start-server.ts index 537499b59033..6c37ac6b3735 100644 --- a/packages/wrangler/src/dev/start-server.ts +++ b/packages/wrangler/src/dev/start-server.ts @@ -499,6 +499,16 @@ async function runEsbuild({ workflowBindings, projectRoot, defineNavigatorUserAgent, + + // Pages specific options used by wrangler pages commands + entryName: undefined, + inject: undefined, + isOutfile: undefined, + external: undefined, + + watch: undefined, + sourcemap: undefined, + plugins: undefined, }) : undefined; diff --git a/packages/wrangler/src/dev/use-esbuild.ts b/packages/wrangler/src/dev/use-esbuild.ts index 6aa2545f3437..3246cc7d7fe1 100644 --- a/packages/wrangler/src/dev/use-esbuild.ts +++ b/packages/wrangler/src/dev/use-esbuild.ts @@ -200,6 +200,15 @@ export function runBuild( local, projectRoot, defineNavigatorUserAgent, + + // Pages specific options used by wrangler pages commands + entryName: undefined, + inject: undefined, + isOutfile: undefined, + external: undefined, + + // sourcemap defaults to true in dev + sourcemap: undefined, }) : undefined; diff --git a/packages/wrangler/src/pages/functions/buildPlugin.ts b/packages/wrangler/src/pages/functions/buildPlugin.ts index 5886a694da30..11f5234744c6 100644 --- a/packages/wrangler/src/pages/functions/buildPlugin.ts +++ b/packages/wrangler/src/pages/functions/buildPlugin.ts @@ -114,5 +114,13 @@ export function buildPluginFromFunctions({ local, projectRoot: getPagesProjectRoot(), defineNavigatorUserAgent, + + legacyAssets: undefined, + bypassAssetCache: undefined, + jsxFactory: undefined, + jsxFragment: undefined, + tsconfig: undefined, + testScheduled: undefined, + isOutfile: undefined, }); } diff --git a/packages/wrangler/src/pages/functions/buildWorker.ts b/packages/wrangler/src/pages/functions/buildWorker.ts index 883da0b8ce18..44db17ec84f3 100644 --- a/packages/wrangler/src/pages/functions/buildWorker.ts +++ b/packages/wrangler/src/pages/functions/buildWorker.ts @@ -68,7 +68,7 @@ export function buildWorkerFromFunctions({ additionalModules: [], moduleCollector, inject: [routesModule], - ...(outdir ? { entryName: "index" } : {}), + ...(outdir ? { entryName: "index" } : { entryName: undefined }), minify, sourcemap, watch, @@ -90,6 +90,13 @@ export function buildWorkerFromFunctions({ local, projectRoot: getPagesProjectRoot(), defineNavigatorUserAgent, + + legacyAssets: undefined, + bypassAssetCache: undefined, + jsxFactory: undefined, + jsxFragment: undefined, + tsconfig: undefined, + testScheduled: undefined, }); } @@ -195,6 +202,15 @@ export function buildRawWorker({ local, projectRoot: getPagesProjectRoot(), defineNavigatorUserAgent, + + legacyAssets: undefined, + bypassAssetCache: undefined, + jsxFactory: undefined, + jsxFragment: undefined, + tsconfig: undefined, + testScheduled: undefined, + entryName: undefined, + inject: undefined, }); } diff --git a/packages/wrangler/src/versions/upload.ts b/packages/wrangler/src/versions/upload.ts index 8d5040bb1557..91fe3472691e 100644 --- a/packages/wrangler/src/versions/upload.ts +++ b/packages/wrangler/src/versions/upload.ts @@ -11,6 +11,7 @@ import { } from "../deployment-bundle/bundle-reporter"; import { getBundleType } from "../deployment-bundle/bundle-type"; import { createWorkerUploadForm } from "../deployment-bundle/create-worker-upload-form"; +import { logBuildOutput } from "../deployment-bundle/esbuild-plugins/log-build-output"; import { findAdditionalModules, writeAdditionalModules, @@ -321,6 +322,17 @@ See https://developers.cloudflare.com/workers/platform/compatibility-dates for m props.compatibilityDate ?? config.compatibility_date, props.compatibilityFlags ?? config.compatibility_flags ), + plugins: [logBuildOutput(nodejsCompatMode)], + + // Pages specific options used by wrangler pages commands + entryName: undefined, + inject: undefined, + isOutfile: undefined, + external: undefined, + + // These options are dev-only + testScheduled: undefined, + watch: undefined, } );