Implementar Testes de Unidade para Alunos (Tarefa 02)#98
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Warning Rate limit exceeded
To continue reviewing without waiting, purchase usage credits in the billing tab. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
WalkthroughA new Vitest-based test file is added that validates CRUD actions for alunos (students). The suite mocks Prisma, authentication, revalidation, and error tracking dependencies to test successful and unauthorized create, update, and delete operations. ChangesAlunos Actions Test Suite
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
src/lib/actions/alunos.test.ts (2)
1-1: ⚡ Quick winRemove broad
anysuppression and type mocks explicitly.The file-level disable plus repeated
as anyweakens strict TS guarantees in tests. Prefer typed mock helpers/casts scoped to each mock instead of disabling the rule globally.As per coding guidelines, "Use TypeScript 5 in strict mode" and "All code changes must pass `npm run lint` (ESLint quality gate) before merging".Proposed typed approach
-/* eslint-disable `@typescript-eslint/no-explicit-any` */ import { describe, it, expect, vi, beforeEach } from 'vitest'; +import type { MockedFunction } from 'vitest'; ... beforeEach(() => { vi.clearAllMocks(); - vi.mocked(getUser).mockResolvedValue(mockAdmin as any); + (getUser as MockedFunction<typeof getUser>).mockResolvedValue(mockAdmin); }); ... - vi.mocked(prisma.aluno.create).mockResolvedValue(mockCreatedAluno as any); + vi.mocked(prisma.aluno.create).mockResolvedValue(mockCreatedAluno); ... - vi.mocked(getUser).mockResolvedValue({ user: null, error: null } as any); - const result = await createAlunoAction({} as any); + (getUser as MockedFunction<typeof getUser>).mockResolvedValue({ user: null, error: null }); + const result = await createAlunoAction({ + nomeCompleto: '', + cpf: '', + email: '', + telefone: '', + statusMatricula: 'ATIVA', + }); ... - vi.mocked(prisma.aluno.update).mockResolvedValue(mockUpdatedAluno as any); + vi.mocked(prisma.aluno.update).mockResolvedValue(mockUpdatedAluno);Also applies to: 39-39, 62-62, 73-74, 93-93
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/lib/actions/alunos.test.ts` at line 1, Remove the file-level eslint disable at the top of src/lib/actions/alunos.test.ts and replace each broad "as any" mock with properly typed test mocks scoped to that usage (e.g., use jest.Mocked<YourServiceType>, typed helper functions like createMock<T>(), or "as unknown as ExpectedType" per case) where the test currently casts dependencies; update the specific mock instances (the occurrences flagged in the comment) to use those typed helpers/casts so ESLint no-explicit-any is not needed globally and each mock preserves correct types for the functions under test.
80-117: ⚡ Quick winAdd unauthorized tests for
updateAlunoActionanddeleteAlunoAction.Both actions enforce the same auth guard as create; adding those two negative-path tests will complete CRUD auth coverage and prevent regressions.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/lib/actions/alunos.test.ts` around lines 80 - 117, Add negative-path tests that assert updateAlunoAction and deleteAlunoAction return an unauthorized failure and do not call the DB or revalidation when the auth guard denies access: in the alunos.test.ts suite, create tests for updateAlunoAction(validId, updateData) and deleteAlunoAction(validId) where you mock the auth/session to be unauthenticated (same mechanism used in create tests), assert result.success is false (or the expected unauthorized shape), assert prisma.aluno.update / prisma.aluno.delete were not called, and assert revalidatePath was not called for delete; use the existing symbols updateAlunoAction, deleteAlunoAction, prisma.aluno.update, prisma.aluno.delete, and revalidatePath to locate the code to mock and assert against.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@src/lib/actions/alunos.test.ts`:
- Line 1: Remove the file-level eslint disable at the top of
src/lib/actions/alunos.test.ts and replace each broad "as any" mock with
properly typed test mocks scoped to that usage (e.g., use
jest.Mocked<YourServiceType>, typed helper functions like createMock<T>(), or
"as unknown as ExpectedType" per case) where the test currently casts
dependencies; update the specific mock instances (the occurrences flagged in the
comment) to use those typed helpers/casts so ESLint no-explicit-any is not
needed globally and each mock preserves correct types for the functions under
test.
- Around line 80-117: Add negative-path tests that assert updateAlunoAction and
deleteAlunoAction return an unauthorized failure and do not call the DB or
revalidation when the auth guard denies access: in the alunos.test.ts suite,
create tests for updateAlunoAction(validId, updateData) and
deleteAlunoAction(validId) where you mock the auth/session to be unauthenticated
(same mechanism used in create tests), assert result.success is false (or the
expected unauthorized shape), assert prisma.aluno.update / prisma.aluno.delete
were not called, and assert revalidatePath was not called for delete; use the
existing symbols updateAlunoAction, deleteAlunoAction, prisma.aluno.update,
prisma.aluno.delete, and revalidatePath to locate the code to mock and assert
against.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: b6a5369a-0836-4273-8843-78b57d9d2a87
📒 Files selected for processing (1)
src/lib/actions/alunos.test.ts
Testes de unidade para CRUD de alunos. Resolve #97
Summary by CodeRabbit