Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import path from "node:path";
import { normalizeString, seed } from "@cloudflare/workers-utils/test-helpers";
import * as esbuild from "esbuild";
import dedent from "ts-dedent";
// eslint-disable-next-line no-restricted-imports
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { afterEach, beforeEach, describe, test, vi } from "vitest";
import { BundlerController } from "../../../api/startDevWorker/BundlerController";
import { FakeBus } from "../../helpers/fake-bus";
import { mockConsoleMethods } from "../../helpers/mock-console";
Expand Down Expand Up @@ -70,7 +69,7 @@ describe("BundleController", { retry: 5, timeout: 10_000 }, () => {
afterEach(() => controller.teardown());

describe("happy path bundle + watch", () => {
test("single ts source file", async () => {
test("single ts source file", async ({ expect }) => {
await seed({
"src/index.ts": dedent /* javascript */ `
export default {
Expand Down Expand Up @@ -130,7 +129,7 @@ describe("BundleController", { retry: 5, timeout: 10_000 }, () => {
`);
});

test("multiple ts source files", async () => {
test("multiple ts source files", async ({ expect }) => {
await seed({
"src/index.ts": dedent /* javascript */ `
import name from "./other"
Expand Down Expand Up @@ -183,7 +182,7 @@ describe("BundleController", { retry: 5, timeout: 10_000 }, () => {
`);
});

test("custom build", async () => {
test("custom build", async ({ expect }) => {
await seed({
"custom_build_dir/index.ts": dedent /* javascript */ `
export default {
Expand Down Expand Up @@ -257,7 +256,7 @@ describe("BundleController", { retry: 5, timeout: 10_000 }, () => {
});
});

test("module aliasing", async () => {
test("module aliasing", async ({ expect }) => {
await seed({
"src/index.ts": dedent /* javascript */ `
import name from "foo"
Expand Down Expand Up @@ -307,29 +306,32 @@ describe("BundleController", { retry: 5, timeout: 10_000 }, () => {
});

describe("switching", () => {
test("esbuild -> custom builds", { timeout: 500000 }, async () => {
await seed({
"src/index.ts": dedent /* javascript */ `
test(
"esbuild -> custom builds",
{ timeout: 500000 },
async ({ expect }) => {
await seed({
"src/index.ts": dedent /* javascript */ `
export default {
fetch(request, env, ctx) {
//comment
return new Response("hello world")
}
} satisfies ExportedHandler
`,
});
const config = configDefaults({
entrypoint: path.resolve("src/index.ts"),
projectRoot: path.resolve("src"),
});

const ev = bus.waitFor("bundleComplete", undefined, 500000);
controller.onConfigUpdate({
type: "configUpdate",
config: configDefaults(config),
});
expect(findSourceFile((await ev).bundle.entrypointSource, "index.ts"))
.toMatchInlineSnapshot(`
});
const config = configDefaults({
entrypoint: path.resolve("src/index.ts"),
projectRoot: path.resolve("src"),
});

const ev = bus.waitFor("bundleComplete", undefined, 500000);
controller.onConfigUpdate({
type: "configUpdate",
config: configDefaults(config),
});
expect(findSourceFile((await ev).bundle.entrypointSource, "index.ts"))
.toMatchInlineSnapshot(`
"// index.ts
var index_exports = {};
__export(index_exports, {
Expand All @@ -343,36 +345,37 @@ describe("BundleController", { retry: 5, timeout: 10_000 }, () => {
"
`);

// Now switch to custom builds and see that it rebundles
await seed({
"custom_build_dir/index.ts": dedent /* javascript */ `
// Now switch to custom builds and see that it rebundles
await seed({
"custom_build_dir/index.ts": dedent /* javascript */ `
export default {
fetch(request, env, ctx) {
//comment
return new Response("hello custom build")
}
} satisfies ExportedHandler
`,
});
const configCustom = configDefaults({
entrypoint: path.resolve("out.ts"),
projectRoot: path.resolve("."),
build: {
custom: {
command: `node -e "fs.cpSync('custom_build_dir/index.ts', 'out.ts')"`,
watch: "custom_build_dir",
});
const configCustom = configDefaults({
entrypoint: path.resolve("out.ts"),
projectRoot: path.resolve("."),
build: {
custom: {
command: `node -e "fs.cpSync('custom_build_dir/index.ts', 'out.ts')"`,
watch: "custom_build_dir",
},
moduleRoot: path.resolve("."),
},
moduleRoot: path.resolve("."),
},
});

const evCustom = bus.waitFor("bundleComplete", undefined, 500000);
controller.onConfigUpdate({
type: "configUpdate",
config: configCustom,
});
expect(findSourceFile((await evCustom).bundle.entrypointSource, "out.ts"))
.toMatchInlineSnapshot(`
});

const evCustom = bus.waitFor("bundleComplete", undefined, 500000);
controller.onConfigUpdate({
type: "configUpdate",
config: configCustom,
});
expect(
findSourceFile((await evCustom).bundle.entrypointSource, "out.ts")
).toMatchInlineSnapshot(`
"// out.ts
var out_exports = {};
__export(out_exports, {
Expand All @@ -386,30 +389,30 @@ describe("BundleController", { retry: 5, timeout: 10_000 }, () => {
"
`);

await vi.waitFor(
async () => {
// Make sure we are now watching and processing the custom builds after switching to them
const updatedSource = bus.waitFor(
"bundleComplete",
undefined,
500000
);
await seed({
"custom_build_dir/index.ts": dedent /* javascript */ `
await vi.waitFor(
async () => {
// Make sure we are now watching and processing the custom builds after switching to them
const updatedSource = bus.waitFor(
"bundleComplete",
undefined,
500000
);
await seed({
"custom_build_dir/index.ts": dedent /* javascript */ `
export default {
fetch(request, env, ctx) {
//comment
return new Response("hello custom build 2")
}
}
`,
});
expect(
findSourceFile(
(await updatedSource).bundle.entrypointSource,
"out.ts"
)
).toMatchInlineSnapshot(`
});
expect(
findSourceFile(
(await updatedSource).bundle.entrypointSource,
"out.ts"
)
).toMatchInlineSnapshot(`
"// out.ts
var out_exports = {};
__export(out_exports, {
Expand All @@ -422,12 +425,13 @@ describe("BundleController", { retry: 5, timeout: 10_000 }, () => {
};
"
`);
},
{ timeout: 5_000, interval: 500 }
);
});
},
{ timeout: 5_000, interval: 500 }
);
}
);

test("custom builds -> esbuild", async () => {
test("custom builds -> esbuild", async ({ expect }) => {
await seed({
"custom_build_dir/index.ts": dedent /* javascript */ `
export default {
Expand Down Expand Up @@ -537,7 +541,9 @@ describe("BundleController", { retry: 5, timeout: 10_000 }, () => {
});

describe("bundling error messages", () => {
test("should recommend alias when a non-Node module cannot be resolved", async () => {
test("should recommend alias when a non-Node module cannot be resolved", async ({
expect,
}) => {
await seed({
"src/index.ts": dedent /* javascript */ `
import foo from 'some-nonexistent-module';
Expand Down Expand Up @@ -576,7 +582,9 @@ describe("BundleController", { retry: 5, timeout: 10_000 }, () => {
`);
});

test("should NOT recommend alias for Node built-in modules", async () => {
test("should NOT recommend alias for Node built-in modules", async ({
expect,
}) => {
await seed({
"src/index.ts": dedent /* javascript */ `
import fs from 'fs';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import path from "node:path";
import { seed } from "@cloudflare/workers-utils/test-helpers";
import dedent from "ts-dedent";
// eslint-disable-next-line no-restricted-imports
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { afterEach, beforeEach, describe, it, vi } from "vitest";
import { ConfigController } from "../../../api/startDevWorker/ConfigController";
import { unwrapHook } from "../../../api/startDevWorker/utils";
import { logger } from "../../../logger";
Expand Down Expand Up @@ -40,7 +39,9 @@ describe("ConfigController", () => {
logger.resetLoggerLevel();
});

it("should prompt user to update types if they're out of date", async () => {
it("should prompt user to update types if they're out of date", async ({
expect,
}) => {
await seed({
"src/index.ts": dedent /* javascript */ `
export default {}
Expand Down Expand Up @@ -68,7 +69,9 @@ describe("ConfigController", () => {
});
});

it("should use account_id from config file before env var", async () => {
it("should use account_id from config file before env var", async ({
expect,
}) => {
await seed({
"src/index.ts": dedent /* javascript */ `
export default {}
Expand Down Expand Up @@ -105,7 +108,9 @@ describe("ConfigController", () => {
});
});

it("should emit configUpdate events with defaults applied", async () => {
it("should emit configUpdate events with defaults applied", async ({
expect,
}) => {
const event = bus.waitFor("configUpdate");
await seed({
"src/index.ts": dedent /* javascript */ `
Expand Down Expand Up @@ -137,7 +142,9 @@ describe("ConfigController", () => {
});
});

it("should apply module root to parent if main is nested from base_dir", async () => {
it("should apply module root to parent if main is nested from base_dir", async ({
expect,
}) => {
const event = bus.waitFor("configUpdate");
await seed({
"some/base_dir/nested/index.js": dedent /* javascript */ `
Expand Down Expand Up @@ -171,7 +178,7 @@ describe("ConfigController", () => {
});
});

it("should shallow merge patched config", async () => {
it("should shallow merge patched config", async ({ expect }) => {
const event1 = bus.waitFor("configUpdate");
await seed({
"src/index.ts": dedent /* javascript */ `
Expand Down Expand Up @@ -263,7 +270,9 @@ describe("ConfigController", () => {
});
});

it("should only log warnings once even with multiple config updates", async () => {
it("should only log warnings once even with multiple config updates", async ({
expect,
}) => {
await seed({
"src/index.js": dedent /* javascript */ `
addEventListener('fetch', event => {
Expand Down
Loading
Loading