From 042bb84e6792165ac84e38eabc78b2112548b912 Mon Sep 17 00:00:00 2001 From: 5ouma <101255979+5ouma@users.noreply.github.com> Date: Mon, 17 Jun 2024 20:34:35 +0900 Subject: [PATCH] test(step): Use step from `Deno.TestContext` It makes the result clean and readable. --- test/libs/content_test.ts | 41 +++++++++++++++++++------------------- test/libs/env_test.ts | 28 ++++++++++++++------------ test/libs/redirect_test.ts | 35 ++++++++++++++++---------------- test/router_test.ts | 34 ++++++++++++++++--------------- 4 files changed, 72 insertions(+), 66 deletions(-) diff --git a/test/libs/content_test.ts b/test/libs/content_test.ts index c7bdaba..9eca30d 100644 --- a/test/libs/content_test.ts +++ b/test/libs/content_test.ts @@ -4,30 +4,31 @@ import { STATUS_CODE } from "@std/http/status"; import { getContent } from "../../src/libs/content.ts"; import { exportRepo, testRef, testRepo, unknownRepo } from "../utils.ts"; -Deno.test("Get Content", async () => { +Deno.test("Get Content", async (t: Deno.TestContext) => { const ctx: RouterContext = testing.createMockContext(); - exportRepo(testRepo); - await getContent(ctx); - assertEquals(ctx.response.status, STATUS_CODE.OK); -}); + await t.step("normal", async () => { + exportRepo(testRepo); + await getContent(ctx); -Deno.test("Get Content (With ref)", async () => { - const ctx: RouterContext = testing.createMockContext(); - exportRepo(testRepo); - await getContent(ctx, testRef); + assertEquals(ctx.response.status, STATUS_CODE.OK); + }); - assertEquals(ctx.response.status, STATUS_CODE.OK); -}); + await t.step("with ref", async () => { + exportRepo(testRepo); + await getContent(ctx, testRef); -Deno.test("Get Content (Not found)", async () => { - const ctx: RouterContext = testing.createMockContext(); - exportRepo(unknownRepo); - await getContent(ctx); + assertEquals(ctx.response.status, STATUS_CODE.OK); + }); + + await t.step("not found", async () => { + exportRepo(unknownRepo); + await getContent(ctx); - assertEquals(ctx.response.status, STATUS_CODE.NotFound); - assertStringIncludes( - ctx.response.body!.toString(), - `⚠️ ${STATUS_CODE.NotFound}:`, - ); + assertEquals(ctx.response.status, STATUS_CODE.NotFound); + assertStringIncludes( + ctx.response.body!.toString(), + `⚠️ ${STATUS_CODE.NotFound}:`, + ); + }); }); diff --git a/test/libs/env_test.ts b/test/libs/env_test.ts index 448ead6..28d4775 100644 --- a/test/libs/env_test.ts +++ b/test/libs/env_test.ts @@ -2,20 +2,22 @@ import { assertEquals } from "@std/assert"; import { getRepository } from "../../src/libs/env.ts"; import { clearRepo, exportRepo, testRepo } from "../utils.ts"; -Deno.test("Get Repository Env", () => { - exportRepo(testRepo); - const repository = getRepository(); +Deno.test("Get Repository Env", async (t: Deno.TestContext) => { + await t.step("Normal", () => { + exportRepo(testRepo); + const repository = getRepository(); - assertEquals(repository.owner, testRepo.owner); - assertEquals(repository.name, testRepo.name); - assertEquals(repository.path, testRepo.path); -}); + assertEquals(repository.owner, testRepo.owner); + assertEquals(repository.name, testRepo.name); + assertEquals(repository.path, testRepo.path); + }); -Deno.test("Get Repository Env (Not set)", () => { - clearRepo(); - const repository = getRepository(); + await t.step("Not set", () => { + clearRepo(); + const repository = getRepository(); - assertEquals(repository.owner, ""); - assertEquals(repository.name, ""); - assertEquals(repository.path, ""); + assertEquals(repository.owner, ""); + assertEquals(repository.name, ""); + assertEquals(repository.path, ""); + }); }); diff --git a/test/libs/redirect_test.ts b/test/libs/redirect_test.ts index 12f9480..156c5e9 100644 --- a/test/libs/redirect_test.ts +++ b/test/libs/redirect_test.ts @@ -5,26 +5,27 @@ import { UserAgent } from "@std/http/user-agent"; import { redirect } from "../../src/libs/redirect.ts"; import { exportRepo, testRef, testRepo } from "../utils.ts"; -Deno.test("Redirect Detection", () => { +Deno.test("Redirect Detection", async (t: Deno.TestContext) => { const ctx: RouterContext = testing.createMockContext(); exportRepo(testRepo); - redirect(ctx, new UserAgent("Chrome/1.2.3")); - assertEquals(ctx.response.status, STATUS_CODE.PermanentRedirect); - assertEquals( - ctx.response.headers.get("location"), - `https://github.com/${testRepo.owner}/${testRepo.name}/blob/master/${testRepo.path}`, - ); -}); + await t.step("normal", () => { + redirect(ctx, new UserAgent("Chrome/1.2.3")); -Deno.test("Redirect Detection (With ref)", () => { - const ctx: RouterContext = testing.createMockContext(); - exportRepo(testRepo); - redirect(ctx, new UserAgent("Chrome/1.2.3"), testRef); + assertEquals(ctx.response.status, STATUS_CODE.PermanentRedirect); + assertEquals( + ctx.response.headers.get("location"), + `https://github.com/${testRepo.owner}/${testRepo.name}/blob/master/${testRepo.path}`, + ); + }); + + await t.step("with ref", () => { + redirect(ctx, new UserAgent("Chrome/1.2.3"), testRef); - assertEquals(ctx.response.status, STATUS_CODE.PermanentRedirect); - assertEquals( - ctx.response.headers.get("location"), - `https://github.com/${testRepo.owner}/${testRepo.name}/blob/${testRef}/${testRepo.path}`, - ); + assertEquals(ctx.response.status, STATUS_CODE.PermanentRedirect); + assertEquals( + ctx.response.headers.get("location"), + `https://github.com/${testRepo.owner}/${testRepo.name}/blob/${testRef}/${testRepo.path}`, + ); + }); }); diff --git a/test/router_test.ts b/test/router_test.ts index d520a21..59409db 100644 --- a/test/router_test.ts +++ b/test/router_test.ts @@ -4,24 +4,26 @@ import { STATUS_CODE } from "@std/http/status"; import { router } from "../src/router.ts"; import { exportRepo, testRef, testRepo } from "./utils.ts"; -Deno.test("Serve (/)", async () => { - const ctx: RouterContext = testing.createMockContext({ - method: "GET", - path: "/", +Deno.test("Serve", async (t: Deno.TestContext) => { + await t.step("/", async () => { + const ctx: RouterContext = testing.createMockContext({ + method: "GET", + path: "/", + }); + exportRepo(testRepo); + await router.routes()(ctx, () => Promise.resolve()); + + assertEquals(ctx.response.status, STATUS_CODE.OK); }); - exportRepo(testRepo); - await router.routes()(ctx, () => Promise.resolve()); - assertEquals(ctx.response.status, STATUS_CODE.OK); -}); + await t.step("/:ref", async () => { + const ctx: RouterContext = testing.createMockContext({ + method: "GET", + path: `/${testRef}`, + }); + exportRepo(testRepo); + await router.routes()(ctx, () => Promise.resolve()); -Deno.test("Serve (/:ref)", async () => { - const ctx: RouterContext = testing.createMockContext({ - method: "GET", - path: `/${testRef}`, + assertEquals(ctx.response.status, STATUS_CODE.OK); }); - exportRepo(testRepo); - await router.routes()(ctx, () => Promise.resolve()); - - assertEquals(ctx.response.status, STATUS_CODE.OK); });