feat(tests): add unit tests for accounts and transactions APIs

- Updated package.json to include Vitest for testing and added necessary devDependencies.
- Created accounts.test.ts to test the accounts API endpoints for listing and retrieving accounts.
- Implemented setup.ts to reset test data before each test run.
- Developed transactions.test.ts to cover creating, updating, and deleting transactions through the API.
- Added vitest.config.ts for configuring Vitest with appropriate settings and coverage options.
This commit is contained in:
GitHub Copilot
2025-04-24 08:52:48 -04:00
parent bb6bd75434
commit 99b70b519b
6 changed files with 2726 additions and 2 deletions

View File

@@ -0,0 +1,128 @@
import { describe, it, expect } from "vitest";
import { POST as createTransaction } from "../pages/api/transactions/index";
import {
PUT as updateTransaction,
DELETE as deleteTransaction,
} from "../pages/api/transactions/[id]/index";
import { accounts, transactions } from "../data/store";
describe("Transactions API", () => {
describe("POST /api/transactions", () => {
it("should create a new transaction", async () => {
const initialBalance = accounts[0].balance;
const newTransaction = {
accountId: "1",
date: "2025-04-24",
description: "Test New Transaction",
amount: -25.0,
};
const response = await createTransaction({
request: new Request("http://localhost:4321/api/transactions", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(newTransaction),
}),
});
const result = await response.json();
expect(response.status).toBe(201);
expect(result).toHaveProperty("id");
expect(result.description).toBe(newTransaction.description);
expect(accounts[0].balance).toBe(initialBalance + newTransaction.amount);
});
it("should reject transaction with missing fields", async () => {
const invalidTransaction = {
accountId: "1",
// Missing required fields
};
const response = await createTransaction({
request: new Request("http://localhost:4321/api/transactions", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(invalidTransaction),
}),
});
const error = await response.json();
expect(response.status).toBe(400);
expect(error).toHaveProperty("error", "Missing required fields");
});
});
describe("PUT /api/transactions/:id", () => {
it("should update an existing transaction", async () => {
const initialBalance = accounts[0].balance;
const originalAmount = transactions[0].amount;
const updates = {
description: "Updated Description",
amount: -75.0,
};
const response = await updateTransaction({
params: { id: "1" },
request: new Request("http://localhost:4321/api/transactions/1", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(updates),
}),
});
const result = await response.json();
expect(response.status).toBe(200);
expect(result.description).toBe(updates.description);
expect(result.amount).toBe(updates.amount);
expect(accounts[0].balance).toBe(
initialBalance - originalAmount + updates.amount
);
});
it("should reject update for non-existent transaction", async () => {
const response = await updateTransaction({
params: { id: "999" },
request: new Request("http://localhost:4321/api/transactions/999", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ description: "Test" }),
}),
});
const error = await response.json();
expect(response.status).toBe(404);
expect(error).toHaveProperty("error", "Transaction not found");
});
});
describe("DELETE /api/transactions/:id", () => {
it("should delete a transaction", async () => {
const initialBalance = accounts[0].balance;
const transactionAmount = transactions[0].amount;
const initialCount = transactions.length;
const response = await deleteTransaction({
params: { id: "1" },
});
expect(response.status).toBe(204);
expect(transactions).toHaveLength(initialCount - 1);
expect(accounts[0].balance).toBe(initialBalance - transactionAmount);
});
it("should return 404 for non-existent transaction", async () => {
const response = await deleteTransaction({
params: { id: "999" },
});
const error = await response.json();
expect(response.status).toBe(404);
expect(error).toHaveProperty("error", "Transaction not found");
});
});
});