Skip to content

Commit

Permalink
test: add test for incorrect entry-point path computation
Browse files Browse the repository at this point in the history
Follow up to #292
  • Loading branch information
petebacondarwin committed Jan 25, 2022
1 parent 5b7f46b commit fe5dcfd
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
4 changes: 2 additions & 2 deletions packages/wrangler/src/__tests__/mock-cfetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type MockHandler<ResponseType> = (
uri: RegExpExecArray,
init: RequestInit,
queryParams: URLSearchParams
) => ResponseType;
) => ResponseType | Promise<ResponseType>;

type RemoveMockFn = () => void;

Expand Down Expand Up @@ -41,7 +41,7 @@ export async function mockFetchInternal(
if (uri !== null && (!method || method === (init.method ?? "GET"))) {
// The `resource` regular expression will extract the labelled groups from the URL.
// These are passed through to the `handler` call, to allow it to do additional checks or behaviour.
return handler(uri, init, queryParams); // TODO: should we have some kind of fallthrough system? we'll see.
return await handler(uri, init, queryParams); // TODO: should we have some kind of fallthrough system? we'll see.
}
}
throw new Error(
Expand Down
75 changes: 70 additions & 5 deletions packages/wrangler/src/__tests__/publish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,54 @@ describe("publish", () => {
expect(stderr).toMatchInlineSnapshot(`""`);
expect(error).toMatchInlineSnapshot(`undefined`);
});

it("should be able to transpile TypeScript", async () => {
writeWranglerToml();
writeEsmWorkerSource({ format: "ts" });
mockUploadWorkerRequest({ expectedBody: "var foo = 100;" });
mockSubDomainRequest();

const { stdout, stderr, error } = await runWrangler(
"publish " + fs.realpathSync("./index.ts")
);

expect(stripTimings(stdout)).toMatchInlineSnapshot(`
"Uploaded
test-name
(TIMINGS)
Deployed
test-name
(TIMINGS)
test-name.test-sub-domain.workers.dev"
`);
expect(stderr).toMatchInlineSnapshot(`""`);
expect(error).toMatchInlineSnapshot(`undefined`);
});

it("should be able to transpile entry-points in sub-directories", async () => {
writeWranglerToml();
writeEsmWorkerSource({ basePath: "./src" });
mockUploadWorkerRequest({ expectedBody: "var foo = 100;" });
mockSubDomainRequest();

const { stdout, stderr, error } = await runWrangler(
"publish ./src/index.js"
);

expect(stripTimings(stdout)).toMatchInlineSnapshot(`
"Uploaded
test-name
(TIMINGS)
Deployed
test-name
(TIMINGS)
test-name.test-sub-domain.workers.dev"
`);
expect(stderr).toMatchInlineSnapshot(`""`);
expect(error).toMatchInlineSnapshot(`undefined`);
});
});

describe("asset upload", () => {
Expand Down Expand Up @@ -572,9 +620,15 @@ function writeWranglerToml(
}

/** Write a mock Worker script to disk. */
function writeEsmWorkerSource() {
function writeEsmWorkerSource({
basePath = ".",
format = "js",
}: { basePath?: string; format?: "js" | "ts" } = {}) {
if (basePath !== ".") {
fs.mkdirSync(basePath, { recursive: true });
}
fs.writeFileSync(
"index.js",
`${basePath}/index.${format}`,
[
`import { foo } from "./another";`,
`export default {`,
Expand All @@ -584,7 +638,7 @@ function writeEsmWorkerSource() {
`};`,
].join("\n")
);
fs.writeFileSync("another.js", `export const foo = 100;`);
fs.writeFileSync(`${basePath}/another.${format}`, `export const foo = 100;`);
}

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

/** Create a mock handler for the request to upload a worker script. */
function mockUploadWorkerRequest(available_on_subdomain = true) {
function mockUploadWorkerRequest({
available_on_subdomain = true,
expectedBody,
}: {
available_on_subdomain?: boolean;
expectedBody?: string;
} = {}) {
setMockResponse(
"/accounts/:accountId/workers/scripts/:scriptName",
"PUT",
([_url, accountId, scriptName], _init, queryParams) => {
async ([_url, accountId, scriptName], { body }, queryParams) => {
expect(accountId).toEqual("some-account-id");
expect(scriptName).toEqual("test-name");
expect(queryParams.get("available_on_subdomains")).toEqual("true");
if (expectedBody !== undefined) {
expect(
await ((body as FormData).get("index.js") as File).text()
).toMatch(expectedBody);
}
return { available_on_subdomain };
}
);
Expand Down

0 comments on commit fe5dcfd

Please sign in to comment.