egg-unittest

Write unit tests for Egg.js applications using @eggjs/mock and Vitest.

19.0k|1.8k|Updated Jun 18, 2016
One-click install
npx skills add https://github.com/eggjs/egg --skill egg-unittest
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: egg-unittest
Source: https://github.com/eggjs/egg/tree/main/packages/skills/egg-unittest
Command: npx skills add https://github.com/eggjs/egg --skill egg-unittest

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires @eggjs/bin, @eggjs/mock, and includes references (resource) components.

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.

Frequently Asked Questions about egg-unittest

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I test HTTP endpoints in an Egg.js application?

Use app.httpRequest() from @eggjs/mock/bootstrap to send requests and chain .expect() assertions for status codes and response bodies. For POST/PUT/DELETE requests, call app.mockCsrf() first to bypass CSRF validation, and always return or await the request.

How to mock a Service method in Egg.js unit tests?

Use mm(ServiceClass.prototype, 'methodName', mockFn) to mock the prototype method of a DI-managed object, not an instance. The mock function records call counts and arguments, and egg-bin automatically restores mocks after each test.

Why does my Egg.js POST test return a 403 error?

The security plugin enables CSRF validation by default, which blocks POST requests without a valid token. Call app.mockCsrf() before sending the request to skip CSRF checks in tests.

How do I test async background tasks in Egg.js?

Use app.mockModuleContextScope(), which automatically waits for background tasks when the scope exits, or call app.backgroundTasksFinished() manually after triggering tasks via HTTP. Assert results only after the wait completes.

Does Egg.js testing use Mocha or Vitest?

egg-bin test runs tests with Vitest, so use beforeAll and afterAll hooks instead of Mocha's before and after. Vitest globals like describe and it are injected automatically without imports.