Skip to content

Commit

Permalink
Merge pull request #10 from sor4chi/feat/add-tests
Browse files Browse the repository at this point in the history
Feat/add-tests
  • Loading branch information
sor4chi authored Sep 27, 2023
2 parents d95d82d + fabb516 commit 825f0c2
Show file tree
Hide file tree
Showing 12 changed files with 781 additions and 6 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"eslint-plugin-unused-imports": "^3.0.0",
"prettier": "^3.0.3",
"turbo": "^1.10.13",
"typescript": "^5.2.2"
"typescript": "^5.2.2",
"vitest": "^0.34.5",
"wrangler": "^3.7.0"
}
}
1 change: 1 addition & 0 deletions packages/hono-do/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.wrangler
3 changes: 2 additions & 1 deletion packages/hono-do/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"scripts": {
"lint": "eslint --fix --ext .ts,.tsx src",
"format": "prettier --write \"src/**/*.{ts,tsx}\"",
"build": "tsc"
"build": "tsc -p tsconfig.build.json",
"test": "vitest"
},
"keywords": [
"hono",
Expand Down
24 changes: 24 additions & 0 deletions packages/hono-do/tests/fixtures/delay-init/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Hono } from "hono";

import { generateHonoObject } from "../../../src";

const app = new Hono<{
Bindings: {
DELAY_INIT: DurableObjectNamespace;
};
}>();

app.all("/delay-init/*", (c) => {
const id = c.env.DELAY_INIT.idFromName("delay-init");
const obj = c.env.DELAY_INIT.get(id);
return obj.fetch(c.req.raw);
});

export const DelayInit = generateHonoObject("/delay-init", async (app) => {
let count = 0;
app.get("/count", (c) => c.text(String(count)));
await new Promise((resolve) => setTimeout(resolve, 1000));
count = 1;
});

export default app;
9 changes: 9 additions & 0 deletions packages/hono-do/tests/fixtures/delay-init/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name = "hono-do-test"
compatibility_date = "2023-01-01"

[durable_objects]
bindings = [{ name = "DELAY_INIT", class_name = "DelayInit" }]

[[migrations]]
tag = "v1"
new_classes = ["DelayInit"]
21 changes: 21 additions & 0 deletions packages/hono-do/tests/fixtures/simple/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Hono } from "hono";

import { generateHonoObject } from "../../../src";

const app = new Hono<{
Bindings: {
SIMPLE: DurableObjectNamespace;
};
}>();

app.all("/simple/*", (c) => {
const id = c.env.SIMPLE.idFromName("simple");
const obj = c.env.SIMPLE.get(id);
return obj.fetch(c.req.raw);
});

export const Simple = generateHonoObject("/simple", async (app) => {
app.get("/", (c) => c.text("Hello, Hono DO!"));
});

export default app;
9 changes: 9 additions & 0 deletions packages/hono-do/tests/fixtures/simple/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name = "hono-do-test"
compatibility_date = "2023-01-01"

[durable_objects]
bindings = [{ name = "SIMPLE", class_name = "Simple" }]

[[migrations]]
tag = "v1"
new_classes = ["Simple"]
75 changes: 75 additions & 0 deletions packages/hono-do/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { join } from "path";

import { unstable_dev } from "wrangler";

import { generateHonoObject } from "../src";

import type { UnstableDevWorker } from "wrangler";

describe("generateHonoObject", () => {
it("return type should be satisified Durable Object interface", async () => {
const DO = generateHonoObject("/", () => {});
expect(DO.prototype.fetch).toBeDefined();
});

it("should work generics type", async () => {
generateHonoObject<{
Variables: {
HOGE_VAR: string;
};
Bindings: {
HOGE_DB: D1Database;
};
}>("/", (app) => {
app.get("/hoge", async (c) => {
expectTypeOf(c.var.HOGE_VAR).toEqualTypeOf<string>();
expectTypeOf(c.env.HOGE_DB).toEqualTypeOf<D1Database>();
});
});
});
});

describe("Worker", () => {
describe("Simple", () => {
let worker: UnstableDevWorker;

beforeAll(async () => {
worker = await unstable_dev(join(__dirname, "fixtures/simple/index.ts"), {
experimental: { disableExperimentalWarning: true },
});
});

afterAll(async () => {
await worker.stop();
});

it("should work with base path", async () => {
const resp = await worker.fetch("/simple");
expect(resp.status).toBe(200);
expect(await resp.text()).toBe("Hello, Hono DO!");
});
});

describe("DelayInit", () => {
let worker: UnstableDevWorker;

beforeEach(async () => {
worker = await unstable_dev(
join(__dirname, "fixtures/delay-init/index.ts"),
{
experimental: { disableExperimentalWarning: true },
},
);
});

afterEach(async () => {
await worker.stop();
});

it("should wait for initialization", async () => {
const resp = await worker.fetch("/delay-init/count");
expect(resp.status).toBe(200);
expect(await resp.text()).toBe("1");
});
});
});
11 changes: 11 additions & 0 deletions packages/hono-do/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"baseUrl": "./src",
"types": ["@cloudflare/workers-types"],
"declaration": true
},
"include": ["./src/**/*.ts"],
"exclude": ["./tests/**/*.ts"]
}
7 changes: 3 additions & 4 deletions packages/hono-do/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src",
"baseUrl": "./src",
"types": ["@cloudflare/workers-types"],
"declaration": true,
}
"types": ["@cloudflare/workers-types", "vitest/globals"]
},
"include": ["./src/**/*.ts", "./tests/**/*.ts"]
}
8 changes: 8 additions & 0 deletions packages/hono-do/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference types="vitest" />
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
globals: true,
},
});
Loading

0 comments on commit 825f0c2

Please sign in to comment.