Test Generator
This skill automates the creation of unit tests for NestJS components, ensuring a consistent testing pattern across the project.
Workflow
- •Identify the target file: Provide the path to the Service or Controller you want to test (e.g.,
src/modules/auth/auth.service.ts). - •Execute the generator script: Use the bundled script to create the
.spec.tsfile. - •Refine the test: The generated test will include a basic setup and "should be defined" test. You will need to add specific business logic tests.
- •Run the test: Use
npm run testornpx jest path/to/file.spec.ts.
Usage
Run the following command from the project root:
bash
node .gemini/skills/test-generator/scripts/generate_test.cjs <file-path>
Example
To create tests for AuthService:
bash
node .gemini/skills/test-generator/scripts/generate_test.cjs src/modules/auth/auth.service.ts
This will:
- •Create
src/modules/auth/auth.service.spec.ts. - •Analyze the constructor of
AuthServiceto identify dependencies. - •Create mock providers for each dependency (e.g.,
UnitOfWork,MoodleService). - •Scaffold a
describeblock with abeforeEachthat sets up theTest.createTestingModule.
Standards Applied
- •File Naming:
<original-name>.spec.ts. - •Framework: Jest with
@nestjs/testing. - •Mocks: Uses
jest.fn()or specialized mock objects for complex dependencies likeUnitOfWork.