What problem does it solve?
Writing unit tests for Egg.js applications requires knowing the correct testing APIs, lifecycle hooks, and mock patterns. This Skill provides structured guidance for testing HTTP endpoints, Service/DI objects, background tasks, and event handlers without common pitfalls like CSRF 403 errors or unawaited assertions.
Core Features & Use Cases
- HTTP Interface Testing: Test GET/POST/PUT/DELETE endpoints with app.httpRequest(), including CSRF mocking, request construction, and response assertions.
- Service & DI Object Testing: Retrieve SingletonProto and ContextProto instances via app.getEggObject() and mockModuleContextScope for lifecycle-managed testing.
- Mock Patterns: Use mm() to mock prototype methods, mm.spy() to record calls, and app.mockHttpclient() to stub external HTTP requests.
- Async Workflow Testing: Wait for BackgroundTaskHelper completion with app.backgroundTasksFinished() and verify EventBus handlers with app.getEventWaiter().
- Use Case: When building an Egg.js order module, use this Skill to write tests that mock the PaymentService prototype, POST to the order API with CSRF disabled, and assert the background notification task completes.
Quick Start
Ask the AI to write a Vitest unit test for your Egg.js controller or service using @eggjs/mock bootstrap.