-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from sor4chi/feat/add-tests
Feat/add-tests
- Loading branch information
Showing
12 changed files
with
781 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.wrangler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}); |
Oops, something went wrong.