-
Notifications
You must be signed in to change notification settings - Fork 0
🧪 [testing improvement] Add error handling test for /api/dashboard/summary #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
is0692vs
wants to merge
1
commit into
main
from
test-dashboard-summary-error-handling-7950621441683495241
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,138 @@ | ||
| import { describe, expect, it, vi, beforeEach } from "vitest"; | ||
| import { getServerSession } from "next-auth"; | ||
| import { fetchUserSummary } from "@/lib/github"; | ||
| import { fetchViewerLogin } from "@/lib/githubViewer"; | ||
|
|
||
| vi.mock("next-auth", () => ({ | ||
| getServerSession: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/github", () => ({ | ||
| fetchUserSummary: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/githubViewer", () => ({ | ||
| fetchViewerLogin: vi.fn(), | ||
| })); | ||
|
|
||
| describe("GET /api/dashboard/summary", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it("returns 401 if no session exists", async () => { | ||
| vi.mocked(getServerSession).mockResolvedValueOnce(null); | ||
|
|
||
| const { GET } = await import("./route"); | ||
| const response = await GET(); | ||
| const data = await response.json(); | ||
|
|
||
| expect(response.status).toBe(401); | ||
| expect(data.error).toBe("Unauthorized"); | ||
| }); | ||
|
|
||
| it("returns 401 if no access token exists", async () => { | ||
| vi.mocked(getServerSession).mockResolvedValueOnce({ user: { login: "testuser" } }); | ||
|
|
||
| const { GET } = await import("./route"); | ||
| const response = await GET(); | ||
| const data = await response.json(); | ||
|
|
||
| expect(response.status).toBe(401); | ||
| expect(data.error).toBe("Unauthorized"); | ||
| }); | ||
|
|
||
| it("returns 200 and summary if session has login", async () => { | ||
| const mockSession = { | ||
| accessToken: "fake-token", | ||
| user: { login: "testuser" }, | ||
| }; | ||
| const mockSummary = { profile: { login: "testuser" } }; | ||
|
|
||
| vi.mocked(getServerSession).mockResolvedValueOnce(mockSession); | ||
| vi.mocked(fetchUserSummary).mockResolvedValueOnce(mockSummary as any); | ||
|
|
||
| const { GET } = await import("./route"); | ||
| const response = await GET(); | ||
| const data = await response.json(); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(data.username).toBe("testuser"); | ||
| expect(data.summary).toEqual(mockSummary); | ||
| expect(fetchViewerLogin).not.toHaveBeenCalled(); | ||
| expect(fetchUserSummary).toHaveBeenCalledWith("testuser", "fake-token"); | ||
| }); | ||
|
|
||
| it("returns 200 and fetches login if missing from session", async () => { | ||
| const mockSession = { | ||
| accessToken: "fake-token", | ||
| user: { name: "Test User" }, // login missing | ||
| }; | ||
| const mockSummary = { profile: { login: "testuser" } }; | ||
|
|
||
| vi.mocked(getServerSession).mockResolvedValueOnce(mockSession); | ||
| vi.mocked(fetchViewerLogin).mockResolvedValueOnce("testuser"); | ||
| vi.mocked(fetchUserSummary).mockResolvedValueOnce(mockSummary as any); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| const { GET } = await import("./route"); | ||
| const response = await GET(); | ||
| const data = await response.json(); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(data.username).toBe("testuser"); | ||
| expect(data.summary).toEqual(mockSummary); | ||
| expect(fetchViewerLogin).toHaveBeenCalledWith("fake-token"); | ||
| expect(fetchUserSummary).toHaveBeenCalledWith("testuser", "fake-token"); | ||
| }); | ||
|
|
||
| it("returns 500 if fetchViewerLogin fails", async () => { | ||
| const mockSession = { | ||
| accessToken: "fake-token", | ||
| user: { name: "Test User" }, // login missing | ||
| }; | ||
|
|
||
| vi.mocked(getServerSession).mockResolvedValueOnce(mockSession); | ||
| vi.mocked(fetchViewerLogin).mockRejectedValueOnce(new Error("Viewer login failed")); | ||
|
|
||
| const { GET } = await import("./route"); | ||
| const response = await GET(); | ||
| const data = await response.json(); | ||
|
|
||
| expect(response.status).toBe(500); | ||
| expect(data.error).toBe("Viewer login failed"); | ||
| }); | ||
|
|
||
| it("returns 500 if fetchUserSummary fails", async () => { | ||
| const mockSession = { | ||
| accessToken: "fake-token", | ||
| user: { login: "testuser" }, | ||
| }; | ||
|
|
||
| vi.mocked(getServerSession).mockResolvedValueOnce(mockSession); | ||
| vi.mocked(fetchUserSummary).mockRejectedValueOnce(new Error("Summary fetch failed")); | ||
|
|
||
| const { GET } = await import("./route"); | ||
| const response = await GET(); | ||
| const data = await response.json(); | ||
|
|
||
| expect(response.status).toBe(500); | ||
| expect(data.error).toBe("Summary fetch failed"); | ||
| }); | ||
|
|
||
| it("returns 500 with 'Unknown error' if error is not an Error instance", async () => { | ||
| const mockSession = { | ||
| accessToken: "fake-token", | ||
| user: { login: "testuser" }, | ||
| }; | ||
|
|
||
| vi.mocked(getServerSession).mockResolvedValueOnce(mockSession); | ||
| vi.mocked(fetchUserSummary).mockRejectedValueOnce("Something went wrong"); | ||
|
|
||
| const { GET } = await import("./route"); | ||
| const response = await GET(); | ||
| const data = await response.json(); | ||
|
|
||
| expect(response.status).toBe(500); | ||
| expect(data.error).toBe("Unknown error"); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as anyを使用すると、TypeScriptの型チェックが機能しなくなり、テストが将来の変更に対して脆弱になる可能性があります。UserSummaryの型定義が変更されても、このキャストが原因でコンパイル時エラーとして検出されず、意図しない動作につながる恐れがあります。より堅牢にするために、UserSummary型を満たすモックデータを作成するか、テストデータ生成用のファクトリ関数を導入することをお勧めします。