Skip to content

Commit fe5dcfd

Browse files
test: add test for incorrect entry-point path computation
Follow up to #292
1 parent 5b7f46b commit fe5dcfd

File tree

2 files changed

+72
-7
lines changed

2 files changed

+72
-7
lines changed

packages/wrangler/src/__tests__/mock-cfetch.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export type MockHandler<ResponseType> = (
1111
uri: RegExpExecArray,
1212
init: RequestInit,
1313
queryParams: URLSearchParams
14-
) => ResponseType;
14+
) => ResponseType | Promise<ResponseType>;
1515

1616
type RemoveMockFn = () => void;
1717

@@ -41,7 +41,7 @@ export async function mockFetchInternal(
4141
if (uri !== null && (!method || method === (init.method ?? "GET"))) {
4242
// The `resource` regular expression will extract the labelled groups from the URL.
4343
// These are passed through to the `handler` call, to allow it to do additional checks or behaviour.
44-
return handler(uri, init, queryParams); // TODO: should we have some kind of fallthrough system? we'll see.
44+
return await handler(uri, init, queryParams); // TODO: should we have some kind of fallthrough system? we'll see.
4545
}
4646
}
4747
throw new Error(

packages/wrangler/src/__tests__/publish.test.ts

+70-5
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,54 @@ describe("publish", () => {
5757
expect(stderr).toMatchInlineSnapshot(`""`);
5858
expect(error).toMatchInlineSnapshot(`undefined`);
5959
});
60+
61+
it("should be able to transpile TypeScript", async () => {
62+
writeWranglerToml();
63+
writeEsmWorkerSource({ format: "ts" });
64+
mockUploadWorkerRequest({ expectedBody: "var foo = 100;" });
65+
mockSubDomainRequest();
66+
67+
const { stdout, stderr, error } = await runWrangler(
68+
"publish " + fs.realpathSync("./index.ts")
69+
);
70+
71+
expect(stripTimings(stdout)).toMatchInlineSnapshot(`
72+
"Uploaded
73+
test-name
74+
(TIMINGS)
75+
Deployed
76+
test-name
77+
(TIMINGS)
78+
79+
test-name.test-sub-domain.workers.dev"
80+
`);
81+
expect(stderr).toMatchInlineSnapshot(`""`);
82+
expect(error).toMatchInlineSnapshot(`undefined`);
83+
});
84+
85+
it("should be able to transpile entry-points in sub-directories", async () => {
86+
writeWranglerToml();
87+
writeEsmWorkerSource({ basePath: "./src" });
88+
mockUploadWorkerRequest({ expectedBody: "var foo = 100;" });
89+
mockSubDomainRequest();
90+
91+
const { stdout, stderr, error } = await runWrangler(
92+
"publish ./src/index.js"
93+
);
94+
95+
expect(stripTimings(stdout)).toMatchInlineSnapshot(`
96+
"Uploaded
97+
test-name
98+
(TIMINGS)
99+
Deployed
100+
test-name
101+
(TIMINGS)
102+
103+
test-name.test-sub-domain.workers.dev"
104+
`);
105+
expect(stderr).toMatchInlineSnapshot(`""`);
106+
expect(error).toMatchInlineSnapshot(`undefined`);
107+
});
60108
});
61109

62110
describe("asset upload", () => {
@@ -572,9 +620,15 @@ function writeWranglerToml(
572620
}
573621

574622
/** Write a mock Worker script to disk. */
575-
function writeEsmWorkerSource() {
623+
function writeEsmWorkerSource({
624+
basePath = ".",
625+
format = "js",
626+
}: { basePath?: string; format?: "js" | "ts" } = {}) {
627+
if (basePath !== ".") {
628+
fs.mkdirSync(basePath, { recursive: true });
629+
}
576630
fs.writeFileSync(
577-
"index.js",
631+
`${basePath}/index.${format}`,
578632
[
579633
`import { foo } from "./another";`,
580634
`export default {`,
@@ -584,7 +638,7 @@ function writeEsmWorkerSource() {
584638
`};`,
585639
].join("\n")
586640
);
587-
fs.writeFileSync("another.js", `export const foo = 100;`);
641+
fs.writeFileSync(`${basePath}/another.${format}`, `export const foo = 100;`);
588642
}
589643

590644
/** Write mock assets to the file system so they can be uploaded. */
@@ -596,14 +650,25 @@ function writeAssets(assets: { filePath: string; content: string }[]) {
596650
}
597651

598652
/** Create a mock handler for the request to upload a worker script. */
599-
function mockUploadWorkerRequest(available_on_subdomain = true) {
653+
function mockUploadWorkerRequest({
654+
available_on_subdomain = true,
655+
expectedBody,
656+
}: {
657+
available_on_subdomain?: boolean;
658+
expectedBody?: string;
659+
} = {}) {
600660
setMockResponse(
601661
"/accounts/:accountId/workers/scripts/:scriptName",
602662
"PUT",
603-
([_url, accountId, scriptName], _init, queryParams) => {
663+
async ([_url, accountId, scriptName], { body }, queryParams) => {
604664
expect(accountId).toEqual("some-account-id");
605665
expect(scriptName).toEqual("test-name");
606666
expect(queryParams.get("available_on_subdomains")).toEqual("true");
667+
if (expectedBody !== undefined) {
668+
expect(
669+
await ((body as FormData).get("index.js") as File).text()
670+
).toMatch(expectedBody);
671+
}
607672
return { available_on_subdomain };
608673
}
609674
);

0 commit comments

Comments
 (0)