コンテンツにスキップ

Durable Objectsを使ったテスト

Durable Objectsのテストを書く。

import { unstable_dev } from "wrangler"
import type { UnstableDevWorker } from "wrangler"
import { describe, expect, it, beforeAll, afterAll } from "vitest"
describe("Worker", () => {
let worker: UnstableDevWorker
beforeAll(async () => {
worker = await unstable_dev("src/index.ts", {
experimental: { disableExperimentalWarning: true },
});
});
afterAll(async () => {
await worker.stop()
})
it("短いパスのリクエストを拒否するべき", async () => {
const cases = {
failures: ["/", "/foo", "/foo/", "/%2F"],
}
for (const path of cases.failures) {
const resp = await worker.fetch(`http://example.com${path}`)
if (resp) {
const text = await resp.text()
expect(text).toMatchInlineSnapshot('"path must be at least 5 characters"')
}
}
})
describe("durable object", () => {
it("POSTからGETにテキストを送信するべき", async () => {
const path = "/stuff1"
const url = `http://example.com${path}`
// GETリクエストはPOSTリクエストの完了を待つべき
const getResponsePromise = worker.fetch(url)
// 同じパスへのPOSTリクエストは、テキストが消費されたという応答を受け取るべき
const postResponse = await worker.fetch(url, { method: "POST", body: "Hello World 12345" })
expect(postResponse.status).toBe(200)
const postText = await postResponse.text()
expect(postText).toBe("The text was consumed!")
// GETリクエストは今、テキストを受け取るべき
const getResponse = await getResponsePromise
expect(getResponse.status).toBe(200)
const text = await getResponse.text()
expect(text).toBe("Hello World 12345")
})
it("異なるGETにテキストを送信しないべき", async () => {
const path1 = "/stuff1"
const path2 = "/stuff2"
const url = (p: string) => `http://example.com${p}`
// GETリクエストはPOSTリクエストの完了を待つべき
const getResponsePromise1 = worker.fetch(url(path1))
const getResponsePromise2 = worker.fetch(url(path2))
// 同じパスへのPOSTリクエストは、テキストが消費されたという応答を受け取るべき
const postResponse1 = await worker.fetch(url(path1), { method: "POST", body: "Hello World 12345" })
expect(postResponse1.status).toBe(200)
const postText1 = await postResponse1.text()
expect(postText1).toBe("The text was consumed!")
const postResponse2 = await worker.fetch(url(path2), { method: "POST", body: "Hello World 789" })
expect(postResponse2.status).toBe(200)
const postText2 = await postResponse2.text()
expect(postText2).toBe("The text was consumed!")
// GETリクエストは今、テキストを受け取るべき
const getResponse1 = await getResponsePromise1
expect(getResponse1.status).toBe(200)
const text1 = await getResponse1.text()
expect(text1).toBe("Hello World 12345")
const getResponse2 = await getResponsePromise2
expect(getResponse2.status).toBe(200)
const text2 = await getResponse2.text()
expect(text2).toBe("Hello World 789")
})
it("同じPOSTを二度送信しないべき", async () => {
const path = "/stuff1"
const url = (p: string) => `http://example.com${p}`
// GETリクエストはPOSTリクエストの完了を待つべき
const getResponsePromise1 = worker.fetch(url(path))
// 同じパスへのPOSTリクエストは、テキストが消費されたという応答を受け取るべき
const postResponse1 = await worker.fetch(url(path), { method: "POST", body: "Hello World 12345" })
expect(postResponse1.status).toBe(200)
const postText1 = await postResponse1.text()
expect(postText1).toBe("The text was consumed!")
// GETリクエストは今、テキストを受け取るべき
const getResponse1 = await getResponsePromise1
expect(getResponse1.status).toBe(200)
const text1 = await getResponse1.text()
expect(text1).toBe("Hello World 12345")
// 次のGETリクエストは次のPOSTリクエストの完了を待つべき
const getResponsePromise2 = worker.fetch(url(path))
// 異なるテキストで新しいPOSTを送信する
const postResponse2 = await worker.fetch(url(path), { method: "POST", body: "Hello World 789" })
expect(postResponse2.status).toBe(200)
const postText2 = await postResponse2.text()
expect(postText2).toBe("The text was consumed!")
// GETリクエストは新しいテキストを受け取るべき、古いテキストではない
const getResponse2 = await getResponsePromise2
expect(getResponse2.status).toBe(200)
const text2 = await getResponse2.text()
expect(text2).toBe("Hello World 789")
})
})
})

Find the full code for this example on GitHub.