UNPKG

baseline-lint

Version:

Check web features for Baseline compatibility

314 lines (293 loc) 7.64 kB
// src/utils/test-helpers.js // Test helpers and utilities for testing environment /** * Check if we're running in a test environment */ export function isTestEnvironment() { return process.env.NODE_ENV === 'test' || process.env.NODE_TEST_CONTEXT !== undefined || process.argv.some(arg => arg.includes('node_modules/.bin/jest') || arg.includes('node_modules/jest')); } /** * Mock BCD status for testing */ export function getMockBCDStatus(bcdKey) { // Return mock status for common test cases const mockStatuses = { // CSS Properties 'css.properties.backdrop-filter': { baseline: 'low', baseline_low_date: '2024-09-16', support: { chrome: '76', chrome_android: '76', edge: '79', firefox: '103', firefox_android: '103', safari: '18', safari_ios: '18' } }, 'css.properties.display': { baseline: 'high', baseline_high_date: '2012-01-01', support: { chrome: '1', chrome_android: '18', edge: '12', firefox: '1', firefox_android: '4', safari: '1', safari_ios: '1' } }, 'css.properties.grid': { baseline: 'high', baseline_high_date: '2017-03-14', support: { chrome: '57', chrome_android: '57', edge: '16', firefox: '52', firefox_android: '52', safari: '10.1', safari_ios: '10.3' } }, 'css.properties.display.grid': { baseline: 'high', baseline_high_date: '2017-03-14', support: { chrome: '57', chrome_android: '57', edge: '16', firefox: '52', firefox_android: '52', safari: '10.1', safari_ios: '10.3' } }, 'css.properties.container-type': { baseline: 'low', baseline_low_date: '2023-06-06', support: { chrome: '105', chrome_android: '105', edge: '105', firefox: '110', firefox_android: '110', safari: '16', safari_ios: '16' } }, 'css.properties.container-name': { baseline: 'low', baseline_low_date: '2023-06-06', support: { chrome: '105', chrome_android: '105', edge: '105', firefox: '110', firefox_android: '110', safari: '16', safari_ios: '16' } }, 'css.properties.aspect-ratio': { baseline: 'high', baseline_high_date: '2021-03-09', support: { chrome: '88', chrome_android: '88', edge: '88', firefox: '89', firefox_android: '89', safari: '15', safari_ios: '15' } }, 'css.properties.grid-template-columns': { baseline: 'high', baseline_high_date: '2017-03-14', support: { chrome: '57', chrome_android: '57', edge: '16', firefox: '52', firefox_android: '52', safari: '10.1', safari_ios: '10.3' } }, 'css.properties.grid-template-rows': { baseline: 'high', baseline_high_date: '2017-03-14', support: { chrome: '57', chrome_android: '57', edge: '16', firefox: '52', firefox_android: '52', safari: '10.1', safari_ios: '10.3' } }, 'css.properties.gap': { baseline: 'high', baseline_high_date: '2018-05-29', support: { chrome: '66', chrome_android: '66', edge: '16', firefox: '61', firefox_android: '61', safari: '12', safari_ios: '12' } }, 'css.properties.subgrid': { baseline: false, // Limited availability support: { chrome: '117', chrome_android: '117', edge: '117', firefox: '71', firefox_android: '71', safari: '16', safari_ios: '16' } }, // JavaScript APIs 'javascript.builtins.Promise.try': { baseline: 'low', baseline_low_date: '2025-01-07', support: { chrome: '128', chrome_android: '128', edge: '128', firefox: '134', firefox_android: '134', safari: '18.2', safari_ios: '18.2' } }, 'javascript.builtins.Array.at': { baseline: 'high', baseline_high_date: '2022-03-15', support: { chrome: '92', chrome_android: '92', edge: '92', firefox: '90', firefox_android: '90', safari: '15.4', safari_ios: '15.4' } }, 'javascript.builtins.structuredClone': { baseline: false, // Limited availability support: { chrome: '98', chrome_android: '98', edge: '98', firefox: '94', firefox_android: '94', safari: '15.4', safari_ios: '15.4' } }, 'javascript.builtins.fetch': { baseline: 'high', baseline_high_date: '2015-05-26', support: { chrome: '42', chrome_android: '42', edge: '14', firefox: '39', firefox_android: '39', safari: '10.1', safari_ios: '10.3' } } }; // Check for exact match first if (mockStatuses[bcdKey]) { return mockStatuses[bcdKey]; } // Check for partial matches only for specific cases (e.g., css.properties.display.null -> css.properties.display) // But not for property-value combinations that should return null if (bcdKey.includes('.null') || bcdKey.includes('.undefined')) { const baseKey = bcdKey.replace(/\.[^.]*$/, ''); if (mockStatuses[baseKey]) { return mockStatuses[baseKey]; } } // Return null for unknown keys (simulating not found) return null; } /** * Safe BCD status lookup that doesn't hang in tests */ export async function safeGetBCDStatus(bcdKey) { if (isTestEnvironment()) { return getMockBCDStatus(bcdKey); } // In non-test environments, use the real BCD lookup try { const { getStatus } = await import('compute-baseline'); return getStatus(null, bcdKey); } catch (error) { console.debug(`BCD lookup failed for ${bcdKey}:`, error.message); return null; } } /** * Test configuration for baseline-lint */ export const TEST_CONFIG = { // Disable performance monitoring in tests enablePerformanceMonitoring: false, // Disable memory monitoring in tests enableMemoryMonitoring: false, // Use mock BCD data useMockBCD: true, // Faster timeouts for tests bcdLookupTimeout: 100, // 100ms instead of 500ms // Disable file logging in tests enableFileLogging: false, // Use console logging only logOutput: 'console', // Lower log level for tests logLevel: 'warn' }; /** * Apply test configuration */ export function applyTestConfig() { if (isTestEnvironment()) { // Set environment variables for test mode process.env.BASELINE_LINT_TEST_MODE = 'true'; process.env.NODE_ENV = 'test'; // Disable performance monitoring process.env.DISABLE_PERFORMANCE_MONITORING = 'true'; // Disable memory monitoring process.env.DISABLE_MEMORY_MONITORING = 'true'; } } /** * Clean up test environment */ export function cleanupTestEnvironment() { if (isTestEnvironment()) { // Clear any timers const highestTimeoutId = setTimeout(() => {}, 0); for (let i = 0; i < highestTimeoutId; i++) { clearTimeout(i); } // Clear any intervals const highestIntervalId = setInterval(() => {}, 0); for (let i = 0; i < highestIntervalId; i++) { clearInterval(i); } } }