ndomo 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.bun-version +1 -0
- package/.dockerignore +79 -0
- package/.editorconfig +18 -0
- package/.env.example +19 -0
- package/.github/CODEOWNERS +8 -0
- package/.github/ISSUE_TEMPLATE/bug_report.yml +62 -0
- package/.github/ISSUE_TEMPLATE/config.yml +2 -0
- package/.github/ISSUE_TEMPLATE/feature_request.yml +34 -0
- package/.github/dependabot.yml +36 -0
- package/.github/pull_request_template.md +24 -0
- package/.github/release.yml +30 -0
- package/.github/workflows/gitleaks.yml +28 -0
- package/.github/workflows/release-please.yml +27 -0
- package/.github/workflows/smoke.yml +29 -0
- package/.husky/commit-msg +1 -0
- package/CHANGELOG.md +114 -0
- package/Dockerfile +32 -0
- package/README.es.md +174 -0
- package/README.md +187 -0
- package/agents/chronicler.md +98 -0
- package/agents/ci-smith.md +136 -0
- package/agents/craftsman.md +341 -0
- package/agents/deploy-smith.md +138 -0
- package/agents/foreman.md +377 -0
- package/agents/go-smith.md +164 -0
- package/agents/guild.md +188 -0
- package/agents/inspector.md +83 -0
- package/agents/js-smith.md +127 -0
- package/agents/ops-scout.md +173 -0
- package/agents/painter.md +200 -0
- package/agents/python-smith.md +120 -0
- package/agents/ranger.md +307 -0
- package/agents/release-smith.md +165 -0
- package/agents/rust-smith.md +159 -0
- package/agents/sage.md +178 -0
- package/agents/scout.md +144 -0
- package/agents/scribe.md +156 -0
- package/agents/smith.md +201 -0
- package/agents/vue-smith.md +155 -0
- package/agents/warden.md +216 -0
- package/agents/zig-smith.md +156 -0
- package/bin/ndomo-analyses.ts +4 -0
- package/bin/ndomo-status.ts +4 -0
- package/biome.json +57 -0
- package/bun.lock +514 -0
- package/commitlint.config.js +3 -0
- package/config/ndomo.config.json +258 -0
- package/config/ndomo.schema.json +166 -0
- package/docs/agents.md +375 -0
- package/docs/bugs/plan-create-orphan-fk.md +131 -0
- package/docs/bugs/task_create_batch-order-index-collision.md +158 -0
- package/docs/configuration.md +276 -0
- package/docs/database.md +364 -0
- package/docs/features/feature-flexible-builder-v1.md +724 -0
- package/docs/features/feature-flexible-builder-v2.md +882 -0
- package/docs/features/feature-flexible-builder.md +974 -0
- package/docs/http-server.md +244 -0
- package/docs/installation.md +259 -0
- package/docs/integrations.md +129 -0
- package/docs/operations/anti-pattern-sub-agent-verify-2026-06-21.md +32 -0
- package/docs/operations/audit-v1.md +417 -0
- package/docs/operations/audit-v2.md +197 -0
- package/docs/operations/audit-v3.md +306 -0
- package/docs/operations/db-optimize-foundations.md +123 -0
- package/docs/operations/verify-gate-architecture.md +82 -0
- package/docs/workflows.md +448 -0
- package/opencode.json +5 -0
- package/package.json +65 -0
- package/release-please-config.json +11 -0
- package/scripts/dev-bust-cache.sh +164 -0
- package/scripts/install.sh +688 -0
- package/scripts/smoke-e2e.ts +704 -0
- package/scripts/smoke-hot.ts +417 -0
- package/scripts/smoke-http.sh +228 -0
- package/scripts/smoke-v4.ts +256 -0
- package/scripts/smoke-v5.ts +397 -0
- package/scripts/smoke.sh +9 -0
- package/scripts/uninstall.sh +224 -0
- package/skills/api-security-best-practices/SKILL.md +915 -0
- package/skills/bash-scripting/SKILL.md +201 -0
- package/skills/bun/SKILL.md +313 -0
- package/skills/cavecrew/SKILL.md +82 -0
- package/skills/caveman/SKILL.md +74 -0
- package/skills/caveman-review/README.md +33 -0
- package/skills/caveman-review/SKILL.md +55 -0
- package/skills/find-skills/SKILL.md +142 -0
- package/skills/frontend-design/LICENSE.txt +177 -0
- package/skills/frontend-design/SKILL.md +55 -0
- package/skills/golang-patterns/SKILL.md +674 -0
- package/skills/golang-security/SKILL.md +185 -0
- package/skills/golang-security/evals/evals.json +595 -0
- package/skills/golang-security/references/architecture.md +268 -0
- package/skills/golang-security/references/checklist.md +80 -0
- package/skills/golang-security/references/cookies.md +200 -0
- package/skills/golang-security/references/cryptography.md +424 -0
- package/skills/golang-security/references/filesystem.md +285 -0
- package/skills/golang-security/references/injection.md +315 -0
- package/skills/golang-security/references/logging.md +163 -0
- package/skills/golang-security/references/memory-safety.md +241 -0
- package/skills/golang-security/references/network.md +253 -0
- package/skills/golang-security/references/secrets.md +189 -0
- package/skills/golang-security/references/third-party.md +159 -0
- package/skills/golang-security/references/threat-modeling.md +189 -0
- package/skills/golang-testing/SKILL.md +720 -0
- package/skills/grill-me/SKILL.md +7 -0
- package/skills/javascript-testing-patterns/SKILL.md +537 -0
- package/skills/javascript-testing-patterns/references/advanced-testing-patterns.md +513 -0
- package/skills/modern-javascript-patterns/SKILL.md +43 -0
- package/skills/modern-javascript-patterns/references/advanced-patterns.md +487 -0
- package/skills/modern-javascript-patterns/references/details.md +457 -0
- package/skills/python-anti-patterns/SKILL.md +349 -0
- package/skills/python-design-patterns/SKILL.md +85 -0
- package/skills/python-design-patterns/references/details.md +353 -0
- package/skills/python-error-handling/SKILL.md +193 -0
- package/skills/python-error-handling/references/details.md +171 -0
- package/skills/python-testing-patterns/SKILL.md +278 -0
- package/skills/python-testing-patterns/references/advanced-patterns.md +411 -0
- package/skills/python-testing-patterns/references/details.md +349 -0
- package/skills/rust-patterns/SKILL.md +500 -0
- package/skills/rust-testing/SKILL.md +501 -0
- package/skills/security-review/SKILL.md +504 -0
- package/skills/security-review/cloud-infrastructure-security.md +361 -0
- package/skills/vue-best-practices/SKILL.md +154 -0
- package/skills/vue-best-practices/references/animation-class-based-technique.md +254 -0
- package/skills/vue-best-practices/references/animation-state-driven-technique.md +291 -0
- package/skills/vue-best-practices/references/component-async.md +97 -0
- package/skills/vue-best-practices/references/component-data-flow.md +307 -0
- package/skills/vue-best-practices/references/component-fallthrough-attrs.md +174 -0
- package/skills/vue-best-practices/references/component-keep-alive.md +137 -0
- package/skills/vue-best-practices/references/component-slots.md +216 -0
- package/skills/vue-best-practices/references/component-suspense.md +228 -0
- package/skills/vue-best-practices/references/component-teleport.md +108 -0
- package/skills/vue-best-practices/references/component-transition-group.md +128 -0
- package/skills/vue-best-practices/references/component-transition.md +125 -0
- package/skills/vue-best-practices/references/composables.md +290 -0
- package/skills/vue-best-practices/references/directives.md +162 -0
- package/skills/vue-best-practices/references/perf-avoid-component-abstraction-in-lists.md +159 -0
- package/skills/vue-best-practices/references/perf-v-once-v-memo-directives.md +182 -0
- package/skills/vue-best-practices/references/perf-virtualize-large-lists.md +187 -0
- package/skills/vue-best-practices/references/plugins.md +166 -0
- package/skills/vue-best-practices/references/reactivity.md +344 -0
- package/skills/vue-best-practices/references/render-functions.md +201 -0
- package/skills/vue-best-practices/references/sfc.md +310 -0
- package/skills/vue-best-practices/references/state-management.md +135 -0
- package/skills/vue-best-practices/references/updated-hook-performance.md +187 -0
- package/skills/vue-pinia-best-practices/SKILL.md +21 -0
- package/skills/vue-pinia-best-practices/reference/pinia-no-active-pinia-error.md +248 -0
- package/skills/vue-pinia-best-practices/reference/pinia-setup-store-return-all-state.md +227 -0
- package/skills/vue-pinia-best-practices/reference/pinia-store-destructuring-breaks-reactivity.md +193 -0
- package/skills/vue-pinia-best-practices/reference/state-url-for-ephemeral-filters.md +238 -0
- package/skills/vue-pinia-best-practices/reference/state-use-pinia-for-large-apps.md +262 -0
- package/skills/vue-pinia-best-practices/reference/store-method-binding-parentheses.md +191 -0
- package/skills/zig-0.16/SKILL.md +840 -0
- package/skills/zig-0.16/scripts/check-zig-version.sh +21 -0
- package/src/cli/analyses.ts +280 -0
- package/src/cli/index.ts +108 -0
- package/src/cli/serve.ts +192 -0
- package/src/cli/smoke.ts +131 -0
- package/src/cli/status.test.ts +204 -0
- package/src/cli/status.ts +263 -0
- package/src/cli/vacuum.test.ts +82 -0
- package/src/cli/vacuum.ts +96 -0
- package/src/config/schema.test.ts +88 -0
- package/src/config/schema.ts +64 -0
- package/src/db/analyses-migration.test.ts +210 -0
- package/src/db/analyses.test.ts +466 -0
- package/src/db/analyses.ts +375 -0
- package/src/db/auto-checkpoint.ts +131 -0
- package/src/db/client.test.ts +129 -0
- package/src/db/client.ts +55 -0
- package/src/db/fts-escape.ts +20 -0
- package/src/db/incidents.test.ts +201 -0
- package/src/db/incidents.ts +93 -0
- package/src/db/index.ts +86 -0
- package/src/db/migrations-v13.test.ts +141 -0
- package/src/db/migrations-v8.test.ts +301 -0
- package/src/db/migrations.ts +147 -0
- package/src/db/plan-archive.test.ts +180 -0
- package/src/db/plan-archive.ts +274 -0
- package/src/db/plan-create.test.ts +276 -0
- package/src/db/plan-create.ts +78 -0
- package/src/db/plan-files.test.ts +289 -0
- package/src/db/plan-update-status.ts +287 -0
- package/src/db/plans.test.ts +490 -0
- package/src/db/plans.ts +534 -0
- package/src/db/resolve-project-dir.test.ts +143 -0
- package/src/db/resolve-project-dir.ts +75 -0
- package/src/db/rollbacks.test.ts +150 -0
- package/src/db/rollbacks.ts +67 -0
- package/src/db/schema.ts +907 -0
- package/src/db/sessions.test.ts +80 -0
- package/src/db/sessions.ts +135 -0
- package/src/db/shutdown.test.ts +147 -0
- package/src/db/shutdown.ts +45 -0
- package/src/db/tasks.test.ts +921 -0
- package/src/db/tasks.ts +747 -0
- package/src/db/types.ts +619 -0
- package/src/http/__tests__/auth.test.ts +196 -0
- package/src/http/__tests__/routes.test.ts +465 -0
- package/src/http/__tests__/sse.test.ts +317 -0
- package/src/http/auth.ts +72 -0
- package/src/http/middleware/cors.ts +53 -0
- package/src/http/middleware/security-headers.ts +21 -0
- package/src/http/routes/events.ts +112 -0
- package/src/http/routes/health.ts +51 -0
- package/src/http/routes/plans.ts +66 -0
- package/src/http/routes/sessions.ts +50 -0
- package/src/http/routes/tasks.ts +60 -0
- package/src/http/server.ts +95 -0
- package/src/http/sse.ts +116 -0
- package/src/index.ts +37 -0
- package/src/lib.ts +65 -0
- package/src/mem/scoped.ts +65 -0
- package/src/orchestrator/background.test.ts +268 -0
- package/src/orchestrator/background.ts +293 -0
- package/src/orchestrator/memory-hook.ts +182 -0
- package/src/orchestrator/reconciler.ts +123 -0
- package/src/orchestrator/scheduler.test.ts +300 -0
- package/src/orchestrator/scheduler.ts +243 -0
- package/src/plugin.test.ts +2574 -0
- package/src/plugin.ts +1690 -0
- package/src/sdk/client.ts +66 -0
- package/src/worktrees/manager.ts +236 -0
- package/src/worktrees/state.ts +87 -0
- package/tests/integration/ranger-flow.test.ts +257 -0
- package/tools/analysis_archive.ts +28 -0
- package/tools/analysis_create.ts +55 -0
- package/tools/analysis_get.ts +33 -0
- package/tools/analysis_link_plan.ts +44 -0
- package/tools/analysis_list.ts +48 -0
- package/tools/analysis_search.ts +36 -0
- package/tools/analysis_update.ts +44 -0
- package/tools/plan_approve.ts +31 -0
- package/tools/plan_create.ts +58 -0
- package/tools/plan_get.ts +40 -0
- package/tools/plan_list.ts +37 -0
- package/tools/plan_search.ts +34 -0
- package/tools/plan_update_status.ts +71 -0
- package/tools/session_checkpoint.ts +31 -0
- package/tools/session_end.ts +26 -0
- package/tools/session_start.ts +43 -0
- package/tools/task_create_batch.ts +70 -0
- package/tools/task_list.ts +35 -0
- package/tools/task_next_for_agent.ts +30 -0
- package/tools/task_search.ts +34 -0
- package/tools/task_update_status.ts +37 -0
- package/tsconfig.json +31 -0
|
@@ -0,0 +1,537 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: javascript-testing-patterns
|
|
3
|
+
description: Implement comprehensive testing strategies using Jest, Vitest, and Testing Library for unit tests, integration tests, and end-to-end testing with mocking, fixtures, and test-driven development. Use when writing JavaScript/TypeScript tests, setting up test infrastructure, or implementing TDD/BDD workflows.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# JavaScript Testing Patterns
|
|
7
|
+
|
|
8
|
+
Comprehensive guide for implementing robust testing strategies in JavaScript/TypeScript applications using modern testing frameworks and best practices.
|
|
9
|
+
|
|
10
|
+
## When to Use This Skill
|
|
11
|
+
|
|
12
|
+
- Setting up test infrastructure for new projects
|
|
13
|
+
- Writing unit tests for functions and classes
|
|
14
|
+
- Creating integration tests for APIs and services
|
|
15
|
+
- Implementing end-to-end tests for user flows
|
|
16
|
+
- Mocking external dependencies and APIs
|
|
17
|
+
- Testing React, Vue, or other frontend components
|
|
18
|
+
- Implementing test-driven development (TDD)
|
|
19
|
+
- Setting up continuous testing in CI/CD pipelines
|
|
20
|
+
|
|
21
|
+
## Testing Frameworks
|
|
22
|
+
|
|
23
|
+
### Jest - Full-Featured Testing Framework
|
|
24
|
+
|
|
25
|
+
**Setup:**
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
// jest.config.ts
|
|
29
|
+
import type { Config } from "jest";
|
|
30
|
+
|
|
31
|
+
const config: Config = {
|
|
32
|
+
preset: "ts-jest",
|
|
33
|
+
testEnvironment: "node",
|
|
34
|
+
roots: ["<rootDir>/src"],
|
|
35
|
+
testMatch: ["**/__tests__/**/*.ts", "**/?(*.)+(spec|test).ts"],
|
|
36
|
+
collectCoverageFrom: [
|
|
37
|
+
"src/**/*.ts",
|
|
38
|
+
"!src/**/*.d.ts",
|
|
39
|
+
"!src/**/*.interface.ts",
|
|
40
|
+
],
|
|
41
|
+
coverageThreshold: {
|
|
42
|
+
global: {
|
|
43
|
+
branches: 80,
|
|
44
|
+
functions: 80,
|
|
45
|
+
lines: 80,
|
|
46
|
+
statements: 80,
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
setupFilesAfterEnv: ["<rootDir>/src/test/setup.ts"],
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export default config;
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Vitest - Fast, Vite-Native Testing
|
|
56
|
+
|
|
57
|
+
**Setup:**
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
// vitest.config.ts
|
|
61
|
+
import { defineConfig } from "vitest/config";
|
|
62
|
+
|
|
63
|
+
export default defineConfig({
|
|
64
|
+
test: {
|
|
65
|
+
globals: true,
|
|
66
|
+
environment: "node",
|
|
67
|
+
coverage: {
|
|
68
|
+
provider: "v8",
|
|
69
|
+
reporter: ["text", "json", "html"],
|
|
70
|
+
exclude: ["**/*.d.ts", "**/*.config.ts", "**/dist/**"],
|
|
71
|
+
},
|
|
72
|
+
setupFiles: ["./src/test/setup.ts"],
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Unit Testing Patterns
|
|
78
|
+
|
|
79
|
+
### Pattern 1: Testing Pure Functions
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
// utils/calculator.ts
|
|
83
|
+
export function add(a: number, b: number): number {
|
|
84
|
+
return a + b;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function divide(a: number, b: number): number {
|
|
88
|
+
if (b === 0) {
|
|
89
|
+
throw new Error("Division by zero");
|
|
90
|
+
}
|
|
91
|
+
return a / b;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// utils/calculator.test.ts
|
|
95
|
+
import { describe, it, expect } from "vitest";
|
|
96
|
+
import { add, divide } from "./calculator";
|
|
97
|
+
|
|
98
|
+
describe("Calculator", () => {
|
|
99
|
+
describe("add", () => {
|
|
100
|
+
it("should add two positive numbers", () => {
|
|
101
|
+
expect(add(2, 3)).toBe(5);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("should add negative numbers", () => {
|
|
105
|
+
expect(add(-2, -3)).toBe(-5);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("should handle zero", () => {
|
|
109
|
+
expect(add(0, 5)).toBe(5);
|
|
110
|
+
expect(add(5, 0)).toBe(5);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
describe("divide", () => {
|
|
115
|
+
it("should divide two numbers", () => {
|
|
116
|
+
expect(divide(10, 2)).toBe(5);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("should handle decimal results", () => {
|
|
120
|
+
expect(divide(5, 2)).toBe(2.5);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("should throw error when dividing by zero", () => {
|
|
124
|
+
expect(() => divide(10, 0)).toThrow("Division by zero");
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Pattern 2: Testing Classes
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
// services/user.service.ts
|
|
134
|
+
export class UserService {
|
|
135
|
+
private users: Map<string, User> = new Map();
|
|
136
|
+
|
|
137
|
+
create(user: User): User {
|
|
138
|
+
if (this.users.has(user.id)) {
|
|
139
|
+
throw new Error("User already exists");
|
|
140
|
+
}
|
|
141
|
+
this.users.set(user.id, user);
|
|
142
|
+
return user;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
findById(id: string): User | undefined {
|
|
146
|
+
return this.users.get(id);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
update(id: string, updates: Partial<User>): User {
|
|
150
|
+
const user = this.users.get(id);
|
|
151
|
+
if (!user) {
|
|
152
|
+
throw new Error("User not found");
|
|
153
|
+
}
|
|
154
|
+
const updated = { ...user, ...updates };
|
|
155
|
+
this.users.set(id, updated);
|
|
156
|
+
return updated;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
delete(id: string): boolean {
|
|
160
|
+
return this.users.delete(id);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// services/user.service.test.ts
|
|
165
|
+
import { describe, it, expect, beforeEach } from "vitest";
|
|
166
|
+
import { UserService } from "./user.service";
|
|
167
|
+
|
|
168
|
+
describe("UserService", () => {
|
|
169
|
+
let service: UserService;
|
|
170
|
+
|
|
171
|
+
beforeEach(() => {
|
|
172
|
+
service = new UserService();
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
describe("create", () => {
|
|
176
|
+
it("should create a new user", () => {
|
|
177
|
+
const user = { id: "1", name: "John", email: "john@example.com" };
|
|
178
|
+
const created = service.create(user);
|
|
179
|
+
|
|
180
|
+
expect(created).toEqual(user);
|
|
181
|
+
expect(service.findById("1")).toEqual(user);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it("should throw error if user already exists", () => {
|
|
185
|
+
const user = { id: "1", name: "John", email: "john@example.com" };
|
|
186
|
+
service.create(user);
|
|
187
|
+
|
|
188
|
+
expect(() => service.create(user)).toThrow("User already exists");
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
describe("update", () => {
|
|
193
|
+
it("should update existing user", () => {
|
|
194
|
+
const user = { id: "1", name: "John", email: "john@example.com" };
|
|
195
|
+
service.create(user);
|
|
196
|
+
|
|
197
|
+
const updated = service.update("1", { name: "Jane" });
|
|
198
|
+
|
|
199
|
+
expect(updated.name).toBe("Jane");
|
|
200
|
+
expect(updated.email).toBe("john@example.com");
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("should throw error if user not found", () => {
|
|
204
|
+
expect(() => service.update("999", { name: "Jane" })).toThrow(
|
|
205
|
+
"User not found",
|
|
206
|
+
);
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### Pattern 3: Testing Async Functions
|
|
213
|
+
|
|
214
|
+
```typescript
|
|
215
|
+
// services/api.service.ts
|
|
216
|
+
export class ApiService {
|
|
217
|
+
async fetchUser(id: string): Promise<User> {
|
|
218
|
+
const response = await fetch(`https://api.example.com/users/${id}`);
|
|
219
|
+
if (!response.ok) {
|
|
220
|
+
throw new Error("User not found");
|
|
221
|
+
}
|
|
222
|
+
return response.json();
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
async createUser(user: CreateUserDTO): Promise<User> {
|
|
226
|
+
const response = await fetch("https://api.example.com/users", {
|
|
227
|
+
method: "POST",
|
|
228
|
+
headers: { "Content-Type": "application/json" },
|
|
229
|
+
body: JSON.stringify(user),
|
|
230
|
+
});
|
|
231
|
+
return response.json();
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// services/api.service.test.ts
|
|
236
|
+
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
237
|
+
import { ApiService } from "./api.service";
|
|
238
|
+
|
|
239
|
+
// Mock fetch globally
|
|
240
|
+
global.fetch = vi.fn();
|
|
241
|
+
|
|
242
|
+
describe("ApiService", () => {
|
|
243
|
+
let service: ApiService;
|
|
244
|
+
|
|
245
|
+
beforeEach(() => {
|
|
246
|
+
service = new ApiService();
|
|
247
|
+
vi.clearAllMocks();
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
describe("fetchUser", () => {
|
|
251
|
+
it("should fetch user successfully", async () => {
|
|
252
|
+
const mockUser = { id: "1", name: "John", email: "john@example.com" };
|
|
253
|
+
|
|
254
|
+
(fetch as any).mockResolvedValueOnce({
|
|
255
|
+
ok: true,
|
|
256
|
+
json: async () => mockUser,
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
const user = await service.fetchUser("1");
|
|
260
|
+
|
|
261
|
+
expect(user).toEqual(mockUser);
|
|
262
|
+
expect(fetch).toHaveBeenCalledWith("https://api.example.com/users/1");
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it("should throw error if user not found", async () => {
|
|
266
|
+
(fetch as any).mockResolvedValueOnce({
|
|
267
|
+
ok: false,
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
await expect(service.fetchUser("999")).rejects.toThrow("User not found");
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
describe("createUser", () => {
|
|
275
|
+
it("should create user successfully", async () => {
|
|
276
|
+
const newUser = { name: "John", email: "john@example.com" };
|
|
277
|
+
const createdUser = { id: "1", ...newUser };
|
|
278
|
+
|
|
279
|
+
(fetch as any).mockResolvedValueOnce({
|
|
280
|
+
ok: true,
|
|
281
|
+
json: async () => createdUser,
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
const user = await service.createUser(newUser);
|
|
285
|
+
|
|
286
|
+
expect(user).toEqual(createdUser);
|
|
287
|
+
expect(fetch).toHaveBeenCalledWith(
|
|
288
|
+
"https://api.example.com/users",
|
|
289
|
+
expect.objectContaining({
|
|
290
|
+
method: "POST",
|
|
291
|
+
body: JSON.stringify(newUser),
|
|
292
|
+
}),
|
|
293
|
+
);
|
|
294
|
+
});
|
|
295
|
+
});
|
|
296
|
+
});
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
## Mocking Patterns
|
|
300
|
+
|
|
301
|
+
### Pattern 1: Mocking Modules
|
|
302
|
+
|
|
303
|
+
```typescript
|
|
304
|
+
// services/email.service.ts
|
|
305
|
+
import nodemailer from "nodemailer";
|
|
306
|
+
|
|
307
|
+
export class EmailService {
|
|
308
|
+
private transporter = nodemailer.createTransport({
|
|
309
|
+
host: process.env.SMTP_HOST,
|
|
310
|
+
port: 587,
|
|
311
|
+
auth: {
|
|
312
|
+
user: process.env.SMTP_USER,
|
|
313
|
+
pass: process.env.SMTP_PASS,
|
|
314
|
+
},
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
async sendEmail(to: string, subject: string, html: string) {
|
|
318
|
+
await this.transporter.sendMail({
|
|
319
|
+
from: process.env.EMAIL_FROM,
|
|
320
|
+
to,
|
|
321
|
+
subject,
|
|
322
|
+
html,
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// services/email.service.test.ts
|
|
328
|
+
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
329
|
+
import { EmailService } from "./email.service";
|
|
330
|
+
|
|
331
|
+
vi.mock("nodemailer", () => ({
|
|
332
|
+
default: {
|
|
333
|
+
createTransport: vi.fn(() => ({
|
|
334
|
+
sendMail: vi.fn().mockResolvedValue({ messageId: "123" }),
|
|
335
|
+
})),
|
|
336
|
+
},
|
|
337
|
+
}));
|
|
338
|
+
|
|
339
|
+
describe("EmailService", () => {
|
|
340
|
+
let service: EmailService;
|
|
341
|
+
|
|
342
|
+
beforeEach(() => {
|
|
343
|
+
service = new EmailService();
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
it("should send email successfully", async () => {
|
|
347
|
+
await service.sendEmail(
|
|
348
|
+
"test@example.com",
|
|
349
|
+
"Test Subject",
|
|
350
|
+
"<p>Test Body</p>",
|
|
351
|
+
);
|
|
352
|
+
|
|
353
|
+
expect(service["transporter"].sendMail).toHaveBeenCalledWith(
|
|
354
|
+
expect.objectContaining({
|
|
355
|
+
to: "test@example.com",
|
|
356
|
+
subject: "Test Subject",
|
|
357
|
+
}),
|
|
358
|
+
);
|
|
359
|
+
});
|
|
360
|
+
});
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
### Pattern 2: Dependency Injection for Testing
|
|
364
|
+
|
|
365
|
+
```typescript
|
|
366
|
+
// services/user.service.ts
|
|
367
|
+
export interface IUserRepository {
|
|
368
|
+
findById(id: string): Promise<User | null>;
|
|
369
|
+
create(user: User): Promise<User>;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
export class UserService {
|
|
373
|
+
constructor(private userRepository: IUserRepository) {}
|
|
374
|
+
|
|
375
|
+
async getUser(id: string): Promise<User> {
|
|
376
|
+
const user = await this.userRepository.findById(id);
|
|
377
|
+
if (!user) {
|
|
378
|
+
throw new Error("User not found");
|
|
379
|
+
}
|
|
380
|
+
return user;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
async createUser(userData: CreateUserDTO): Promise<User> {
|
|
384
|
+
// Business logic here
|
|
385
|
+
const user = { id: generateId(), ...userData };
|
|
386
|
+
return this.userRepository.create(user);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// services/user.service.test.ts
|
|
391
|
+
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
392
|
+
import { UserService, IUserRepository } from "./user.service";
|
|
393
|
+
|
|
394
|
+
describe("UserService", () => {
|
|
395
|
+
let service: UserService;
|
|
396
|
+
let mockRepository: IUserRepository;
|
|
397
|
+
|
|
398
|
+
beforeEach(() => {
|
|
399
|
+
mockRepository = {
|
|
400
|
+
findById: vi.fn(),
|
|
401
|
+
create: vi.fn(),
|
|
402
|
+
};
|
|
403
|
+
service = new UserService(mockRepository);
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
describe("getUser", () => {
|
|
407
|
+
it("should return user if found", async () => {
|
|
408
|
+
const mockUser = { id: "1", name: "John", email: "john@example.com" };
|
|
409
|
+
vi.mocked(mockRepository.findById).mockResolvedValue(mockUser);
|
|
410
|
+
|
|
411
|
+
const user = await service.getUser("1");
|
|
412
|
+
|
|
413
|
+
expect(user).toEqual(mockUser);
|
|
414
|
+
expect(mockRepository.findById).toHaveBeenCalledWith("1");
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
it("should throw error if user not found", async () => {
|
|
418
|
+
vi.mocked(mockRepository.findById).mockResolvedValue(null);
|
|
419
|
+
|
|
420
|
+
await expect(service.getUser("999")).rejects.toThrow("User not found");
|
|
421
|
+
});
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
describe("createUser", () => {
|
|
425
|
+
it("should create user successfully", async () => {
|
|
426
|
+
const userData = { name: "John", email: "john@example.com" };
|
|
427
|
+
const createdUser = { id: "1", ...userData };
|
|
428
|
+
|
|
429
|
+
vi.mocked(mockRepository.create).mockResolvedValue(createdUser);
|
|
430
|
+
|
|
431
|
+
const user = await service.createUser(userData);
|
|
432
|
+
|
|
433
|
+
expect(user).toEqual(createdUser);
|
|
434
|
+
expect(mockRepository.create).toHaveBeenCalled();
|
|
435
|
+
});
|
|
436
|
+
});
|
|
437
|
+
});
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
### Pattern 3: Spying on Functions
|
|
441
|
+
|
|
442
|
+
```typescript
|
|
443
|
+
// utils/logger.ts
|
|
444
|
+
export const logger = {
|
|
445
|
+
info: (message: string) => console.log(`INFO: ${message}`),
|
|
446
|
+
error: (message: string) => console.error(`ERROR: ${message}`),
|
|
447
|
+
};
|
|
448
|
+
|
|
449
|
+
// services/order.service.ts
|
|
450
|
+
import { logger } from "../utils/logger";
|
|
451
|
+
|
|
452
|
+
export class OrderService {
|
|
453
|
+
async processOrder(orderId: string): Promise<void> {
|
|
454
|
+
logger.info(`Processing order ${orderId}`);
|
|
455
|
+
// Process order logic
|
|
456
|
+
logger.info(`Order ${orderId} processed successfully`);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// services/order.service.test.ts
|
|
461
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
462
|
+
import { OrderService } from "./order.service";
|
|
463
|
+
import { logger } from "../utils/logger";
|
|
464
|
+
|
|
465
|
+
describe("OrderService", () => {
|
|
466
|
+
let service: OrderService;
|
|
467
|
+
let loggerSpy: any;
|
|
468
|
+
|
|
469
|
+
beforeEach(() => {
|
|
470
|
+
service = new OrderService();
|
|
471
|
+
loggerSpy = vi.spyOn(logger, "info");
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
afterEach(() => {
|
|
475
|
+
loggerSpy.mockRestore();
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
it("should log order processing", async () => {
|
|
479
|
+
await service.processOrder("123");
|
|
480
|
+
|
|
481
|
+
expect(loggerSpy).toHaveBeenCalledWith("Processing order 123");
|
|
482
|
+
expect(loggerSpy).toHaveBeenCalledWith("Order 123 processed successfully");
|
|
483
|
+
expect(loggerSpy).toHaveBeenCalledTimes(2);
|
|
484
|
+
});
|
|
485
|
+
});
|
|
486
|
+
```
|
|
487
|
+
|
|
488
|
+
## Integration Testing
|
|
489
|
+
|
|
490
|
+
Integration tests verify real database operations and HTTP endpoints using `supertest` and a test database instance. Always truncate tables in `beforeEach` and tear down in `afterAll`.
|
|
491
|
+
|
|
492
|
+
For full API integration test examples (supertest + PostgreSQL) and database repository integration tests, see [references/advanced-testing-patterns.md](references/advanced-testing-patterns.md).
|
|
493
|
+
|
|
494
|
+
## Frontend Testing with Testing Library
|
|
495
|
+
|
|
496
|
+
Test React components by rendering them and querying by role, placeholder, or test ID. Test hooks with `renderHook` + `act`. Prefer semantic queries (`getByRole`, `getByPlaceholderText`) over `data-testid`.
|
|
497
|
+
|
|
498
|
+
For complete React component test examples (UserForm, hooks with `renderHook`/`act`), see [references/advanced-testing-patterns.md](references/advanced-testing-patterns.md).
|
|
499
|
+
|
|
500
|
+
## Test Fixtures and Factories
|
|
501
|
+
|
|
502
|
+
Use `@faker-js/faker` to generate realistic test data factories. Factories accept optional `overrides` so tests can set only the fields they care about:
|
|
503
|
+
|
|
504
|
+
```typescript
|
|
505
|
+
// tests/fixtures/user.fixture.ts
|
|
506
|
+
import { faker } from "@faker-js/faker";
|
|
507
|
+
|
|
508
|
+
export function createUserFixture(overrides?: Partial<User>): User {
|
|
509
|
+
return {
|
|
510
|
+
id: faker.string.uuid(),
|
|
511
|
+
name: faker.person.fullName(),
|
|
512
|
+
email: faker.internet.email(),
|
|
513
|
+
createdAt: faker.date.past(),
|
|
514
|
+
...overrides,
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
```
|
|
518
|
+
|
|
519
|
+
For snapshot testing, coverage configuration, test organization patterns, promise testing, and timer mocking, see [references/advanced-testing-patterns.md](references/advanced-testing-patterns.md).
|
|
520
|
+
|
|
521
|
+
## Best Practices
|
|
522
|
+
|
|
523
|
+
1. **Follow AAA Pattern**: Arrange, Act, Assert
|
|
524
|
+
2. **One assertion per test**: Or logically related assertions
|
|
525
|
+
3. **Descriptive test names**: Should describe what is being tested
|
|
526
|
+
4. **Use beforeEach/afterEach**: For setup and teardown
|
|
527
|
+
5. **Mock external dependencies**: Keep tests isolated
|
|
528
|
+
6. **Test edge cases**: Not just happy paths
|
|
529
|
+
7. **Avoid implementation details**: Test behavior, not implementation
|
|
530
|
+
8. **Use test factories**: For consistent test data
|
|
531
|
+
9. **Keep tests fast**: Mock slow operations
|
|
532
|
+
10. **Write tests first (TDD)**: When possible
|
|
533
|
+
11. **Maintain test coverage**: Aim for 80%+ coverage
|
|
534
|
+
12. **Use TypeScript**: For type-safe tests
|
|
535
|
+
13. **Test error handling**: Not just success cases
|
|
536
|
+
14. **Use data-testid sparingly**: Prefer semantic queries
|
|
537
|
+
15. **Clean up after tests**: Prevent test pollution
|