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,513 @@
|
|
|
1
|
+
# Advanced JavaScript Testing Patterns
|
|
2
|
+
|
|
3
|
+
Advanced patterns for integration testing, frontend component testing, fixtures, snapshot testing, coverage, and common test utilities.
|
|
4
|
+
|
|
5
|
+
## Integration Testing
|
|
6
|
+
|
|
7
|
+
### Pattern 1: API Integration Tests
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
// tests/integration/user.api.test.ts
|
|
11
|
+
import request from "supertest";
|
|
12
|
+
import { app } from "../../src/app";
|
|
13
|
+
import { pool } from "../../src/config/database";
|
|
14
|
+
|
|
15
|
+
describe("User API Integration Tests", () => {
|
|
16
|
+
beforeAll(async () => {
|
|
17
|
+
// Setup test database
|
|
18
|
+
await pool.query("CREATE TABLE IF NOT EXISTS users (...)");
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
afterAll(async () => {
|
|
22
|
+
// Cleanup
|
|
23
|
+
await pool.query("DROP TABLE IF EXISTS users");
|
|
24
|
+
await pool.end();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
beforeEach(async () => {
|
|
28
|
+
// Clear data before each test
|
|
29
|
+
await pool.query("TRUNCATE TABLE users CASCADE");
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe("POST /api/users", () => {
|
|
33
|
+
it("should create a new user", async () => {
|
|
34
|
+
const userData = {
|
|
35
|
+
name: "John Doe",
|
|
36
|
+
email: "john@example.com",
|
|
37
|
+
password: "password123",
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const response = await request(app)
|
|
41
|
+
.post("/api/users")
|
|
42
|
+
.send(userData)
|
|
43
|
+
.expect(201);
|
|
44
|
+
|
|
45
|
+
expect(response.body).toMatchObject({
|
|
46
|
+
name: userData.name,
|
|
47
|
+
email: userData.email,
|
|
48
|
+
});
|
|
49
|
+
expect(response.body).toHaveProperty("id");
|
|
50
|
+
expect(response.body).not.toHaveProperty("password");
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("should return 400 if email is invalid", async () => {
|
|
54
|
+
const userData = {
|
|
55
|
+
name: "John Doe",
|
|
56
|
+
email: "invalid-email",
|
|
57
|
+
password: "password123",
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const response = await request(app)
|
|
61
|
+
.post("/api/users")
|
|
62
|
+
.send(userData)
|
|
63
|
+
.expect(400);
|
|
64
|
+
|
|
65
|
+
expect(response.body).toHaveProperty("error");
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("should return 409 if email already exists", async () => {
|
|
69
|
+
const userData = {
|
|
70
|
+
name: "John Doe",
|
|
71
|
+
email: "john@example.com",
|
|
72
|
+
password: "password123",
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
await request(app).post("/api/users").send(userData);
|
|
76
|
+
|
|
77
|
+
const response = await request(app)
|
|
78
|
+
.post("/api/users")
|
|
79
|
+
.send(userData)
|
|
80
|
+
.expect(409);
|
|
81
|
+
|
|
82
|
+
expect(response.body.error).toContain("already exists");
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
describe("GET /api/users/:id", () => {
|
|
87
|
+
it("should get user by id", async () => {
|
|
88
|
+
const createResponse = await request(app).post("/api/users").send({
|
|
89
|
+
name: "John Doe",
|
|
90
|
+
email: "john@example.com",
|
|
91
|
+
password: "password123",
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
const userId = createResponse.body.id;
|
|
95
|
+
|
|
96
|
+
const response = await request(app)
|
|
97
|
+
.get(`/api/users/${userId}`)
|
|
98
|
+
.expect(200);
|
|
99
|
+
|
|
100
|
+
expect(response.body).toMatchObject({
|
|
101
|
+
id: userId,
|
|
102
|
+
name: "John Doe",
|
|
103
|
+
email: "john@example.com",
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("should return 404 if user not found", async () => {
|
|
108
|
+
await request(app).get("/api/users/999").expect(404);
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
describe("Authentication", () => {
|
|
113
|
+
it("should require authentication for protected routes", async () => {
|
|
114
|
+
await request(app).get("/api/users/me").expect(401);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("should allow access with valid token", async () => {
|
|
118
|
+
// Create user and login
|
|
119
|
+
await request(app).post("/api/users").send({
|
|
120
|
+
name: "John Doe",
|
|
121
|
+
email: "john@example.com",
|
|
122
|
+
password: "password123",
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
const loginResponse = await request(app).post("/api/auth/login").send({
|
|
126
|
+
email: "john@example.com",
|
|
127
|
+
password: "password123",
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
const token = loginResponse.body.token;
|
|
131
|
+
|
|
132
|
+
const response = await request(app)
|
|
133
|
+
.get("/api/users/me")
|
|
134
|
+
.set("Authorization", `Bearer ${token}`)
|
|
135
|
+
.expect(200);
|
|
136
|
+
|
|
137
|
+
expect(response.body.email).toBe("john@example.com");
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Pattern 2: Database Integration Tests
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
// tests/integration/user.repository.test.ts
|
|
147
|
+
import { describe, it, expect, beforeAll, afterAll, beforeEach } from "vitest";
|
|
148
|
+
import { Pool } from "pg";
|
|
149
|
+
import { UserRepository } from "../../src/repositories/user.repository";
|
|
150
|
+
|
|
151
|
+
describe("UserRepository Integration Tests", () => {
|
|
152
|
+
let pool: Pool;
|
|
153
|
+
let repository: UserRepository;
|
|
154
|
+
|
|
155
|
+
beforeAll(async () => {
|
|
156
|
+
pool = new Pool({
|
|
157
|
+
host: "localhost",
|
|
158
|
+
port: 5432,
|
|
159
|
+
database: "test_db",
|
|
160
|
+
user: "test_user",
|
|
161
|
+
password: "test_password",
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
repository = new UserRepository(pool);
|
|
165
|
+
|
|
166
|
+
// Create tables
|
|
167
|
+
await pool.query(`
|
|
168
|
+
CREATE TABLE IF NOT EXISTS users (
|
|
169
|
+
id SERIAL PRIMARY KEY,
|
|
170
|
+
name VARCHAR(255) NOT NULL,
|
|
171
|
+
email VARCHAR(255) UNIQUE NOT NULL,
|
|
172
|
+
password VARCHAR(255) NOT NULL,
|
|
173
|
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
174
|
+
)
|
|
175
|
+
`);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
afterAll(async () => {
|
|
179
|
+
await pool.query("DROP TABLE IF EXISTS users");
|
|
180
|
+
await pool.end();
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
beforeEach(async () => {
|
|
184
|
+
await pool.query("TRUNCATE TABLE users CASCADE");
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("should create a user", async () => {
|
|
188
|
+
const user = await repository.create({
|
|
189
|
+
name: "John Doe",
|
|
190
|
+
email: "john@example.com",
|
|
191
|
+
password: "hashed_password",
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
expect(user).toHaveProperty("id");
|
|
195
|
+
expect(user.name).toBe("John Doe");
|
|
196
|
+
expect(user.email).toBe("john@example.com");
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("should find user by email", async () => {
|
|
200
|
+
await repository.create({
|
|
201
|
+
name: "John Doe",
|
|
202
|
+
email: "john@example.com",
|
|
203
|
+
password: "hashed_password",
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
const user = await repository.findByEmail("john@example.com");
|
|
207
|
+
|
|
208
|
+
expect(user).toBeTruthy();
|
|
209
|
+
expect(user?.name).toBe("John Doe");
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("should return null if user not found", async () => {
|
|
213
|
+
const user = await repository.findByEmail("nonexistent@example.com");
|
|
214
|
+
expect(user).toBeNull();
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## Frontend Testing with Testing Library
|
|
220
|
+
|
|
221
|
+
### Pattern 1: React Component Testing
|
|
222
|
+
|
|
223
|
+
```typescript
|
|
224
|
+
// components/UserForm.tsx
|
|
225
|
+
import { useState } from 'react';
|
|
226
|
+
|
|
227
|
+
interface Props {
|
|
228
|
+
onSubmit: (user: { name: string; email: string }) => void;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export function UserForm({ onSubmit }: Props) {
|
|
232
|
+
const [name, setName] = useState('');
|
|
233
|
+
const [email, setEmail] = useState('');
|
|
234
|
+
|
|
235
|
+
const handleSubmit = (e: React.FormEvent) => {
|
|
236
|
+
e.preventDefault();
|
|
237
|
+
onSubmit({ name, email });
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
return (
|
|
241
|
+
<form onSubmit={handleSubmit}>
|
|
242
|
+
<input
|
|
243
|
+
type="text"
|
|
244
|
+
placeholder="Name"
|
|
245
|
+
value={name}
|
|
246
|
+
onChange={(e) => setName(e.target.value)}
|
|
247
|
+
data-testid="name-input"
|
|
248
|
+
/>
|
|
249
|
+
<input
|
|
250
|
+
type="email"
|
|
251
|
+
placeholder="Email"
|
|
252
|
+
value={email}
|
|
253
|
+
onChange={(e) => setEmail(e.target.value)}
|
|
254
|
+
data-testid="email-input"
|
|
255
|
+
/>
|
|
256
|
+
<button type="submit">Submit</button>
|
|
257
|
+
</form>
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// components/UserForm.test.tsx
|
|
262
|
+
import { render, screen, fireEvent } from '@testing-library/react';
|
|
263
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
264
|
+
import { UserForm } from './UserForm';
|
|
265
|
+
|
|
266
|
+
describe('UserForm', () => {
|
|
267
|
+
it('should render form inputs', () => {
|
|
268
|
+
render(<UserForm onSubmit={vi.fn()} />);
|
|
269
|
+
|
|
270
|
+
expect(screen.getByPlaceholderText('Name')).toBeInTheDocument();
|
|
271
|
+
expect(screen.getByPlaceholderText('Email')).toBeInTheDocument();
|
|
272
|
+
expect(screen.getByRole('button', { name: 'Submit' })).toBeInTheDocument();
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it('should update input values', () => {
|
|
276
|
+
render(<UserForm onSubmit={vi.fn()} />);
|
|
277
|
+
|
|
278
|
+
const nameInput = screen.getByTestId('name-input') as HTMLInputElement;
|
|
279
|
+
const emailInput = screen.getByTestId('email-input') as HTMLInputElement;
|
|
280
|
+
|
|
281
|
+
fireEvent.change(nameInput, { target: { value: 'John Doe' } });
|
|
282
|
+
fireEvent.change(emailInput, { target: { value: 'john@example.com' } });
|
|
283
|
+
|
|
284
|
+
expect(nameInput.value).toBe('John Doe');
|
|
285
|
+
expect(emailInput.value).toBe('john@example.com');
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it('should call onSubmit with form data', () => {
|
|
289
|
+
const onSubmit = vi.fn();
|
|
290
|
+
render(<UserForm onSubmit={onSubmit} />);
|
|
291
|
+
|
|
292
|
+
fireEvent.change(screen.getByTestId('name-input'), {
|
|
293
|
+
target: { value: 'John Doe' },
|
|
294
|
+
});
|
|
295
|
+
fireEvent.change(screen.getByTestId('email-input'), {
|
|
296
|
+
target: { value: 'john@example.com' },
|
|
297
|
+
});
|
|
298
|
+
fireEvent.click(screen.getByRole('button', { name: 'Submit' }));
|
|
299
|
+
|
|
300
|
+
expect(onSubmit).toHaveBeenCalledWith({
|
|
301
|
+
name: 'John Doe',
|
|
302
|
+
email: 'john@example.com',
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
});
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
### Pattern 2: Testing Hooks
|
|
309
|
+
|
|
310
|
+
```typescript
|
|
311
|
+
// hooks/useCounter.ts
|
|
312
|
+
import { useState, useCallback } from "react";
|
|
313
|
+
|
|
314
|
+
export function useCounter(initialValue = 0) {
|
|
315
|
+
const [count, setCount] = useState(initialValue);
|
|
316
|
+
|
|
317
|
+
const increment = useCallback(() => setCount((c) => c + 1), []);
|
|
318
|
+
const decrement = useCallback(() => setCount((c) => c - 1), []);
|
|
319
|
+
const reset = useCallback(() => setCount(initialValue), [initialValue]);
|
|
320
|
+
|
|
321
|
+
return { count, increment, decrement, reset };
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// hooks/useCounter.test.ts
|
|
325
|
+
import { renderHook, act } from "@testing-library/react";
|
|
326
|
+
import { describe, it, expect } from "vitest";
|
|
327
|
+
import { useCounter } from "./useCounter";
|
|
328
|
+
|
|
329
|
+
describe("useCounter", () => {
|
|
330
|
+
it("should initialize with default value", () => {
|
|
331
|
+
const { result } = renderHook(() => useCounter());
|
|
332
|
+
expect(result.current.count).toBe(0);
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
it("should initialize with custom value", () => {
|
|
336
|
+
const { result } = renderHook(() => useCounter(10));
|
|
337
|
+
expect(result.current.count).toBe(10);
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
it("should increment count", () => {
|
|
341
|
+
const { result } = renderHook(() => useCounter());
|
|
342
|
+
|
|
343
|
+
act(() => {
|
|
344
|
+
result.current.increment();
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
expect(result.current.count).toBe(1);
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
it("should decrement count", () => {
|
|
351
|
+
const { result } = renderHook(() => useCounter(5));
|
|
352
|
+
|
|
353
|
+
act(() => {
|
|
354
|
+
result.current.decrement();
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
expect(result.current.count).toBe(4);
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
it("should reset to initial value", () => {
|
|
361
|
+
const { result } = renderHook(() => useCounter(10));
|
|
362
|
+
|
|
363
|
+
act(() => {
|
|
364
|
+
result.current.increment();
|
|
365
|
+
result.current.increment();
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
expect(result.current.count).toBe(12);
|
|
369
|
+
|
|
370
|
+
act(() => {
|
|
371
|
+
result.current.reset();
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
expect(result.current.count).toBe(10);
|
|
375
|
+
});
|
|
376
|
+
});
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
## Test Fixtures and Factories
|
|
380
|
+
|
|
381
|
+
```typescript
|
|
382
|
+
// tests/fixtures/user.fixture.ts
|
|
383
|
+
import { faker } from "@faker-js/faker";
|
|
384
|
+
|
|
385
|
+
export function createUserFixture(overrides?: Partial<User>): User {
|
|
386
|
+
return {
|
|
387
|
+
id: faker.string.uuid(),
|
|
388
|
+
name: faker.person.fullName(),
|
|
389
|
+
email: faker.internet.email(),
|
|
390
|
+
createdAt: faker.date.past(),
|
|
391
|
+
...overrides,
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
export function createUsersFixture(count: number): User[] {
|
|
396
|
+
return Array.from({ length: count }, () => createUserFixture());
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// Usage in tests
|
|
400
|
+
import {
|
|
401
|
+
createUserFixture,
|
|
402
|
+
createUsersFixture,
|
|
403
|
+
} from "../fixtures/user.fixture";
|
|
404
|
+
|
|
405
|
+
describe("UserService", () => {
|
|
406
|
+
it("should process user", () => {
|
|
407
|
+
const user = createUserFixture({ name: "John Doe" });
|
|
408
|
+
// Use user in test
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
it("should handle multiple users", () => {
|
|
412
|
+
const users = createUsersFixture(10);
|
|
413
|
+
// Use users in test
|
|
414
|
+
});
|
|
415
|
+
});
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
## Snapshot Testing
|
|
419
|
+
|
|
420
|
+
```typescript
|
|
421
|
+
// components/UserCard.test.tsx
|
|
422
|
+
import { render } from '@testing-library/react';
|
|
423
|
+
import { describe, it, expect } from 'vitest';
|
|
424
|
+
import { UserCard } from './UserCard';
|
|
425
|
+
|
|
426
|
+
describe('UserCard', () => {
|
|
427
|
+
it('should match snapshot', () => {
|
|
428
|
+
const user = {
|
|
429
|
+
id: '1',
|
|
430
|
+
name: 'John Doe',
|
|
431
|
+
email: 'john@example.com',
|
|
432
|
+
avatar: 'https://example.com/avatar.jpg',
|
|
433
|
+
};
|
|
434
|
+
|
|
435
|
+
const { container } = render(<UserCard user={user} />);
|
|
436
|
+
|
|
437
|
+
expect(container.firstChild).toMatchSnapshot();
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
it('should match snapshot with loading state', () => {
|
|
441
|
+
const { container } = render(<UserCard loading />);
|
|
442
|
+
expect(container.firstChild).toMatchSnapshot();
|
|
443
|
+
});
|
|
444
|
+
});
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
## Coverage Reports
|
|
448
|
+
|
|
449
|
+
```typescript
|
|
450
|
+
// package.json
|
|
451
|
+
{
|
|
452
|
+
"scripts": {
|
|
453
|
+
"test": "vitest",
|
|
454
|
+
"test:coverage": "vitest --coverage",
|
|
455
|
+
"test:ui": "vitest --ui"
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
```
|
|
459
|
+
|
|
460
|
+
## Common Patterns
|
|
461
|
+
|
|
462
|
+
### Test Organization
|
|
463
|
+
|
|
464
|
+
```typescript
|
|
465
|
+
describe("UserService", () => {
|
|
466
|
+
describe("createUser", () => {
|
|
467
|
+
it("should create user successfully", () => {});
|
|
468
|
+
it("should throw error if email exists", () => {});
|
|
469
|
+
it("should hash password", () => {});
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
describe("updateUser", () => {
|
|
473
|
+
it("should update user", () => {});
|
|
474
|
+
it("should throw error if not found", () => {});
|
|
475
|
+
});
|
|
476
|
+
});
|
|
477
|
+
```
|
|
478
|
+
|
|
479
|
+
### Testing Promises
|
|
480
|
+
|
|
481
|
+
```typescript
|
|
482
|
+
// Using async/await
|
|
483
|
+
it("should fetch user", async () => {
|
|
484
|
+
const user = await service.fetchUser("1");
|
|
485
|
+
expect(user).toBeDefined();
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
// Testing rejections
|
|
489
|
+
it("should throw error", async () => {
|
|
490
|
+
await expect(service.fetchUser("invalid")).rejects.toThrow("Not found");
|
|
491
|
+
});
|
|
492
|
+
```
|
|
493
|
+
|
|
494
|
+
### Testing Timers
|
|
495
|
+
|
|
496
|
+
```typescript
|
|
497
|
+
import { vi } from "vitest";
|
|
498
|
+
|
|
499
|
+
it("should call function after delay", () => {
|
|
500
|
+
vi.useFakeTimers();
|
|
501
|
+
|
|
502
|
+
const callback = vi.fn();
|
|
503
|
+
setTimeout(callback, 1000);
|
|
504
|
+
|
|
505
|
+
expect(callback).not.toHaveBeenCalled();
|
|
506
|
+
|
|
507
|
+
vi.advanceTimersByTime(1000);
|
|
508
|
+
|
|
509
|
+
expect(callback).toHaveBeenCalled();
|
|
510
|
+
|
|
511
|
+
vi.useRealTimers();
|
|
512
|
+
});
|
|
513
|
+
```
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: modern-javascript-patterns
|
|
3
|
+
description: Master ES6+ features including async/await, destructuring, spread operators, arrow functions, promises, modules, iterators, generators, and functional programming patterns for writing clean, efficient JavaScript code. Use when refactoring legacy code, implementing modern patterns, or optimizing JavaScript applications.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Modern JavaScript Patterns
|
|
7
|
+
|
|
8
|
+
Comprehensive guide for mastering modern JavaScript (ES6+) features, functional programming patterns, and best practices for writing clean, maintainable, and performant code.
|
|
9
|
+
|
|
10
|
+
## When to Use This Skill
|
|
11
|
+
|
|
12
|
+
- Refactoring legacy JavaScript to modern syntax
|
|
13
|
+
- Implementing functional programming patterns
|
|
14
|
+
- Optimizing JavaScript performance
|
|
15
|
+
- Writing maintainable and readable code
|
|
16
|
+
- Working with asynchronous operations
|
|
17
|
+
- Building modern web applications
|
|
18
|
+
- Migrating from callbacks to Promises/async-await
|
|
19
|
+
- Implementing data transformation pipelines
|
|
20
|
+
|
|
21
|
+
## Detailed patterns and worked examples
|
|
22
|
+
|
|
23
|
+
Detailed pattern documentation lives in `references/details.md`. Read that file when the navigation tier above is insufficient.
|
|
24
|
+
|
|
25
|
+
## Best Practices
|
|
26
|
+
|
|
27
|
+
1. **Use const by default**: Only use let when reassignment is needed
|
|
28
|
+
2. **Prefer arrow functions**: Especially for callbacks
|
|
29
|
+
3. **Use template literals**: Instead of string concatenation
|
|
30
|
+
4. **Destructure objects and arrays**: For cleaner code
|
|
31
|
+
5. **Use async/await**: Instead of Promise chains
|
|
32
|
+
6. **Avoid mutating data**: Use spread operator and array methods
|
|
33
|
+
7. **Use optional chaining**: Prevent "Cannot read property of undefined"
|
|
34
|
+
8. **Use nullish coalescing**: For default values
|
|
35
|
+
9. **Prefer array methods**: Over traditional loops
|
|
36
|
+
10. **Use modules**: For better code organization
|
|
37
|
+
11. **Write pure functions**: Easier to test and reason about
|
|
38
|
+
12. **Use meaningful variable names**: Self-documenting code
|
|
39
|
+
13. **Keep functions small**: Single responsibility principle
|
|
40
|
+
14. **Handle errors properly**: Use try/catch with async/await
|
|
41
|
+
15. **Use strict mode**: `'use strict'` for better error catching
|
|
42
|
+
|
|
43
|
+
For common pitfalls (this binding, promise anti-patterns, memory leaks), see [references/advanced-patterns.md](references/advanced-patterns.md).
|