AgentSkillsCN

nx-monorepo-generator

在产品概念验证阶段,搭建上线前的着陆页。当工厂代理需要为需求验证、等待名单收集,或早期访问注册创建 Astro 站点时,可使用此技能。阅读 Scout 的交接文档,了解市场调研与竞争对手动态。涵盖英雄页面的选择、视觉系统、组件组装、文案框架,以及转化优化。本技能不适用于产品界面,或上线后的页面。

SKILL.md
--- frontmatter
name: nx-monorepo-generator
description: Use when adding Jest testing configuration to Nx projects, running `nx g @nx/jest:configuration`, setting up testing infrastructure, or troubleshooting tests that fail after running the Jest generator. Ensures proper TypeScript moduleResolution (nodenext), testing enhancement libraries (jest-dom, user-event, MSW), and monorepo pattern adherence. Triggers: "add tests", "setup Jest", "configure testing", "Jest not working", "moduleResolution error", "tests failing after generator"

Nx Monorepo Generator - Jest Configuration Workflow

The Nx Jest generator produces defaults that conflict with this workspace's standards. This skill ensures all mandatory post-generation fixes are applied.

Phase 1: Determine Project Type

Identify the project type before generating:

TypeExamplesTesting Needs
UIweb apps, mobileFull stack: jest-dom, user-event, MSW
Nodeserver, APIsjest-dom + conditional MSW
Logicschemas, utilsBasic Jest only

How to identify:

  • Check project.json executors (React/Next.js = UI, Node = server)
  • Schema/utility libraries = Logic
  • When uncertain, ask the user

Phase 2: Generate Configuration

bash
pnpm exec nx g @nx/jest:configuration <project-name>

Creates: jest.config.ts, tsconfig.spec.json, updates project.json

Warning: Generated config has incorrect defaults - proceed to Phase 3.

Phase 3: Post-Generation Fixes

Execute ALL applicable steps. See references/post-gen-checklist.md for full details.

StepUINodeLogicScript
Install testing libsscripts/install-testing-libs.sh
Create jest.setup.tsManual
Update jest.config.tsManual
Fix moduleResolutionscripts/fix-tsconfig-spec.sh
Verify Jest typesscripts/validate-jest-config.sh
Clean production configscripts/validate-jest-config.sh

Critical Fix: TypeScript moduleResolution

In tsconfig.spec.json, change:

  • "module": "commonjs""module": "nodenext"
  • "moduleResolution": "node10""moduleResolution": "nodenext"

Why: Workspace uses customConditions requiring modern module resolution.

Phase 4: Validation

bash
# Automated validation
scripts/validate-jest-config.sh <project-path>

# Run tests
pnpm exec nx run <project-name>:test

Success indicators:

  • ✅ Tests run without TypeScript errors
  • ✅ No module resolution warnings
  • ✅ Coverage reports generate correctly

References

  • references/post-gen-checklist.md - Step-by-step checklist with code examples
  • references/jest-config-patterns.md - Configuration structure and rationale
  • references/testing-standards.md - user-event, MSW, jest-dom patterns

Scripts

  • scripts/validate-jest-config.sh <path> - Validate configuration against standards
  • scripts/install-testing-libs.sh <type> <path> - Install testing packages by project type
  • scripts/fix-tsconfig-spec.sh <path> - Auto-fix moduleResolution settings

Key Principle

Generated code rarely matches monorepo patterns. Post-generation fixes are MANDATORY. This skill ensures consistency and prevents pattern drift that causes mysterious failures.