Mock testing library for Egg.js applications with comprehensive mocking capabilities for HTTP, services, contexts, and testing utilities
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Core functionality for creating mock Egg.js applications in single and cluster modes with comprehensive lifecycle management and test environment setup.
Creates a mocked Egg application instance for testing with full framework integration.
/**
* Create a mocked Egg application instance
* @param {MockOption} [options] - Configuration options for the mock application
* @returns {MockApplication} Mocked application instance with full Egg.js functionality
*/
function app(options);
interface MockOption {
/** The directory of the application */
baseDir?: string;
/** Custom plugins configuration */
plugins?: any;
/** The directory of the egg framework */
framework?: string;
/** Cache application based on baseDir */
cache?: boolean;
/** Switch on process coverage, but it'll be slower */
coverage?: boolean;
/** Remove $baseDir/logs directory */
clean?: boolean;
}
interface MockApplication {
/** Wait for application to be ready */
ready(): Promise<void>;
/** Close the mock application */
close(): Promise<void>;
/** Get application callback for server creation */
callback(): any;
}Usage Examples:
const mm = require('egg-mock');
// Basic application creation
const app = mm.app();
await app.ready();
// Application with custom base directory
const app = mm.app({
baseDir: 'test/fixtures/apps/demo'
});
await app.ready();
// Framework testing
const app = mm.app({
baseDir: 'apps/demo',
framework: true,
});
await app.ready();
// Custom framework path
const app = mm.app({
baseDir: 'apps/demo',
framework: path.join(__dirname, 'my-framework'),
});
await app.ready();
// Disable coverage and cleaning
const app = mm.app({
coverage: false,
clean: false
});
await app.ready();Creates a mocked cluster application for testing with supertest integration and multi-process simulation.
/**
* Create a mocked cluster application for testing with supertest
* @param {MockOption} [options] - Configuration options for the cluster application
* @returns {ClusterApplication} Cluster application instance compatible with supertest
*/
function cluster(options);
interface ClusterApplication {
/** Get the forked process */
readonly process: ChildProcess;
/** Compatible API for supertest - returns the instance */
callback(): ClusterApplication;
/** Compatible API for supertest - get URL */
readonly url: string;
/** Compatible API for supertest - get address info */
address(): { port: number };
/** Compatible API for supertest - returns the instance */
listen(): ClusterApplication;
/** Kill the cluster process */
close(): Promise<void>;
/** Get port number for the cluster */
readonly port: number;
/** Get base directory */
readonly baseDir: string;
/** Router with pathFor method for URL generation */
readonly router: {
pathFor(name: string): string;
};
}Usage Examples:
const mm = require('egg-mock');
const request = require('supertest');
// Basic cluster application
describe('ClusterApplication', () => {
let app;
before(function (done) {
app = mm.cluster({ baseDir: 'test/fixtures/apps/demo' });
app.ready(done);
});
after(function () {
return app.close();
});
it('should 200', function (done) {
request(app.callback())
.get('/')
.expect(200, done);
});
});
// Cluster with custom workers
const app = mm.cluster({
baseDir: 'apps/demo',
workers: 2,
port: 7001
});
// Cluster with custom options
const app = mm.cluster({
baseDir: 'apps/demo',
opt: {
execArgv: ['--debug']
}
});
// Using router.pathFor for URL generation
const app = mm.cluster({ baseDir: 'apps/demo' });
const url = app.router.pathFor('user.show'); // Get URL for named route
request(app.callback())
.get(url)
.expect(200);Methods for managing the lifecycle of mock applications with proper cleanup and background task handling.
/**
* Wait for application to be ready
* @returns {Promise<void>} Promise that resolves when app is ready
*/
app.ready();
/**
* Close the mock application and clean up resources
* @returns {Promise<void>} Promise that resolves when app is closed
*/
app.close();
/**
* Wait for all background tasks to complete
* @returns {Promise<void>} Promise that resolves when all tasks finish
*/
app.backgroundTasksFinished();Usage Examples:
const mm = require('egg-mock');
describe('app lifecycle', () => {
let app;
beforeEach(async () => {
app = mm.app({
baseDir: 'test/fixtures/apps/demo'
});
await app.ready();
});
afterEach(async () => {
await app.close();
});
it('should handle background tasks', async () => {
// Trigger some background operations
app.backgroundTask(() => {
return new Promise(resolve => setTimeout(resolve, 100));
});
// Wait for all background tasks to complete
await app.backgroundTasksFinished();
});
});Simplified application setup for testing using the bootstrap module.
/**
* Bootstrap exports for simplified test setup
*/
interface BootstrapExports {
/** Node.js assert module */
assert: typeof import('assert');
/** Application instance getter */
readonly app: MockApplication;
/** Reference to main mock function */
mock: typeof mm;
/** Alias for mock */
mm: typeof mm;
}Usage Examples:
// Using bootstrap for simplified setup
const { assert, app, mock } = require('egg-mock/bootstrap');
describe('bootstrap test', () => {
it('should work with bootstrap', () => {
return app.httpRequest()
.get('/')
.expect(200);
});
afterEach(() => mock.restore());
});Application caching system for improved test performance with baseDir-based caching.
/**
* Cache configuration in MockOption
*/
interface MockOption {
/** Cache application based on baseDir (default: true) */
cache?: boolean;
}Usage Examples:
// Enable caching (default behavior)
const app1 = mm.app({ baseDir: 'apps/demo', cache: true });
const app2 = mm.app({ baseDir: 'apps/demo', cache: true });
// app2 will reuse app1 if it hasn't been closed
// Disable caching for isolated tests
const app = mm.app({
baseDir: 'apps/demo',
cache: false
});
// Always creates a new instanceSupport for testing Egg.js plugins with automatic plugin detection and loading.
/**
* Plugin configuration in MockOption
*/
interface MockOption {
/** Custom plugins configuration */
plugins?: any;
}Usage Examples:
// Test plugin automatically (if eggPlugin.name is defined in package.json)
const app = mm.app({
baseDir: 'test/fixtures/apps/plugin-demo',
});
// Test plugin with custom plugins
const app = mm.app({
baseDir: 'test/fixtures/apps/demo',
plugins: {
'my-plugin': {
enable: true,
path: path.join(__dirname, '../lib/plugin'),
},
},
});
// Test plugin in different frameworks
const app = mm.app({
baseDir: 'test/fixtures/apps/plugin-demo',
framework: path.join(__dirname, 'my-framework'),
});Install with Tessl CLI
npx tessl i tessl/npm-egg-mock