Skip to content

Commit

Permalink
adding tests and new ci configs
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenmelnicki committed Jan 25, 2024
1 parent b2896b8 commit 0b5d89e
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 7 deletions.
8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2
updates:

- package-ecosystem: "github-actions"
directory: "/"
schedule:
# Check for updates to GitHub Actions every week
interval: "weekly"
59 changes: 59 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: ci

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
runs-on: ${{ matrix.os }}
timeout-minutes: 10

strategy:
fail-fast: false
matrix:
deno: ["canary", "v1.x"]
os: [macOS-latest, windows-latest, ubuntu-latest]
include:
- os: ubuntu-latest
cache_path: ~/.cache/deno/
- os: macos-latest
cache_path: ~/Library/Caches/deno/
- os: windows-latest
cache_path: ~\AppData\Local\deno\

steps:
- name: Checkout repo
uses: actions/checkout@v4

- name: Setup Deno
uses: denoland/setup-deno@v1
with:
deno-version: ${{ matrix.deno }}

- name: Verify formatting
run: deno fmt --check

- name: Run linter
run: deno lint

- name: Cache dependencies and Chrome
uses: actions/cache@v4
with:
path: |
${{ matrix.cache_path }}deps
${{ matrix.cache_path }}deno_puppeteer
key: ${{ runner.os }}-${{ hashFiles('**/*deps.ts', 'tests/fixture_twind_hydrate/twind.config.ts') }}

- name: Install Chromium
run: deno run -A --unstable https://deno.land/x/puppeteer@16.2.0/install.ts
env:
PUPPETEER_PRODUCT: chrome

- name: Type check project
run: deno task check

- name: Run tests
run: deno task test
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

steps:
- name: Clone repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Install Deno
uses: denoland/setup-deno@v1
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
_fresh/
# npm dependencies
node_modules/
# test coverage directory
coverage/
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"lock": false,
"tasks": {
"test": "deno test -A --parallel --unstable-kv",
"check": "deno fmt --check && deno lint && deno check **/*.ts && deno check **/*.tsx",
"cli": "echo \"import '\\$fresh/src/dev/cli.ts'\" | deno run --unstable -A -",
"manifest": "deno task cli manifest $(pwd)",
"start": "deno run -A --watch=static/,routes/ --unstable-kv dev.ts",
"build": "deno run -A --unstable-kv dev.ts build",
"preview": "deno run -A --unstable-kv main.ts",
"update": "deno run -A -r https://fresh.deno.dev/update ."
"preview": "deno run -A --unstable-kv main.ts"
},
"lint": {
"rules": {
Expand Down
6 changes: 2 additions & 4 deletions fresh.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import * as $_id_ from "./routes/[id].tsx";
import * as $_app from "./routes/_app.tsx";
import * as $index from "./routes/index.tsx";
import * as $UploadForm from "./islands/UploadForm.tsx";

import { type Manifest } from "$fresh/server.ts";

const manifest = {
Expand All @@ -14,9 +14,7 @@ const manifest = {
"./routes/_app.tsx": $_app,
"./routes/index.tsx": $index,
},
islands: {
"./islands/UploadForm.tsx": $UploadForm,
},
islands: {},
baseUrl: import.meta.url,
} satisfies Manifest;

Expand Down
17 changes: 17 additions & 0 deletions tests/id_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { assertEquals, assertMatch } from "$std/assert/mod.ts";
import { createId } from "utils/id.ts";

Deno.test("returns a string of length 8 by default", () => {
const id = createId();
assertEquals(id.length, 8);
});

Deno.test("returns a string of custom length when specified", () => {
const id = createId(12);
assertEquals(id.length, 12);
});

Deno.test("returns a string containing letters and numbers", () => {
const id = createId();
assertMatch(id, /^[a-zA-Z0-9]+$/);
});
109 changes: 109 additions & 0 deletions tests/routes_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { createHandler, ServeHandlerInfo } from "$fresh/server.ts";
import manifest from "../fresh.gen.ts";
import config from "../fresh.config.ts";
import { assertEquals, assertExists } from "$std/assert/mod.ts";

const CONNECTION_INFO: ServeHandlerInfo = {
remoteAddr: {
hostname: "127.0.0.1",
port: 53496,
transport: "tcp",
},
};

const handler = await createHandler(manifest, config);

Deno.test("GET /", async () => {
const response = await handler(
new Request("http://127.0.0.1/"),
CONNECTION_INFO,
);

assertEquals(response.status, 200);
});

Deno.test("POST /", async () => {
const formData = new FormData();
formData.append("content", "Hello, Deno!");

const response = await handler(
new Request("http://127.0.0.1/", { method: "POST", body: formData }),
CONNECTION_INFO,
);

assertEquals(response.status, 302);
assertExists(response.headers.get("Location"));
});

Deno.test("POST / invalid request", async () => {
const formData = new FormData();
formData.append("content", new Blob([JSON.stringify({ hello: "deno" })]));

const response = await handler(
new Request("http://127.0.0.1/", { method: "POST", body: formData }),
CONNECTION_INFO,
);

assertEquals(response.status, 400);
const body = await response.text();
assertEquals(body, "invalid request");
});

Deno.test("POST / request body too large", async () => {
const formData = new FormData();
const paste = await new Blob(["deno".repeat(64 * 1024)]).text();
formData.append("content", paste);

const response = await handler(
new Request("http://127.0.0.1/", { method: "POST", body: formData }),
CONNECTION_INFO,
);

assertEquals(response.status, 400);
const body = await response.text();
assertEquals(body, "paste contents cannot exceed 64 KiB");
});

Deno.test("POST / error", async () => {
const response = await handler(
new Request("http://127.0.0.1/", { method: "POST" }),
CONNECTION_INFO,
);

assertEquals(response.status, 500);
const body = await response.text();
assertEquals(body, "server error");
});

Deno.test("GET /:id", async () => {
const formData = new FormData();
formData.append("content", "Hello, Deno!");

let response = await handler(
new Request("http://127.0.0.1/", { method: "POST", body: formData }),
CONNECTION_INFO,
);

const id = response.headers.get("Location")?.split("/").pop();
assertExists(id);

response = await handler(
new Request(`http://127.0.0.1/${id}`),
CONNECTION_INFO,
);

assertEquals(response.status, 200);
const body = await response.text();
assertEquals(body, "Hello, Deno!");
});

Deno.test("GET /:id not found", async () => {
const response = await handler(
new Request(`http://127.0.0.1/1234`),
CONNECTION_INFO,
);

assertEquals(response.status, 404);
const body = await response.text();
assertEquals(body, "paste not found");
});

0 comments on commit 0b5d89e

Please sign in to comment.