Environment variable mocking, server configuration, and global settings management for comprehensive test environment setup.
Mock Egg.js server environment variables for testing different deployment scenarios.
/**
* Mock the server environment of Egg
* @param {EnvType} env - Environment name
*/
mm.env(env);
/**
* Mock server environment on application instance
* @param {string} env - Environment name
* @returns {Application} Application instance for chaining
*/
app.mockEnv(env);
type EnvType = 'default' | 'test' | 'prod' | 'local' | 'unittest' | string;Usage Examples:
// Test production environment behavior
describe('Production environment', () => {
beforeEach(() => {
mm.env('prod');
});
afterEach(() => {
mm.restore();
});
it('should use production configuration', () => {
const app = mm.app();
return app.ready().then(() => {
assert.equal(app.config.env, 'prod');
assert.equal(process.env.EGG_SERVER_ENV, 'prod');
assert.equal(process.env.EGG_MOCK_SERVER_ENV, 'prod');
return app.close();
});
});
});
// Test different environments
['development', 'test', 'production'].forEach(env => {
it(`should work in ${env} environment`, () => {
mm.env(env);
const app = mm.app();
return app.ready().then(() => {
assert.equal(app.config.env, env);
return app.close();
});
});
});
// Application-level environment mocking
it('should mock environment on app instance', () => {
const app = mm.app();
return app.ready().then(() => {
app.mockEnv('staging');
assert.equal(app.config.env, 'staging');
assert.equal(app.config.serverEnv, 'staging');
return app.close();
});
});Control console logging levels for testing and debugging.
/**
* Mock console log level
* @param {LogLevel} level - Logger level
*/
mm.consoleLevel(level);
type LogLevel = 'DEBUG' | 'INFO' | 'WARN' | 'ERROR' | 'NONE';Usage Examples:
// Set debug level for verbose testing
describe('Debug logging', () => {
beforeEach(() => {
mm.consoleLevel('DEBUG');
});
afterEach(() => {
mm.restore();
});
it('should show debug messages', () => {
const app = mm.app();
return app.ready().then(() => {
assert.equal(process.env.EGG_LOG, 'DEBUG');
// Debug messages will be visible in test output
return app.close();
});
});
});
// Suppress logging during tests
it('should suppress logs in quiet tests', () => {
mm.consoleLevel('NONE');
const app = mm.app();
return app.ready().then(() => {
assert.equal(process.env.EGG_LOG, 'NONE');
// No log output during this test
return app.close();
});
});
// Test different log levels
['DEBUG', 'INFO', 'WARN', 'ERROR'].forEach(level => {
it(`should set log level to ${level}`, () => {
mm.consoleLevel(level);
assert.equal(process.env.EGG_LOG, level);
});
});
// Case insensitive level setting
it('should handle lowercase log levels', () => {
mm.consoleLevel('debug'); // lowercase
assert.equal(process.env.EGG_LOG, 'DEBUG'); // uppercase
});Set and mock the EGG_HOME environment variable for testing.
/**
* Set EGG_HOME path
* @param {string} homePath - Home directory path
*/
mm.home(homePath);Usage Examples:
// Set custom home directory
describe('Custom home directory', () => {
const testHome = path.join(__dirname, 'fixtures', 'egg-home');
beforeEach(() => {
mm.home(testHome);
});
afterEach(() => {
mm.restore();
});
it('should use custom home directory', () => {
const app = mm.app();
return app.ready().then(() => {
assert.equal(process.env.EGG_HOME, testHome);
return app.close();
});
});
});
// Test without home directory override
it('should use default home when not set', () => {
// Don't call mm.home()
const app = mm.app();
return app.ready().then(() => {
// EGG_HOME should not be set by egg-mock
assert(!process.env.EGG_HOME);
return app.close();
});
});
// Test home directory with configuration files
it('should load config from custom home', () => {
const customHome = path.join(__dirname, 'fixtures', 'custom-home');
mm.home(customHome);
const app = mm.app();
return app.ready().then(() => {
// Configuration should be loaded from custom home
assert.equal(app.config.customSetting, 'from-custom-home');
return app.close();
});
});Configure application callback for bootstrap testing scenarios.
/**
* Set callback for getting app instance in tests
* @param {Function} cb - Callback function that returns Promise<MockApplication>
*/
mm.setGetAppCallback(cb);Usage Examples:
// Custom app creation for bootstrap testing
describe('Bootstrap with custom app', () => {
before(() => {
mm.setGetAppCallback(async (suite) => {
const app = mm.app({
baseDir: path.join(__dirname, 'fixtures', suite.title),
framework: path.join(__dirname, 'custom-framework')
});
await app.ready();
return app;
});
});
it('should use custom app creation', () => {
const { app } = require('egg-mock/bootstrap');
assert(app);
assert(app.config.customFramework);
});
});
// Dynamic app configuration based on test suite
describe('Dynamic app configuration', () => {
before(() => {
mm.setGetAppCallback(async (suite) => {
const config = suite.title.includes('cluster')
? { workers: 2 }
: { cache: false };
const app = mm.app({
baseDir: 'test/fixtures/apps/demo',
...config
});
await app.ready();
return app;
});
});
it('should create single worker app', () => {
const { app } = require('egg-mock/bootstrap');
assert.equal(app.config.workers, undefined);
});
describe('cluster tests', () => {
it('should create cluster app', () => {
const { app } = require('egg-mock/bootstrap');
assert.equal(app.config.workers, 2);
});
});
});Integration with Egg.js configuration system for environment-specific settings.
Usage Examples:
// Test environment-specific configuration
describe('Environment-specific config', () => {
it('should load test configuration', () => {
mm.env('test');
const app = mm.app();
return app.ready().then(() => {
assert.equal(app.config.env, 'test');
assert.equal(app.config.database.host, 'localhost'); // test config
assert.equal(app.config.redis.port, 6379); // test config
return app.close();
});
});
it('should load production configuration', () => {
mm.env('prod');
const app = mm.app();
return app.ready().then(() => {
assert.equal(app.config.env, 'prod');
assert.equal(app.config.database.host, 'prod-db.example.com');
assert.equal(app.config.redis.host, 'prod-redis.example.com');
return app.close();
});
});
});
// Test custom environment configurations
it('should support custom environments', () => {
mm.env('staging');
const app = mm.app();
return app.ready().then(() => {
assert.equal(app.config.env, 'staging');
// Custom staging configuration should be loaded
assert.equal(app.config.customStagingSetting, true);
return app.close();
});
});
// Test environment precedence
it('should respect environment variable precedence', () => {
// Set both global and app-level environment
mm.env('test');
const app = mm.app();
return app.ready().then(() => {
app.mockEnv('local');
// app.mockEnv should override mm.env for config
assert.equal(app.config.env, 'local');
assert.equal(app.config.serverEnv, 'local');
return app.close();
});
});Test plugin behavior in different environments.
Usage Examples:
// Test plugin in different environments
describe('Plugin environment behavior', () => {
['development', 'test', 'production'].forEach(env => {
it(`should work in ${env} environment`, () => {
mm.env(env);
const app = mm.app({
baseDir: 'test/fixtures/apps/plugin-demo'
});
return app.ready().then(() => {
// Plugin should behave differently based on environment
const plugin = app.plugins.get('test-plugin');
assert.equal(plugin.config.env, env);
return app.close();
});
});
});
});
// Test environment-specific plugin loading
it('should load environment-specific plugins', () => {
mm.env('development');
const app = mm.app();
return app.ready().then(() => {
// Development-only plugins should be loaded
assert(app.plugins.has('dev-tools'));
assert(!app.plugins.has('monitoring')); // prod-only plugin
return app.close();
});
});Manage global environment state across test suites.
Usage Examples:
// Global environment setup
describe('Global environment tests', () => {
// Save original environment
const originalEnv = process.env.NODE_ENV;
beforeEach(() => {
mm.env('test');
mm.consoleLevel('ERROR'); // Quiet during tests
});
afterEach(() => {
mm.restore();
// Restore original environment if needed
if (originalEnv) {
process.env.NODE_ENV = originalEnv;
}
});
it('should maintain environment isolation', () => {
const app1 = mm.app();
return app1.ready().then(() => {
assert.equal(app1.config.env, 'test');
return app1.close();
}).then(() => {
// Environment should be clean for next app
mm.env('prod');
const app2 = mm.app();
return app2.ready().then(() => {
assert.equal(app2.config.env, 'prod');
return app2.close();
});
});
});
});