Skip to content

Commit

Permalink
Add unit tests (#1)
Browse files Browse the repository at this point in the history
* chore: Add unit tests

* ci: preview deployments

* ci: preview deployments

* ci: preview deployments

* ci: preview deployments

* ci: remove preview deployments

* fix(ci): copy files on CI and windows instead of symlink

* fix(ci): copy files on CI and windows instead of symlink

* fix: preview command

* fix: preview command
  • Loading branch information
magne4000 committed Jun 26, 2024
1 parent c5a4542 commit 2d6b47d
Show file tree
Hide file tree
Showing 19 changed files with 637 additions and 48 deletions.
32 changes: 0 additions & 32 deletions .github/workflows/build.yml

This file was deleted.

44 changes: 44 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: CI
on: [ push, pull_request ]

jobs:
test:
# Prevent workflow being run twice, https://github.com/orgs/community/discussions/57827#discussioncomment-6579237
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name

name: ${{ matrix.cmd }} (${{ matrix.os }})

runs-on: ${{ matrix.os }}

strategy:
# Don't cancel other matrix operations if one fails
fail-fast: false
matrix:
os:
- ubuntu-latest
- windows-latest
cmd:
- pnpm run test:e2e
- pnpm run test:units
- pnpm run test:types
# `exclude` docs & alternatives:
# - https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstrategymatrixexclude
# - https://stackoverflow.com/questions/68994484/how-to-skip-a-configuration-of-a-matrix-with-github-actions/68994907#68994907
# - https://stackoverflow.com/questions/66025220/paired-values-in-github-actions-matrix/68940067#68940067
exclude:
- os: windows-latest
cmd: pnpm run test:types

steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: 18
cache: "pnpm"

- run: pnpm install
- run: pnpm exec playwright install chromium
- run: pnpm run build

- run: ${{ matrix.cmd }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,6 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# Cloudflare
.wrangler/
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
package-manager-strict=false
link-workspace-packages=deep
2 changes: 2 additions & 0 deletions examples/hono-app/.test-dev.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { testRun } from './.testRun'
testRun('pnpm run dev')
8 changes: 8 additions & 0 deletions examples/hono-app/.test-preview.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { testRun } from "./.testRun";

testRun("pnpm run preview --port 3000", {
doNotFailOnWarning: true,
serverIsReadyMessage: "Starting local server",
serverIsReadyDelay: 2000,
serverUrl: "http://127.0.0.1:3000",
});
63 changes: 63 additions & 0 deletions examples/hono-app/.testRun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { expect, fetchHtml, getServerUrl, page, run, test } from "@brillout/test-e2e";

export { testRun };

let isProd: boolean;

function testRun(cmd: `pnpm run ${"dev" | "preview"}${string}`, options?: Parameters<typeof run>[1]) {
run(cmd, options);

isProd = cmd !== "pnpm run dev";

testUrl({
url: "/",
title: "My Vike App",
text: "built with https://github.com/vikejs/vike-solid",
textHydration: "Rendered to HTML",
});

testUrl({
url: "/star-wars",
title: "6 Star Wars Movies",
text: "A New Hope",
});

testUrl({
url: "/star-wars/3",
title: "Return of the Jedi",
text: "1983-05-25",
});
}

function testUrl({
url,
title,
text,
textHydration,
noSSR,
}: {
url: string;
title: string;
text: string;
textHydration?: string;
counter?: true;
noSSR?: true;
}) {
test(url + " (HTML)", async () => {
const html = await fetchHtml(url);
if (!noSSR) {
expect(html).toContain(text);
}
expect(getTitle(html)).toBe(title);
});
test(url + " (Hydration)", async () => {
await page.goto(getServerUrl() + url);
const body = await page.textContent("body");
expect(body).toContain(textHydration ?? text);
});
}

function getTitle(html: string) {
const title = html.match(/<title>(.*?)<\/title>/i)?.[1];
return title;
}
6 changes: 3 additions & 3 deletions examples/hono-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "wrangler pages dev",
"deploy": "wrangler pages deploy",
"preview": "vite build && wrangler pages dev",
"deploy": "vite build && wrangler pages deploy",
"test": "tsc --noEmit"
},
"keywords": [],
Expand All @@ -19,7 +19,7 @@
"@hono/vite-dev-server": "^0.12.2",
"@types/node": "^18.19.14",
"typescript": "^5.5.2",
"vike-cloudflare": "workspace:*",
"vike-cloudflare": "^0.0.2",
"wrangler": "^3.61.0"
},
"dependencies": {
Expand Down
1 change: 0 additions & 1 deletion examples/hono-app/pages/+config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ export default {
Head,
// <title>
title: "My Vike App",
stream: "web",
extends: vikeSolid,
} satisfies Config;
6 changes: 6 additions & 0 deletions examples/hono-app/pages/star-wars/+config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Config } from "vike/types";

// Default config (can be overridden by pages)
export default {
stream: "web",
} satisfies Config;
2 changes: 2 additions & 0 deletions examples/vike-app/.test-dev.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { testRun } from './.testRun'
testRun('pnpm run dev')
8 changes: 8 additions & 0 deletions examples/vike-app/.test-preview.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { testRun } from "./.testRun";

testRun("pnpm run preview --port 3000", {
doNotFailOnWarning: true,
serverIsReadyMessage: "Starting local server",
serverIsReadyDelay: 2000,
serverUrl: "http://127.0.0.1:3000",
});
63 changes: 63 additions & 0 deletions examples/vike-app/.testRun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { expect, fetchHtml, getServerUrl, page, run, test } from "@brillout/test-e2e";

export { testRun };

let isProd: boolean;

function testRun(cmd: `pnpm run ${"dev" | "preview"}${string}`, options?: Parameters<typeof run>[1]) {
run(cmd, options);

isProd = cmd !== "pnpm run dev";

testUrl({
url: "/",
title: "My Vike App",
text: "built with https://github.com/vikejs/vike-solid",
textHydration: "Rendered to HTML",
});

testUrl({
url: "/star-wars",
title: "6 Star Wars Movies",
text: "A New Hope",
});

testUrl({
url: "/star-wars/3",
title: "Return of the Jedi",
text: "1983-05-25",
});
}

function testUrl({
url,
title,
text,
textHydration,
noSSR,
}: {
url: string;
title: string;
text: string;
textHydration?: string;
counter?: true;
noSSR?: true;
}) {
test(url + " (HTML)", async () => {
const html = await fetchHtml(url);
if (!noSSR) {
expect(html).toContain(text);
}
expect(getTitle(html)).toBe(title);
});
test(url + " (Hydration)", async () => {
await page.goto(getServerUrl() + url);
const body = await page.textContent("body");
expect(body).toContain(textHydration ?? text);
});
}

function getTitle(html: string) {
const title = html.match(/<title>(.*?)<\/title>/i)?.[1];
return title;
}
6 changes: 3 additions & 3 deletions examples/vike-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "wrangler pages dev",
"deploy": "wrangler pages deploy",
"preview": "vite build && wrangler pages dev",
"deploy": "vite build && wrangler pages deploy",
"test": "tsc --noEmit"
},
"keywords": [],
Expand All @@ -17,7 +17,7 @@
"@auth/core": "^0.34.1",
"@types/node": "^18.19.14",
"typescript": "^5.5.2",
"vike-cloudflare": "workspace:*",
"vike-cloudflare": "^0.0.2",
"wrangler": "^3.61.0"
},
"dependencies": {
Expand Down
15 changes: 14 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
"scripts": {
"build": "cd packages/vike-cloudflare && pnpm run build",
"release": "cd packages/vike-cloudflare && pnpm run release",
"========= Test": "",
"test": "pnpm run test:units && pnpm run test:e2e && pnpm run test:types",
"test:e2e": "test-e2e",
"test:units": "pnpm --recursive --sequential --filter {packages/*} run test",
"test:types": "test-types",
"========= Formatting": "",
"format": "git ls-files | egrep '\\.(json|js|jsx|css|ts|tsx|vue|mjs|cjs)$' | grep --invert-match package.json | xargs pnpm exec prettier --write",
"lint": "eslint .",
Expand All @@ -13,13 +18,21 @@
},
"keywords": [],
"author": "",
"pnpm": {
"overrides": {
"vike-cloudflare": "link:./packages/vike-cloudflare/"
}
},
"devDependencies": {
"@ianvs/prettier-plugin-sort-imports": "^4.2.1",
"@typescript-eslint/eslint-plugin": "^7.13.1",
"@typescript-eslint/parser": "^7.13.1",
"eslint": "^8.57.0",
"eslint-plugin-solid": "^0.14.1",
"prettier": "^3.3.2"
"prettier": "^3.3.2",
"@brillout/test-e2e": "^0.5.33",
"@brillout/test-types": "^0.1.15",
"playwright": "^1.42.1"
},
"packageManager": "pnpm@9.4.0"
}
3 changes: 3 additions & 0 deletions packages/vike-cloudflare/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@
- Docs at [`vike.dev/vike-cloudflare`](https://vike.dev/vike-cloudflare)
- [Examples](https://github.com/vikejs/vike-cloudflare/tree/main/examples)
- [CHANGELOG.md](https://github.com/vikejs/vike-cloudflare/tree/main/CHANGELOG.md)
- Demos:
- [Hono + Streaming](https://vike-clouflare-hono-demo.pages.dev/) ([code](https://github.com/vikejs/vike-cloudflare/tree/main/examples/hono-app))
- [Plain Vike](https://vike-clouflare-vike-demo.pages.dev/) ([code](https://github.com/vikejs/vike-cloudflare/tree/main/examples/vike-app))
12 changes: 10 additions & 2 deletions packages/vike-cloudflare/assets/vike.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference lib="webworker" />
import { renderPage } from "vike/server";

/**
Expand All @@ -15,8 +16,15 @@ async function handleSsr(url) {
return new Response("Something went wrong", { status: 500 });
} else {
const { statusCode: status, headers } = httpResponse;
const stream = httpResponse.getReadableWebStream();
return new Response(stream, { headers, status });

const { readable, writable } = new TransformStream();

httpResponse.pipe(writable);

return new Response(readable, {
status,
headers,
});
}
}

Expand Down
Loading

0 comments on commit 2d6b47d

Please sign in to comment.