@dbs-portal/tool-mock
Version:
API mocking toolkit using MSW for DBS Portal development workflows
190 lines • 5.29 kB
JavaScript
/**
* Environment detection utilities
*/
/**
* Detect the current environment and determine mocking configuration
*/
export function getEnvironmentInfo() {
const runtime = {
isTest: isTestEnvironment(),
isDevelopment: isDevelopmentEnvironment(),
isProduction: isProductionEnvironment(),
isStorybook: isStorybookEnvironment(),
};
const environment = detectEnvironment();
const mode = detectMockMode(runtime);
const shouldMock = shouldEnableMocking(runtime, mode);
return {
environment,
shouldMock,
mode,
runtime,
};
}
/**
* Check if mocking is enabled in the current environment
*/
export function isMockingEnabled() {
const envInfo = getEnvironmentInfo();
return envInfo.shouldMock;
}
/**
* Get the current mocking mode
*/
export function getMockingMode() {
const envInfo = getEnvironmentInfo();
return envInfo.mode;
}
/**
* Detect if running in browser or Node.js environment
*/
function detectEnvironment() {
if (typeof window !== 'undefined' && typeof document !== 'undefined') {
return 'browser';
}
if (typeof process !== 'undefined' && process.versions?.node) {
return 'node';
}
return 'unknown';
}
/**
* Check if running in test environment
*/
function isTestEnvironment() {
return (process.env['NODE_ENV'] === 'test' ||
process.env['VITEST'] === 'true' ||
process.env['JEST_WORKER_ID'] !== undefined ||
typeof global !== 'undefined' && 'expect' in global);
}
/**
* Check if running in development environment
*/
function isDevelopmentEnvironment() {
return (process.env['NODE_ENV'] === 'development' ||
import.meta.env?.DEV === true);
}
/**
* Check if running in production environment
*/
function isProductionEnvironment() {
return (process.env['NODE_ENV'] === 'production' ||
import.meta.env?.PROD === true);
}
/**
* Check if running in Storybook environment
*/
function isStorybookEnvironment() {
return (typeof window !== 'undefined' &&
(window.location?.pathname?.includes('storybook') ||
window.parent !== window ||
'STORYBOOK_ENV' in process.env));
}
/**
* Detect the appropriate mock mode based on runtime environment
*/
function detectMockMode(runtime) {
if (runtime.isTest) {
return 'testing';
}
if (runtime.isStorybook) {
return 'storybook';
}
if (runtime.isDevelopment) {
return 'development';
}
return 'disabled';
}
/**
* Determine if mocking should be enabled based on environment and configuration
*/
function shouldEnableMocking(runtime, _mode) {
// Always enable in test environment
if (runtime.isTest) {
return true;
}
// Always enable in Storybook
if (runtime.isStorybook) {
return true;
}
// Never enable in production unless explicitly forced
if (runtime.isProduction) {
return process.env['VITE_FORCE_MOCKING'] === 'true';
}
// In development, check environment variables
if (runtime.isDevelopment) {
// Explicit disable
if (process.env['VITE_ENABLE_MOCKING'] === 'false') {
return false;
}
// Explicit enable
if (process.env['VITE_ENABLE_MOCKING'] === 'true') {
return true;
}
// Default to enabled in development
return true;
}
return false;
}
/**
* Get environment-specific configuration defaults
*/
export function getEnvironmentDefaults() {
const envInfo = getEnvironmentInfo();
switch (envInfo.mode) {
case 'development':
return {
enabled: true,
logging: true,
delay: [100, 300],
errorSimulation: {
networkErrorRate: 0.01,
serverErrorRate: 0.005,
},
};
case 'testing':
return {
enabled: true,
logging: false,
delay: 0,
errorSimulation: {
networkErrorRate: 0,
serverErrorRate: 0,
},
};
case 'storybook':
return {
enabled: true,
logging: false,
delay: [50, 150],
errorSimulation: {
networkErrorRate: 0,
serverErrorRate: 0,
},
};
case 'disabled':
default:
return {
enabled: false,
logging: false,
delay: 0,
errorSimulation: {
networkErrorRate: 0,
serverErrorRate: 0,
},
};
}
}
/**
* Get environment variables related to mocking
*/
export function getMockingEnvironmentVariables() {
return {
enabled: process.env['VITE_ENABLE_MOCKING'],
mode: process.env['VITE_MOCK_MODE'],
baseUrl: process.env['VITE_MOCK_API_BASE_URL'],
delay: process.env['VITE_MOCK_DELAY'],
logging: process.env['VITE_MOCK_LOGGING'],
forceEnabled: process.env['VITE_FORCE_MOCKING'],
};
}
//# sourceMappingURL=environment.js.map