Skip to content

Commit

Permalink
Add --x-dev-env to fixtures tests (#6330)
Browse files Browse the repository at this point in the history
* add --x-dev-env to fixtures usage of `wrangler dev`

* set experimental.devEnv flag on fixtures usage of `unstable_dev()`

* fixups

* fixups

* cleanup runWranglerLongLived with tree-kill

* disable --x-registry/fileBasedRegistry in external-durable-objects-app fixtures

* unstable_dev should wait for ProxyWorker and UserWorker to be ready

* fixup

* fixup

* use runWranglerDevPages instead of fork

* add changeset
  • Loading branch information
RamIdeas authored Jul 29, 2024
1 parent 5aa4753 commit cfbdede
Show file tree
Hide file tree
Showing 31 changed files with 186 additions and 111 deletions.
9 changes: 9 additions & 0 deletions .changeset/late-bikes-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"wrangler": patch
---

fix: when the worker's request.url is overridden using the `host` or `localUpstream`, ensure `port` is overridden/cleared too

When using `--localUpstream=example.com`, the request.url would incorrectly be "example.com:8787" but is now "example.com".

This only applies to `wrangler dev --x-dev-env` and `unstable_dev({ experimental: { devEnv: true } })`.
2 changes: 1 addition & 1 deletion fixtures/additional-modules/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"build": "wrangler deploy --dry-run --outdir=dist",
"check:type": "tsc",
"deploy": "wrangler deploy",
"start": "wrangler dev",
"start": "wrangler dev --x-dev-env",
"test:ci": "vitest run",
"test:watch": "vitest",
"type:tests": "tsc -p ./test/tsconfig.json"
Expand Down
2 changes: 1 addition & 1 deletion fixtures/d1-worker-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"db:query": "wrangler d1 execute UPDATE_THIS_FOR_REMOTE_USE --local --command='SELECT * FROM Customers'",
"db:query-json": "wrangler d1 execute UPDATE_THIS_FOR_REMOTE_USE --local --command='SELECT * FROM Customers' --json",
"db:reset": "wrangler d1 execute UPDATE_THIS_FOR_REMOTE_USE --local --file=./schema.sql",
"start": "wrangler dev --local"
"start": "wrangler dev --x-dev-env --local"
},
"devDependencies": {
"wrangler": "workspace:*"
Expand Down
2 changes: 1 addition & 1 deletion fixtures/external-durable-objects-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "A test for external durable objects",
"scripts": {
"check:type": "tsc",
"dev": "npx concurrently -s first -k \"wrangler dev a/index.ts --local --port 8400\" \"wrangler dev b/index.ts --local --port 8401\" \"npx wrangler dev c/index.ts --local --port 8402\" \"cd d && npx wrangler pages dev public --port 8403 --do PAGES_REFERENCED_DO=MyDurableObject@a\"",
"dev": "npx concurrently -s first -k \"wrangler dev --x-dev-env a/index.ts --local --port 8400\" \"wrangler dev --x-dev-env b/index.ts --local --port 8401\" \"npx wrangler dev --x-dev-env c/index.ts --local --port 8402\" \"cd d && npx wrangler pages dev public --port 8403 --do PAGES_REFERENCED_DO=MyDurableObject@a\"",
"test:ci": "vitest run",
"test:watch": "vitest",
"type:tests": "tsc -p ./tests/tsconfig.json"
Expand Down
67 changes: 14 additions & 53 deletions fixtures/external-durable-objects-app/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { fork } from "child_process";
import * as path from "path";
import { setTimeout } from "timers/promises";
import { fetch } from "undici";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { unstable_dev } from "wrangler";
import type { ChildProcess } from "child_process";
import { runWranglerPagesDev } from "../../shared/src/run-wrangler-long-lived";
import type { UnstableDevWorker } from "wrangler";

// TODO: reenable when https://github.com/cloudflare/workers-sdk/pull/4241 lands
Expand All @@ -15,91 +14,53 @@ describe(
let a: UnstableDevWorker;
let b: UnstableDevWorker;
let c: UnstableDevWorker;

let dWranglerProcess: ChildProcess;
let dIP: string;
let dPort: number;
let dResolveReadyPromise: (value: unknown) => void;
const dReadyPromise = new Promise((resolve) => {
dResolveReadyPromise = resolve;
});
let d: Awaited<ReturnType<typeof runWranglerPagesDev>>;

beforeAll(async () => {
a = await unstable_dev(path.join(__dirname, "../a/index.ts"), {
config: path.join(__dirname, "../a/wrangler.toml"),
experimental: {
fileBasedRegistry: true,
disableExperimentalWarning: true,
devEnv: true,
},
});
await setTimeout(1000);
b = await unstable_dev(path.join(__dirname, "../b/index.ts"), {
config: path.join(__dirname, "../b/wrangler.toml"),
experimental: {
fileBasedRegistry: true,
disableExperimentalWarning: true,
devEnv: true,
},
});
await setTimeout(1000);
c = await unstable_dev(path.join(__dirname, "../c/index.ts"), {
config: path.join(__dirname, "../c/wrangler.toml"),
experimental: {
fileBasedRegistry: true,
disableExperimentalWarning: true,
devEnv: true,
},
});
await setTimeout(1000);
dWranglerProcess = fork(
path.join(
"..",
"..",
"..",
"packages",
"wrangler",
"bin",
"wrangler.js"
),

d = await runWranglerPagesDev(
path.resolve(__dirname, "..", "d"),
"public",
[
"pages",
"dev",
"--x-registry",
"public",
"--compatibility-date=2024-03-04",
"--do=PAGES_REFERENCED_DO=MyDurableObject@a",
],
{
stdio: ["ignore", "ignore", "ignore", "ipc"],
cwd: path.resolve(__dirname, "..", "d"),
}
).on("message", (message) => {
const parsedMessage = JSON.parse(message.toString());
dIP = parsedMessage.ip;
dPort = parsedMessage.port;
dResolveReadyPromise(undefined);
});
await setTimeout(1000);
"--port=0",
]
);
});

afterAll(async () => {
await dReadyPromise;
await a.stop();
await b.stop();
await c.stop();

await new Promise((resolve, reject) => {
dWranglerProcess.once("exit", (code) => {
if (!code) {
resolve(code);
} else {
reject(code);
}
});
dWranglerProcess.kill("SIGTERM");
});
await d.stop();
});

it("connects up Durable Objects and keeps state across wrangler instances", async () => {
await dReadyPromise;
await setTimeout(1000);

const responseA = await a.fetch(`/`, {
Expand All @@ -115,7 +76,7 @@ describe(
const responseC = await c.fetch(`/`);
const dataC = (await responseC.json()) as { count: number; id: string };
expect(dataC.count).toEqual(3);
const responseD = await fetch(`http://${dIP}:${dPort}/`);
const responseD = await fetch(`http://${d.ip}:${d.port}/`);
const dataD = (await responseD.json()) as { count: number; id: string };
expect(dataD.count).toEqual(4);
const responseA2 = await a.fetch(`/`);
Expand Down
10 changes: 5 additions & 5 deletions fixtures/external-service-bindings-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"description": "A test for external service bindings",
"scripts": {
"dev": "npx concurrently -s first -k 'npm run mod-a-dev' 'npm run mod-b-dev' 'npm run ser-a-dev' 'npm run mod-c-dev' 'npm run mod-d-dev' 'npm run pages-dev'",
"mod-a-dev": "wrangler dev module-worker-a/index.ts --local --port 8500",
"mod-b-dev": "wrangler dev module-worker-b/index.ts --local --port 8501",
"mod-c-dev": "wrangler dev module-worker-c/index.ts --local --port 8503 --env staging",
"mod-d-dev": "wrangler dev module-worker-d/index.ts --local --port 8504 --env production",
"mod-a-dev": "wrangler dev --x-dev-env module-worker-a/index.ts --local --port 8500",
"mod-b-dev": "wrangler dev --x-dev-env module-worker-b/index.ts --local --port 8501",
"mod-c-dev": "wrangler dev --x-dev-env module-worker-c/index.ts --local --port 8503 --env staging",
"mod-d-dev": "wrangler dev --x-dev-env module-worker-d/index.ts --local --port 8504 --env production",
"pages-dev": "cd pages-functions-app && npx wrangler pages dev public --port 8505 --service MODULE_A_SERVICE=module-worker-a MODULE_B_SERVICE=module-worker-b SERVICE_A_SERVICE=service-worker-a STAGING_MODULE_C_SERVICE=module-worker-c@staging STAGING_MODULE_D_SERVICE=module-worker-d@staging",
"ser-a-dev": "wrangler dev service-worker-a/index.ts --local --port 8502",
"ser-a-dev": "wrangler dev --x-dev-env service-worker-a/index.ts --local --port 8502",
"test:ci": "vitest run",
"test:watch": "vitest",
"type:tests": "tsc --noEmit"
Expand Down
5 changes: 5 additions & 0 deletions fixtures/external-service-bindings-app/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe("Pages Functions", () => {
experimental: {
fileBasedRegistry: true,
disableExperimentalWarning: true,
devEnv: true,
},
}
);
Expand All @@ -53,6 +54,7 @@ describe("Pages Functions", () => {
experimental: {
fileBasedRegistry: true,
disableExperimentalWarning: true,
devEnv: true,
},
}
);
Expand All @@ -65,6 +67,7 @@ describe("Pages Functions", () => {
experimental: {
fileBasedRegistry: true,
disableExperimentalWarning: true,
devEnv: true,
},
}
);
Expand All @@ -78,6 +81,7 @@ describe("Pages Functions", () => {
experimental: {
fileBasedRegistry: true,
disableExperimentalWarning: true,
devEnv: true,
},
}
);
Expand All @@ -91,6 +95,7 @@ describe("Pages Functions", () => {
experimental: {
fileBasedRegistry: true,
disableExperimentalWarning: true,
devEnv: true,
},
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ async function startWorkers(): Promise<UnstableDevWorker[]> {
experimental: {
fileBasedRegistry: true,
disableExperimentalWarning: true,
devEnv: true,
},
});
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,9 @@ async function startWorkers(): Promise<UnstableDevWorker[]> {
return unstable_dev(path.join(workerPath, "index.ts"), {
config: path.join(workerPath, "wrangler.toml"),
ip: "127.0.0.1",
experimental: {
devEnv: true,
},
});
})
);
Expand Down
6 changes: 5 additions & 1 deletion fixtures/local-mode-tests/tests/logging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ it("logs startup errors", async () => {
config: path.resolve(__dirname, "..", "wrangler.logging.toml"),
// Intentionally omitting `compatibilityFlags: ["nodejs_compat"]`
ip: "127.0.0.1",
experimental: { disableExperimentalWarning: true },
experimental: {
disableExperimentalWarning: true,
disableDevRegistry: true,
devEnv: true,
},
}
);
await worker.stop();
Expand Down
1 change: 1 addition & 0 deletions fixtures/local-mode-tests/tests/module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe("module worker", () => {
experimental: {
disableExperimentalWarning: true,
disableDevRegistry: true,
devEnv: true,
},
}
);
Expand Down
3 changes: 3 additions & 0 deletions fixtures/local-mode-tests/tests/ports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,23 @@ describe("multiple workers", () => {
experimental: {
disableExperimentalWarning: true,
disableDevRegistry: true,
devEnv: true,
},
}),
unstable_dev(path.resolve(__dirname, "..", "src", "module.ts"), {
ip: "127.0.0.1",
experimental: {
disableExperimentalWarning: true,
disableDevRegistry: true,
devEnv: true,
},
}),
unstable_dev(path.resolve(__dirname, "..", "src", "module.ts"), {
ip: "127.0.0.1",
experimental: {
disableExperimentalWarning: true,
disableDevRegistry: true,
devEnv: true,
},
}),
]);
Expand Down
1 change: 1 addition & 0 deletions fixtures/local-mode-tests/tests/specified-port.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe("specific port", () => {
experimental: {
disableExperimentalWarning: true,
disableDevRegistry: true,
devEnv: true,
},
}
);
Expand Down
2 changes: 2 additions & 0 deletions fixtures/local-mode-tests/tests/sw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ describe("service worker", () => {
config: path.resolve(__dirname, "..", "wrangler.sw.toml"),
ip: "127.0.0.1",
experimental: {
disableDevRegistry: true,
disableExperimentalWarning: true,
devEnv: true,
},
});
});
Expand Down
5 changes: 4 additions & 1 deletion fixtures/local-mode-tests/vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import configShared from "../../vitest.shared";
export default mergeConfig(
configShared,
defineProject({
test: {},
test: {
maxConcurrency: 1,
fileParallelism: false,
},
})
);
2 changes: 1 addition & 1 deletion fixtures/no-bundle-import/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"private": true,
"scripts": {
"deploy": "wrangler deploy",
"start": "wrangler dev",
"start": "wrangler dev --x-dev-env",
"test:ci": "vitest run",
"test:watch": "vitest"
},
Expand Down
1 change: 1 addition & 0 deletions fixtures/no-bundle-import/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe("Worker", () => {
ip: "127.0.0.1",
experimental: {
disableExperimentalWarning: true,
devEnv: true,
},
});
}, 30_000);
Expand Down
2 changes: 1 addition & 1 deletion fixtures/nodejs-hybrid-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"private": true,
"scripts": {
"build": "wrangler deploy --dry-run --outdir=./dist",
"dev": "wrangler dev",
"dev": "wrangler dev --x-dev-env",
"test:ci": "vitest run",
"test:watch": "vitest"
},
Expand Down
2 changes: 1 addition & 1 deletion fixtures/service-bindings-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "A test for service bindings",
"scripts": {
"check:type": "tsc",
"dev": "concurrently -s first -k \"wrangler dev a/index.ts --port=8300 --inspector-port=0\" \"wrangler dev b/index.ts --port=8301 --inspector-port=0\"",
"dev": "concurrently -s first -k \"wrangler dev --x-dev-env a/index.ts --port=8300 --inspector-port=0\" \"wrangler dev --x-dev-env b/index.ts --port=8301 --inspector-port=0\"",
"test:ci": "vitest run",
"test:watch": "vitest",
"type:tests": "tsc -p ./tests/tsconfig.json"
Expand Down
2 changes: 2 additions & 0 deletions fixtures/service-bindings-app/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe("Service Bindings", () => {
experimental: {
fileBasedRegistry: true,
disableExperimentalWarning: true,
devEnv: true,
},
});
await setTimeout(1000);
Expand All @@ -22,6 +23,7 @@ describe("Service Bindings", () => {
experimental: {
fileBasedRegistry: true,
disableExperimentalWarning: true,
devEnv: true,
},
});
await setTimeout(1000);
Expand Down
Loading

0 comments on commit cfbdede

Please sign in to comment.