Skip to content

Commit 5f8155a

Browse files
further e2e test stability improvements (#10731)
Includes fixing private packages check
1 parent 9e37dda commit 5f8155a

File tree

7 files changed

+111
-133
lines changed

7 files changed

+111
-133
lines changed

packages/wrangler/e2e/helpers/e2e-wrangler-test.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ import {
1919
} from "./wrangler";
2020
import type { WranglerCommandOptions } from "./wrangler";
2121

22+
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
23+
export function importWrangler(): Promise<typeof import("../../src/cli")> {
24+
return import(WRANGLER_IMPORT.href);
25+
}
26+
27+
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
28+
export function importMiniflare(): Promise<typeof import("miniflare")> {
29+
return import(MINIFLARE_IMPORT.href);
30+
}
31+
2232
/**
2333
* Use this class in your e2e tests to create a temp directory, seed it with files
2434
* and then run various Wrangler commands.
@@ -42,16 +52,6 @@ export class WranglerE2ETestHelper {
4252
await removeFiles(this.tmpPath, files);
4353
}
4454

45-
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
46-
importWrangler(): Promise<typeof import("../../src/cli")> {
47-
return import(WRANGLER_IMPORT.href);
48-
}
49-
50-
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
51-
importMiniflare(): Promise<typeof import("miniflare")> {
52-
return import(MINIFLARE_IMPORT.href);
53-
}
54-
5555
runLongLived(
5656
wranglerCommand: string,
5757
{
@@ -76,7 +76,6 @@ export class WranglerE2ETestHelper {
7676
wranglerCommand: string,
7777
{ cwd = this.tmpPath, ...options }: WranglerCommandOptions = {}
7878
) {
79-
console.log(`Running wrangler command: ${wranglerCommand}`);
8079
return runWrangler(wranglerCommand, { cwd, ...options });
8180
}
8281

packages/wrangler/e2e/remote-binding/miniflare-remote-resources.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ import path from "node:path";
22
import dedent from "ts-dedent";
33
import { beforeEach, describe, expect, it, onTestFinished } from "vitest";
44
import { CLOUDFLARE_ACCOUNT_ID } from "../helpers/account-id";
5-
import { WranglerE2ETestHelper } from "../helpers/e2e-wrangler-test";
5+
import {
6+
importMiniflare,
7+
importWrangler,
8+
WranglerE2ETestHelper,
9+
} from "../helpers/e2e-wrangler-test";
610
import { generateResourceName } from "../helpers/generate-resource-name";
7-
import type { startRemoteProxySession } from "../../src/api";
811
import type { RawConfig } from "../../src/config";
912
import type { RemoteProxyConnectionString, WorkerOptions } from "miniflare";
1013
import type { ExpectStatic } from "vitest";
1114

15+
const { startRemoteProxySession } = await importWrangler();
16+
const { Miniflare } = await importMiniflare();
17+
1218
type TestCase<T = void> = {
1319
name: string;
1420
scriptPath: string;
@@ -522,8 +528,6 @@ async function runTestCase<T>(
522528
helper: WranglerE2ETestHelper,
523529
{ disableRemoteBindings } = { disableRemoteBindings: false }
524530
) {
525-
const { startRemoteProxySession } = await helper.importWrangler();
526-
const { Miniflare } = await helper.importMiniflare();
527531
await helper.seed(path.resolve(__dirname, "./workers"));
528532
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
529533
const setupResult = (await testCase.setup?.(helper))!;

packages/wrangler/e2e/remote-binding/remote-bindings-api.test.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,25 @@ import assert from "node:assert";
22
import { resolve } from "node:path";
33
import { beforeAll, describe, expect, test } from "vitest";
44
import { CLOUDFLARE_ACCOUNT_ID } from "../helpers/account-id";
5-
import { WranglerE2ETestHelper } from "../helpers/e2e-wrangler-test";
5+
import {
6+
importMiniflare,
7+
importWrangler,
8+
WranglerE2ETestHelper,
9+
} from "../helpers/e2e-wrangler-test";
610
import { generateResourceName } from "../helpers/generate-resource-name";
711
import type {
8-
Miniflare,
912
MiniflareOptions,
13+
Miniflare as MiniflareType,
1014
RemoteProxyConnectionString,
1115
Response,
1216
} from "miniflare";
1317

18+
const { Miniflare } = await importMiniflare();
19+
const {
20+
startRemoteProxySession: startRemoteProxySession,
21+
maybeStartOrUpdateRemoteProxySession: maybeStartOrUpdateRemoteProxySession,
22+
} = await importWrangler();
23+
1424
// Note: the tests in this file are simple ones that check basic functionalities of the remote bindings programmatic APIs
1525
// various other aspects of these APIs (e.g. different bindings, reloading capabilities) are indirectly tested when
1626
// generally testing remote bindings
@@ -21,14 +31,6 @@ describe.skipIf(!CLOUDFLARE_ACCOUNT_ID)(
2131
const remoteWorkerName = generateResourceName();
2232
const helper = new WranglerE2ETestHelper();
2333

24-
const { Miniflare } = await helper.importMiniflare();
25-
26-
const {
27-
startRemoteProxySession: startRemoteProxySession,
28-
maybeStartOrUpdateRemoteProxySession:
29-
maybeStartOrUpdateRemoteProxySession,
30-
} = await helper.importWrangler();
31-
3234
beforeAll(async () => {
3335
await helper.seed(resolve(__dirname, "./workers"));
3436
const { cleanup } = await helper.worker({
@@ -254,7 +256,7 @@ describe.skipIf(!CLOUDFLARE_ACCOUNT_ID)(
254256
}
255257
);
256258

257-
async function timedDispatchFetch(mf: Miniflare): Promise<Response | null> {
259+
async function timedDispatchFetch(mf: MiniflareType): Promise<Response | null> {
258260
try {
259261
return await mf.dispatchFetch("http://localhost/", {
260262
signal: AbortSignal.timeout(5000),

packages/wrangler/e2e/remote-binding/start-worker-remote-bindings.test.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ import { resolve } from "node:path";
33
import { setTimeout } from "node:timers/promises";
44
import { beforeAll, describe, expect, it } from "vitest";
55
import { CLOUDFLARE_ACCOUNT_ID } from "../helpers/account-id";
6-
import { WranglerE2ETestHelper } from "../helpers/e2e-wrangler-test";
6+
import {
7+
importWrangler,
8+
WranglerE2ETestHelper,
9+
} from "../helpers/e2e-wrangler-test";
710
import { generateResourceName } from "../helpers/generate-resource-name";
811

12+
const { unstable_startWorker: startWorker } = await importWrangler();
13+
914
describe.skipIf(!CLOUDFLARE_ACCOUNT_ID)("startWorker - remote bindings", () => {
1015
const remoteWorkerName = generateResourceName();
1116
const helper = new WranglerE2ETestHelper();
@@ -47,8 +52,7 @@ describe.skipIf(!CLOUDFLARE_ACCOUNT_ID)("startWorker - remote bindings", () => {
4752
}),
4853
});
4954

50-
const { unstable_startWorker } = await helper.importWrangler();
51-
const worker = await unstable_startWorker({
55+
const worker = await startWorker({
5256
config: `${helper.tmpPath}/wrangler.json`,
5357
dev: {
5458
experimentalRemoteBindings,
@@ -82,9 +86,7 @@ describe.skipIf(!CLOUDFLARE_ACCOUNT_ID)("startWorker - remote bindings", () => {
8286
}),
8387
});
8488

85-
const { unstable_startWorker } = await helper.importWrangler();
86-
87-
const worker = await unstable_startWorker({
89+
const worker = await startWorker({
8890
config: `${helper.tmpPath}/wrangler.json`,
8991
dev: {
9092
experimentalRemoteBindings,

packages/wrangler/e2e/start-worker-auth-opts.test.ts

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
import assert from "node:assert";
22
import path from "node:path";
33
import dedent from "ts-dedent";
4-
import { beforeAll, beforeEach, describe, expect, test, vi } from "vitest";
4+
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
55
import { CLOUDFLARE_ACCOUNT_ID } from "./helpers/account-id";
6-
import { WranglerE2ETestHelper } from "./helpers/e2e-wrangler-test";
6+
import {
7+
importWrangler,
8+
WranglerE2ETestHelper,
9+
} from "./helpers/e2e-wrangler-test";
710
import type { Worker } from "../src/api/startDevWorker";
811
import type { MockInstance } from "vitest";
912

10-
type Wrangler = Awaited<ReturnType<WranglerE2ETestHelper["importWrangler"]>>;
11-
12-
describe("startWorker - auth options", () => {
13-
let consoleErrorMock: MockInstance<typeof console.error>;
14-
15-
beforeAll(() => {
16-
consoleErrorMock = vi.spyOn(console, "error").mockImplementation(() => {});
17-
});
13+
const { unstable_startWorker: startWorker } = await importWrangler();
1814

15+
describe("startWorker - auth options", { sequential: true }, () => {
16+
let worker: Worker | undefined;
1917
describe.skipIf(!CLOUDFLARE_ACCOUNT_ID)("with remote bindings", () => {
2018
let helper: WranglerE2ETestHelper;
21-
let wrangler: Wrangler;
22-
let startWorker: Wrangler["unstable_startWorker"];
19+
20+
let consoleErrorMock: MockInstance<typeof console.error>;
2321

2422
beforeEach(async () => {
23+
consoleErrorMock = vi.spyOn(console, "error").mockImplementation(() => {
24+
// suppress error output during tests - we are going to check for specific error messages in the tests themselves
25+
});
26+
2527
helper = new WranglerE2ETestHelper();
2628
const aiWorkerScript = dedent`
2729
export default {
@@ -45,13 +47,11 @@ describe("startWorker - auth options", () => {
4547
await helper.seed({
4648
"src/index.js": aiWorkerScript,
4749
});
48-
wrangler = await helper.importWrangler();
49-
startWorker = wrangler.unstable_startWorker;
5050
});
5151

52-
test("starting a worker with startWorker with the valid auth information and updating it with invalid information", async (t) => {
53-
t.onTestFinished(async () => await worker?.dispose());
52+
afterEach(() => worker?.dispose());
5453

54+
test("starting a worker with startWorker with the valid auth information and updating it with invalid information", async () => {
5555
const validAuth = vi.fn(() => {
5656
assert(process.env.CLOUDFLARE_API_TOKEN);
5757

@@ -63,7 +63,7 @@ describe("startWorker - auth options", () => {
6363
};
6464
});
6565

66-
const worker = await startWorker({
66+
worker = await startWorker({
6767
entrypoint: path.resolve(helper.tmpPath, "src/index.js"),
6868
bindings: {
6969
AI: {
@@ -80,7 +80,7 @@ describe("startWorker - auth options", () => {
8080
},
8181
});
8282

83-
await assertValidWorkerAiResponse(worker);
83+
await assertValidWorkerAiResponse();
8484

8585
expect(validAuth).toHaveBeenCalledOnce();
8686

@@ -101,14 +101,12 @@ describe("startWorker - auth options", () => {
101101
},
102102
});
103103

104-
await assertInvalidWorkerAiResponse(worker);
104+
await assertInvalidWorkerAiResponse();
105105

106106
expect(incorrectAuth).toHaveBeenCalledOnce();
107107
});
108108

109-
test("starting a worker with startWorker with invalid auth information and updating it with valid auth information", async (t) => {
110-
t.onTestFinished(async () => await worker?.dispose());
111-
109+
test("starting a worker with startWorker with invalid auth information and updating it with valid auth information", async () => {
112110
const incorrectAuth = vi.fn(() => {
113111
return {
114112
accountId: CLOUDFLARE_ACCOUNT_ID,
@@ -118,7 +116,7 @@ describe("startWorker - auth options", () => {
118116
};
119117
});
120118

121-
const worker = await startWorker({
119+
worker = await startWorker({
122120
entrypoint: path.resolve(helper.tmpPath, "src/index.js"),
123121
bindings: {
124122
AI: {
@@ -135,7 +133,7 @@ describe("startWorker - auth options", () => {
135133
},
136134
});
137135

138-
await assertInvalidWorkerAiResponse(worker);
136+
await assertInvalidWorkerAiResponse();
139137

140138
expect(incorrectAuth).toHaveBeenCalledOnce();
141139

@@ -158,12 +156,13 @@ describe("startWorker - auth options", () => {
158156
},
159157
});
160158

161-
await assertValidWorkerAiResponse(worker);
159+
await assertValidWorkerAiResponse();
162160

163161
expect(validAuth).toHaveBeenCalledOnce();
164162
});
165163

166-
async function assertValidWorkerAiResponse(worker: Worker) {
164+
async function assertValidWorkerAiResponse() {
165+
assert(worker, "Worker is not defined");
167166
const responseText = await fetchTimedTextFromWorker(worker);
168167

169168
// We've fixed the auth information so now we can indeed get
@@ -179,7 +178,8 @@ describe("startWorker - auth options", () => {
179178
);
180179
}
181180

182-
async function assertInvalidWorkerAiResponse(worker: Worker) {
181+
async function assertInvalidWorkerAiResponse() {
182+
assert(worker, "Worker is not defined");
183183
const responseText = await fetchTimedTextFromWorker(worker);
184184

185185
// The remote connection is not established so we can't successfully
@@ -196,12 +196,8 @@ describe("startWorker - auth options", () => {
196196
});
197197

198198
describe("without remote bindings (no auth is needed)", () => {
199-
test("starting a worker via startWorker without any remote bindings (doesn't cause wrangler to try to get the auth information)", async (t) => {
200-
t.onTestFinished(async () => await worker?.dispose());
201-
199+
test("starting a worker via startWorker without any remote bindings (doesn't cause wrangler to try to get the auth information)", async () => {
202200
const helper = new WranglerE2ETestHelper();
203-
const wrangler = await helper.importWrangler();
204-
const startWorker = wrangler.unstable_startWorker;
205201

206202
const simpleWorkerScript = dedent`
207203
export default {
@@ -223,7 +219,7 @@ describe("startWorker - auth options", () => {
223219
};
224220
});
225221

226-
const worker = await startWorker({
222+
worker = await startWorker({
227223
entrypoint: path.resolve(helper.tmpPath, "src/index.js"),
228224
dev: {
229225
auth: someAuth,
@@ -255,6 +251,7 @@ async function fetchTimedTextFromWorker(
255251
let responseText: string | null = null;
256252

257253
try {
254+
assert(worker, "Worker is not defined");
258255
await vi.waitFor(
259256
async () => {
260257
responseText = await (

0 commit comments

Comments
 (0)