com um clique
write-api-tests
// Write unit tests for API integration code. Required for all changes to src/api/
// Write unit tests for API integration code. Required for all changes to src/api/
| name | write-api-tests |
| description | Write unit tests for API integration code. Required for all changes to src/api/ |
All changes to src/api/ MUST include unit tests. This is mandatory, not optional.
When you add or modify API integration code (functions in src/api/siteadmin.ts or src/api/client.ts), you must write corresponding tests.
npm run test # Run all tests
npm run test -- --watch # Watch mode during development
npm run test -- path/to/file.test.ts # Run specific test file
__tests__ foldersrc/api/__tests__/client.test.tssrc/api/__tests__/siteadmin.test.tsUse these mocks for API tests:
jest.mock("@authgear/web");
jest.mock("../../config", () => ({
SITEADMIN_API_URL: "https://api.example.com",
}));
const mockAuthgear = authgear as jest.Mocked<typeof authgear>;
The @authgear/web mock is already in src/__mocks__/@authgear/web.ts.
For each API function, write tests covering:
SiteAdminAPIErrorimport { apiRequest, SiteAdminAPIError } from "../client";
jest.mock("@authgear/web");
jest.mock("../../config", () => ({
SITEADMIN_API_URL: "https://api.example.com",
}));
const mockAuthgear = authgear as jest.Mocked<typeof authgear>;
describe("getApp", () => {
beforeEach(() => {
jest.clearAllMocks();
});
it("should fetch app by ID with URL encoding", async () => {
const mockData = {
id: "app-123",
owner_email: "owner@example.com",
plan: "pro",
created_at: "2024-01-01T00:00:00Z",
last_month_mau: 1000,
user_count: 5000,
};
(mockAuthgear.fetch as jest.Mock).mockResolvedValue({
ok: true,
json: jest.fn().mockResolvedValue(mockData),
});
const result = await apiRequest("/api/v1/apps/app-123");
expect(result).toEqual(mockData);
expect(mockAuthgear.fetch).toHaveBeenCalledWith(
"https://api.example.com/api/v1/apps/app-123",
undefined
);
});
it("should throw SiteAdminAPIError on 404", async () => {
(mockAuthgear.fetch as jest.Mock).mockResolvedValue({
ok: false,
json: jest.fn().mockResolvedValue({
error: {
name: "AppNotFound",
reason: "app_not_found",
message: "App not found.",
code: 404,
tracking_id: "track-123",
},
}),
});
await expect(apiRequest("/api/v1/apps/invalid")).rejects.toThrow(
SiteAdminAPIError
);
});
});
src/api/__tests__/client.test.ts — 8 tests for base apiRequest() functionsrc/api/__tests__/siteadmin.test.ts — 25 tests for all siteadmin API functionsSee these files for real-world examples covering URL encoding, query parameters, POST bodies, error handling, etc.
Tests run in the CI pipeline:
make ci # Runs: typecheck → lint → format:check → test
All tests must pass before code can be merged.
jest.config.js — Jest setup with ts-jest and jsdomsrc/setupTests.ts — jest-dom matchers initializationsrc/__mocks__/@authgear/web.ts — authgear module mock# Run all tests
npm run test
# Watch mode (rerun on file changes)
npm run test -- --watch
# Run specific test file
npm run test -- src/api/__tests__/client.test.ts
# Run with coverage
npm run test -- --coverage
Update vulnerable npm dependencies safely. Only applies non-breaking changes. Prefers lock-file-only fixes via `npm audit fix`. Generates a summary of what was updated, what was skipped, and why. Use when asked to fix security vulnerabilities, run dependabot-style updates, or update vulnerable packages.
Wire a frontend page in the Authgear Site Admin Portal to the real Site Admin API, replacing mock data from src/data/. Use this skill when asked to connect a page to the API, replace dummy/mock data with live data, implement an API call on a screen, or fetch real data for any page in this project.