一键导入
write-tests
Generate comprehensive unit tests for React/TypeScript files using bun test runner with proper mocking, coverage targets, and organized test structure.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Generate comprehensive unit tests for React/TypeScript files using bun test runner with proper mocking, coverage targets, and organized test structure.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Analyze a project or workspace to identify repeated patterns and propose abstractions for improved code reuse and maintainability.
Review codebase changes and create well-structured conventional commits with Shortcut ticket links. Use when asked to commit, save changes, or prepare code for review.
Run linting checks and fix all issues idiomatically. Never disables rules or suppresses errors.
Migrate React components from Snowflake SQL queries to NQL using the useNqlQuery hook. Handles query design, case sensitivity, and React dependency optimization.
Create a new PR for the current branch or update an existing one. Follows team PR best practices for titles, descriptions, and checklists.
Analyze and improve a React/TypeScript code file following component composition, hooks, performance, and file organization best practices.
| name | write-tests |
| description | Generate comprehensive unit tests for React/TypeScript files using bun test runner with proper mocking, coverage targets, and organized test structure. |
| argument-hint | [file-or-files] |
You are tasked with creating comprehensive unit tests for the following file(s): $ARGUMENTS
bun test)tests folderAnalyze the target file(s) to understand:
Check existing test coverage (if applicable):
bun test --coverage
Document the current coverage percentage for the file(s).
[workspace]/tests/[relative-path-to-source]/[filename].test.ts(x).test.ts for .ts files and .test.tsx for .tsx filesimport { describe, test, expect, beforeEach, afterEach, mock } from "bun:test";
// Import the module/component to test
import { /* items to test */ } from "../path/to/source/file";
describe("[ModuleName/ComponentName]", () => {
// Setup and teardown if needed
beforeEach(() => {
// Reset mocks, initialize test data
});
afterEach(() => {
// Cleanup
});
describe("[FunctionName/MethodName]", () => {
test("should [expected behavior] when [condition]", () => {
// Arrange
// Act
// Assert
});
test("should handle [edge case]", () => {
// Test edge cases
});
test("should throw error when [invalid condition]", () => {
// Test error scenarios
});
});
});
// Mock external modules
mock.module("../api/client", () => ({
fetchData: mock(() => Promise.resolve({ data: "mocked" }))
}));
// Mock timers if needed
import { useFakeTimers } from "bun:test";
Create the test file in the appropriate location
Write tests following the guidelines above
Run tests to ensure they pass:
bun test [test-file-path]
Check coverage after implementation:
bun test --coverage
Document coverage improvement and any uncovered lines
Provide:
// tests/components/UserProfile.test.tsx
import { describe, test, expect, mock } from "bun:test";
import { render, screen, fireEvent } from "@testing-library/react";
import { UserProfile } from "../../src/components/UserProfile";
import { userService } from "../../src/services/userService";
// Mock the service
mock.module("../../src/services/userService", () => ({
userService: {
getUser: mock(),
updateUser: mock()
}
}));
describe("UserProfile", () => {
const mockUser = {
id: "123",
name: "John Doe",
email: "john@example.com"
};
beforeEach(() => {
mock.restore();
});
describe("rendering", () => {
test("should display user information when data is loaded", async () => {
userService.getUser.mockResolvedValue(mockUser);
render(<UserProfile userId="123" />);
expect(await screen.findByText("John Doe")).toBeInTheDocument();
expect(screen.getByText("john@example.com")).toBeInTheDocument();
});
test("should show loading state initially", () => {
userService.getUser.mockResolvedValue(mockUser);
render(<UserProfile userId="123" />);
expect(screen.getByText("Loading...")).toBeInTheDocument();
});
test("should display error message when user fetch fails", async () => {
userService.getUser.mockRejectedValue(new Error("Network error"));
render(<UserProfile userId="123" />);
expect(await screen.findByText(/error/i)).toBeInTheDocument();
});
});
describe("interactions", () => {
test("should call updateUser when save button is clicked", async () => {
userService.getUser.mockResolvedValue(mockUser);
userService.updateUser.mockResolvedValue({ ...mockUser, name: "Jane Doe" });
render(<UserProfile userId="123" />);
const input = await screen.findByLabelText("Name");
fireEvent.change(input, { target: { value: "Jane Doe" } });
const saveButton = screen.getByRole("button", { name: "Save" });
fireEvent.click(saveButton);
expect(userService.updateUser).toHaveBeenCalledWith("123", {
name: "Jane Doe"
});
});
});
});
Remember to think critically about what the code SHOULD do, not just what it currently does. Consider the function/component names, parameters, return types, and any documentation to infer the intended behavior.