site/docs/basics/unittest.md
Unit testing is an important part of application development. Egg provides comprehensive testing support through the @eggjs/mock package and Vitest as the test runner (via @eggjs/bin v8+).
import { app } from '@eggjs/mock/bootstrap';
import assert from 'node:assert';
describe('test/app/controller/home.test.ts', () => {
it('should GET /', async () => {
const result = await app.httpRequest().get('/').expect(200);
assert(result.text === 'hi, egg');
});
});
When running tests via egg-bin test, the following are configured automatically:
describe, it, beforeAll, etc.) are injected — no imports needed in JS filestest/.setup.ts (or .setup.js) is auto-loaded as a vitest setup file@eggjs/mock/setup_vitest is auto-injected for egg applications, handling app lifecycle (beforeAll / afterEach / afterAll)For comprehensive unit testing documentation, please see: