Skip to content

Commit

Permalink
Add Pages Functions example test project (#617)
Browse files Browse the repository at this point in the history
  • Loading branch information
GregBrimble committed Mar 16, 2022
1 parent 0f66675 commit 8c98c00
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-apricots-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"pages-functions-example": patch
---

Added a Pages Functions example project for tests
6 changes: 6 additions & 0 deletions packages/example-pages-functions-app/functions/blog/[slug].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const onRequestGet: PagesFunction<unknown, "slug"> = ({ params }) => {
const { slug } = params;
return new Response(`<h1>A blog with a slug: ${slug}</h1>`, {
headers: { "content-type": "text/html" },
});
};
3 changes: 3 additions & 0 deletions packages/example-pages-functions-app/functions/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const onRequest = () => {
return new Response(new Date().toISOString());
};
10 changes: 10 additions & 0 deletions packages/example-pages-functions-app/functions/intercept.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const onRequest: PagesFunction = async ({ next }) => {
const response = await next();
return new Response(response.body, {
status: response.status,
headers: {
...Object.fromEntries(response.headers.entries()),
"x-set-from-functions": "true",
},
});
};
34 changes: 34 additions & 0 deletions packages/example-pages-functions-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"private": true,
"name": "pages-functions-example",
"scripts": {
"dev": "npx wrangler pages dev public --port 8789",
"test": "npx jest --forceExit"
},
"devDependencies": {
"@cloudflare/workers-types": "^3.2.0",
"typescript": "^4.1.2",
"undici": "^4.13.0"
},
"engines": {
"node": ">=14"
},
"sideEffects": false,
"main": "dist/worker.js",
"jest": {
"restoreMocks": true,
"testTimeout": 30000,
"testRegex": ".*.(test|spec)\\.[jt]sx?$",
"transformIgnorePatterns": [
"node_modules/(?!find-up|locate-path|p-locate|p-limit|yocto-queue|path-exists|execa|strip-final-newline|npm-run-path|path-key|onetime|mimic-fn|human-signals|is-stream)"
],
"transform": {
"^.+\\.c?(t|j)sx?$": [
"esbuild-jest",
{
"sourcemap": true
}
]
}
}
}
1 change: 1 addition & 0 deletions packages/example-pages-functions-app/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Hello, world!</h1>
63 changes: 63 additions & 0 deletions packages/example-pages-functions-app/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { spawn } from "child_process";
import { resolve } from "path";
import { fetch } from "undici";
import type { Response } from "undici";

const waitUntilReady = async (url: string): Promise<Response> => {
let response: Response | undefined = undefined;

while (response === undefined) {
await new Promise((resolvePromise) => setTimeout(resolvePromise, 500));

try {
response = await fetch(url);
} catch {}
}

return response as Response;
};

const isWindows = process.platform === "win32";

describe("Remix", () => {
beforeAll(async () => {
const wranglerProcess = spawn("npm", ["run", "dev"], {
shell: isWindows,
cwd: resolve(__dirname, "../"),
env: { BROWSER: "none", ...process.env },
});
wranglerProcess.stdout.on("data", (chunk) => {
console.log(chunk.toString());
});
wranglerProcess.stderr.on("data", (chunk) => {
console.log(chunk.toString());
});
});

it("renders static pages", async () => {
const response = await waitUntilReady("http://localhost:8789/");
const text = await response.text();
expect(text).toContain("Hello, world!");
});

it("intercepts static requests with next()", async () => {
const response = await waitUntilReady("http://localhost:8789/intercept");
const text = await response.text();
expect(text).toContain("Hello, world!");
expect(response.headers.get("x-set-from-functions")).toBe("true");
});

it("can make SSR responses", async () => {
const response = await waitUntilReady("http://localhost:8789/date");
const text = await response.text();
expect(text).toMatch(/\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d/);
});

it("can use parameters", async () => {
const response = await waitUntilReady(
"http://localhost:8789/blog/hello-world"
);
const text = await response.text();
expect(text).toContain("<h1>A blog with a slug: hello-world</h1>");
});
});
9 changes: 9 additions & 0 deletions packages/example-pages-functions-app/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"include": ["functions"],
"compilerOptions": {
"target": "ES2020",
"module": "CommonJS",
"lib": ["ES2020"],
"types": ["@cloudflare/workers-types"]
}
}

0 comments on commit 8c98c00

Please sign in to comment.