The test runner capability provides comprehensive testing functionality using Mocha with support for TypeScript, parallel execution, changed file detection, and detailed reporting.
Executes test suites with extensive configuration options and intelligent file detection.
/**
* Test execution command extending BaseCommand
* Integrates Mocha test runner with Egg.js-specific enhancements
*/
class TestCommand extends BaseCommand {
/** Array of test files to run, defaults to test/**/*.test.(js|ts) */
files: string[];
/** Test timeout in milliseconds, default 60000 or TEST_TIMEOUT env */
timeout: number | boolean;
/** Test pattern matching using Mocha's --grep option */
grep: string[];
/** Only test changed files, default false */
changed: boolean;
/** Enable Mocha parallel mode, default false */
parallel: boolean;
/** Number of parallel jobs, default CPU count - 1 */
jobs: number;
/** Auto bootstrap agent in mocha master process, default true */
autoAgent: boolean;
/** Enable mochawesome reporter, default true */
mochawesome: boolean;
/** Abort after first test failure, default false */
bail: boolean;
/**
* Executes the test suite with configured options
* Sets NODE_ENV to 'test' and runs Mocha with formatted arguments
*/
run(): Promise<void>;
/**
* Formats Mocha command-line arguments based on configuration
* @returns Promise resolving to Mocha arguments array, or undefined if no tests
*/
protected formatMochaArgs(): Promise<string[] | undefined>;
/**
* Gets changed test files using jest-changed-files
* @param dir - Base directory to scan for changes
* @param ext - File extension (js or ts)
* @returns Promise resolving to array of changed test file paths
*/
protected getChangedTestFiles(dir: string, ext: string): Promise<string[]>;
}The test runner automatically detects test files using configurable patterns:
/**
* Test file detection and pattern matching
* Uses globby for file system scanning with exclusion patterns
*/
interface TestFileDetection {
/** Default test file patterns */
defaultPatterns: string[]; // ['test/**/*.test.(js|ts)']
/** Always excluded patterns */
excludePatterns: string[]; // ['!test/fixtures', '!test/node_modules']
/** Environment variable override */
TESTS?: string; // Comma-separated file patterns
}Usage Examples:
# Run all tests
egg-bin test
# Run specific test files
egg-bin test test/user.test.js test/auth.test.js
# Run only changed tests
egg-bin test --changed
# Run with specific timeout
egg-bin test --timeout 120000
# Run tests matching pattern
egg-bin test --grep "user login"
# Parallel execution
egg-bin test --parallel --jobs 4
# TypeScript tests
egg-bin test --typescriptThe test runner automatically includes setup files when present:
/**
* Automatic setup file detection and inclusion
* Setup files are prepended to the test file list
*/
interface SetupFileIntegration {
/** Auto-detected setup file path */
setupFile: string; // 'test/.setup.(js|ts)'
/** File existence check */
hasSetupFile(): Promise<boolean>;
}File structure example:
test/
├── .setup.ts # Auto-included first
└── user.test.ts # Regular test filesIntegration with Jest's changed file detection for efficient testing:
/**
* Changed file detection using jest-changed-files
* Only runs tests for files that have been modified
*/
interface ChangedFileDetection {
/** Gets changed files for specified roots */
getChangedFilesForRoots(
roots: string[],
options: {}
): Promise<{ changedFiles: Set<string> }>;
/** Filters for test files only */
filterTestFiles(files: string[], extension: string): string[];
}Mocha parallel mode support with agent bootstrapping:
/**
* Parallel test execution configuration
* Enables concurrent test running with process isolation
*/
interface ParallelExecution {
/** Enable parallel mode flag */
ENABLE_MOCHA_PARALLEL: string;
/** Auto-start agent in master process */
AUTO_AGENT?: string;
/** Number of concurrent jobs */
jobs: number;
}Multiple reporting options including mochawesome integration:
/**
* Test reporting configuration
* Supports various Mocha reporters with custom options
*/
interface TestReporting {
/** Reporter module path or name */
reporter?: string;
/** Reporter-specific options */
reporterOptions?: string;
/** Environment variable override */
TEST_REPORTER?: string; // Default 'spec'
/** Mochawesome configuration */
mochawesomeConfig: {
reportDir: string; // 'node_modules/.mochawesome-reports'
registerPath: string; // For parallel mode
};
}Automatic egg-mock integration for Egg.js testing:
/**
* Egg.js mock integration
* Automatically requires egg-mock/register when available
*/
interface MockIntegration {
/** Auto-detected egg-mock register path */
eggMockRegister?: string;
/** Mock registration detection */
detectEggMock(baseDir: string): Promise<string | null>;
}Test execution supports various environment variable overrides:
/**
* Environment variable configuration
* Allows customization without command-line arguments
*/
interface TestEnvironment {
/** Override test file patterns */
TESTS?: string;
/** Override test reporter */
TEST_REPORTER?: string;
/** Override test timeout */
TEST_TIMEOUT?: string;
/** Mocha binary override */
MOCHA_FILE?: string;
/** Test environment flag */
NODE_ENV: 'test';
}Direct integration with Mocha's command-line interface and options:
/**
* Mocha command-line integration
* Passes through all Mocha-compatible options
*/
interface MochaIntegration {
/** Mocha binary resolution */
mochaFile: string; // 'mocha/bin/_mocha'
/** Common Mocha arguments */
mochaArgs: string[]; // ['--exit', '--timeout=60000', etc.]
/** execArgv for Node.js process */
execArgv: string[]; // ['--unhandled-rejections=strict']
}Full TypeScript test execution with automatic configuration:
/**
* TypeScript test execution support
* Automatically configures TypeScript compilation for tests
*/
interface TypeScriptTestSupport {
/** File extension detection */
extension: 'js' | 'ts';
/** TypeScript compilation setup */
tsNodeRegister: string;
/** Path resolution for TypeScript modules */
tsconfigPaths: boolean;
}