Skip to main content
Glama
unit-test.template.ts2.95 kB
/** * Unit Test Template * * This template demonstrates the structure and best practices for unit tests. * * TDD Workflow: * 1. RED: Write this test first (it will fail) * 2. GREEN: Implement minimal code to make it pass * 3. REFACTOR: Improve code while keeping tests green * * Copy this template and replace: * - ComponentName with your component name * - methodName with your method name * - Test descriptions with actual test cases */ import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; // Import the component to test // import { ComponentName } from '../../path/to/component'; describe("ComponentName", () => { // Test setup - runs before each test let component: unknown; // Replace with actual type beforeEach(() => { // Initialize component with default state // component = new ComponentName(); }); afterEach(() => { // Cleanup after each test // Reset mocks, clear state, etc. vi.clearAllMocks(); }); describe("methodName", () => { it("should handle normal case", () => { // Arrange: Set up test data const input = "test input"; const expected = "expected output"; // Act: Execute the method // const result = component.methodName(input); // Assert: Verify the result // expect(result).toBe(expected); // Placeholder assertion expect(true).toBe(true); }); it("should handle edge case with empty input", () => { // Arrange const input = ""; // Act & Assert // expect(() => component.methodName(input)).toThrow(); // Placeholder assertion expect(true).toBe(true); }); it("should handle edge case with null input", () => { // Arrange const input = null; // Act & Assert // expect(() => component.methodName(input)).toThrow('Input cannot be null'); // Placeholder assertion expect(true).toBe(true); }); it("should handle edge case with invalid input", () => { // Arrange const input = "invalid"; // Act & Assert // expect(() => component.methodName(input)).toThrow(); // Placeholder assertion expect(true).toBe(true); }); }); describe("anotherMethod", () => { it("should return correct value", () => { // Arrange const input = 42; const expected = 84; // Act // const result = component.anotherMethod(input); // Assert // expect(result).toBe(expected); // Placeholder assertion expect(true).toBe(true); }); }); }); /** * Best Practices: * * 1. One assertion per test (when possible) * 2. Clear test names that describe what is being tested * 3. Follow Arrange-Act-Assert pattern * 4. Test edge cases and error conditions * 5. Use meaningful variable names * 6. Keep tests independent (no shared state) * 7. Mock external dependencies * 8. Test behavior, not implementation */

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/keyurgolani/ThoughtMcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server