UNPKG

archunit

Version:

ArchUnit TypeScript is an architecture testing library, to specify and assert architecture rules in your TypeScript app

257 lines 13.3 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); const globals_1 = require("@jest/globals"); const fs = __importStar(require("fs")); const path = __importStar(require("path")); const metrics_1 = require("../../src/metrics"); const fluentapi_1 = require("../../src/metrics/fluentapi"); (0, globals_1.describe)('Export Functionality Tests', () => { const testOutputDir = path.join(__dirname, 'test-output'); (0, globals_1.beforeEach)(() => { // Ensure test output directory exists if (!fs.existsSync(testOutputDir)) { fs.mkdirSync(testOutputDir, { recursive: true }); } }); (0, globals_1.afterEach)(() => { // Clean up test files if (fs.existsSync(testOutputDir)) { const files = fs.readdirSync(testOutputDir); files.forEach((file) => { fs.unlinkSync(path.join(testOutputDir, file)); }); fs.rmdirSync(testOutputDir); } }); (0, globals_1.describe)('Count Metrics Export', () => { (0, globals_1.it)('should export count metrics as HTML', async () => { const outputPath = path.join(testOutputDir, 'count-test.html'); const countMetrics = (0, metrics_1.metrics)().count(); await countMetrics.exportAsHTML(outputPath, { title: 'Test Count Metrics Report', includeTimestamp: false, // For consistent testing }); (0, globals_1.expect)(fs.existsSync(outputPath)).toBe(true); const content = fs.readFileSync(outputPath, 'utf-8'); (0, globals_1.expect)(content).toContain('<!DOCTYPE html>'); (0, globals_1.expect)(content).toContain('Test Count Metrics Report'); (0, globals_1.expect)(content).toContain('Count Metrics'); }); (0, globals_1.it)('should automatically append .html extension', async () => { const outputPath = path.join(testOutputDir, 'count-test-no-extension'); const expectedPath = outputPath + '.html'; const countMetrics = (0, metrics_1.metrics)().count(); await countMetrics.exportAsHTML(outputPath); (0, globals_1.expect)(fs.existsSync(expectedPath)).toBe(true); }); (0, globals_1.it)('should support default parameters for count export', async () => { const countMetrics = (0, metrics_1.metrics)().count(); const distPath = path.join(process.cwd(), 'reports'); const expectedPath = path.join(distPath, 'count-metrics-report.html'); // Ensure dist directory exists for test if (!fs.existsSync(distPath)) { fs.mkdirSync(distPath, { recursive: true }); } await countMetrics.exportAsHTML(); (0, globals_1.expect)(fs.existsSync(expectedPath)).toBe(true); // Clean up fs.unlinkSync(expectedPath); if (fs.readdirSync(distPath).length === 0) { fs.rmdirSync(distPath); } }); }); (0, globals_1.describe)('LCOM Metrics Export', () => { (0, globals_1.it)('should export LCOM metrics as HTML', async () => { const outputPath = path.join(testOutputDir, 'lcom-test.html'); const lcomMetrics = (0, metrics_1.metrics)().lcom(); await lcomMetrics.exportAsHTML(outputPath, { title: 'Test LCOM Metrics Report', includeTimestamp: false, }); (0, globals_1.expect)(fs.existsSync(outputPath)).toBe(true); const content = fs.readFileSync(outputPath, 'utf-8'); (0, globals_1.expect)(content).toContain('<!DOCTYPE html>'); (0, globals_1.expect)(content).toContain('Test LCOM Metrics Report'); (0, globals_1.expect)(content).toContain('LCOM Metrics'); }); (0, globals_1.it)('should support default parameters for LCOM export', async () => { const lcomMetrics = (0, metrics_1.metrics)().lcom(); const distPath = path.join(process.cwd(), 'reports'); const expectedPath = path.join(distPath, 'lcom-metrics-report.html'); // Ensure dist directory exists for test if (!fs.existsSync(distPath)) { fs.mkdirSync(distPath, { recursive: true }); } await lcomMetrics.exportAsHTML(); (0, globals_1.expect)(fs.existsSync(expectedPath)).toBe(true); // Clean up fs.unlinkSync(expectedPath); if (fs.readdirSync(distPath).length === 0) { fs.rmdirSync(distPath); } }); }); (0, globals_1.describe)('Distance Metrics Export', () => { (0, globals_1.it)('should export distance metrics as HTML', async () => { const outputPath = path.join(testOutputDir, 'distance-test.html'); const distanceMetrics = new fluentapi_1.DistanceMetricsBuilder('./tsconfig.json'); await distanceMetrics.exportAsHTML(outputPath, { title: 'Test Distance Metrics Report', includeTimestamp: false, }); (0, globals_1.expect)(fs.existsSync(outputPath)).toBe(true); const content = fs.readFileSync(outputPath, 'utf-8'); (0, globals_1.expect)(content).toContain('<!DOCTYPE html>'); (0, globals_1.expect)(content).toContain('Test Distance Metrics Report'); (0, globals_1.expect)(content).toContain('Distance Metrics'); }); (0, globals_1.it)('should support default parameters for distance export', async () => { const distanceMetrics = new fluentapi_1.DistanceMetricsBuilder('./tsconfig.json'); const distPath = path.join(process.cwd(), 'reports'); const expectedPath = path.join(distPath, 'distance-metrics-report.html'); // Ensure dist directory exists for test if (!fs.existsSync(distPath)) { fs.mkdirSync(distPath, { recursive: true }); } await distanceMetrics.exportAsHTML(); (0, globals_1.expect)(fs.existsSync(expectedPath)).toBe(true); // Clean up fs.unlinkSync(expectedPath); if (fs.readdirSync(distPath).length === 0) { fs.rmdirSync(distPath); } }); }); (0, globals_1.describe)('Export Options', () => { (0, globals_1.it)('should apply custom CSS styling', async () => { const outputPath = path.join(testOutputDir, 'custom-css-test.html'); const countMetrics = (0, metrics_1.metrics)().count(); const customCss = '.test-class { color: red; }'; await countMetrics.exportAsHTML(outputPath, { customCss: customCss, includeTimestamp: false, }); const content = fs.readFileSync(outputPath, 'utf-8'); (0, globals_1.expect)(content).toContain(customCss); }); (0, globals_1.it)('should include timestamp when requested', async () => { const outputPath = path.join(testOutputDir, 'timestamp-test.html'); const countMetrics = (0, metrics_1.metrics)().count(); await countMetrics.exportAsHTML(outputPath, { includeTimestamp: true, }); const content = fs.readFileSync(outputPath, 'utf-8'); (0, globals_1.expect)(content).toContain('Generated on:'); }); (0, globals_1.it)('should not include timestamp when disabled', async () => { const outputPath = path.join(testOutputDir, 'no-timestamp-test.html'); const countMetrics = (0, metrics_1.metrics)().count(); await countMetrics.exportAsHTML(outputPath, { includeTimestamp: false, }); const content = fs.readFileSync(outputPath, 'utf-8'); (0, globals_1.expect)(content).not.toContain('Generated on:'); }); }); (0, globals_1.describe)('Combined Export', () => { (0, globals_1.it)('should export combined metrics using MetricsExporter directly', async () => { const outputPath = path.join(testOutputDir, 'combined-test.html'); // Get summaries from different metrics const countSummary = await (0, metrics_1.metrics)().count().summary(); const lcomSummary = await (0, metrics_1.metrics)().lcom().summary(); const combinedData = { count: countSummary, lcom: lcomSummary, }; const { MetricsExporter } = await Promise.resolve().then(() => __importStar(require('../../src/metrics/fluentapi/export-utils'))); await MetricsExporter.exportAsHTML(combinedData, { outputPath: outputPath, title: 'Combined Test Report', includeTimestamp: false, }); (0, globals_1.expect)(fs.existsSync(outputPath)).toBe(true); const content = fs.readFileSync(outputPath, 'utf-8'); (0, globals_1.expect)(content).toContain('Count Metrics'); (0, globals_1.expect)(content).toContain('LCOM (Lack of Cohesion of Methods) Metrics'); }); (0, globals_1.it)('should export comprehensive metrics with default path', async () => { const { MetricsExporter } = await Promise.resolve().then(() => __importStar(require('../../src/metrics/fluentapi/export-utils'))); const distPath = path.join(process.cwd(), 'reports'); const expectedPath = path.join(distPath, 'metrics-report.html'); // Ensure dist directory exists for test if (!fs.existsSync(distPath)) { fs.mkdirSync(distPath, { recursive: true }); } await MetricsExporter.exportComprehensiveAsHTML('./tsconfig.json'); (0, globals_1.expect)(fs.existsSync(expectedPath)).toBe(true); const content = fs.readFileSync(expectedPath, 'utf-8'); (0, globals_1.expect)(content).toContain('Comprehensive ArchUnitTS Metrics Report'); (0, globals_1.expect)(content).toContain('Count Metrics'); (0, globals_1.expect)(content).toContain('LCOM (Lack of Cohesion of Methods) Metrics'); (0, globals_1.expect)(content).toContain('Distance Metrics'); // Clean up fs.unlinkSync(expectedPath); if (fs.readdirSync(distPath).length === 0) { fs.rmdirSync(distPath); } }); }); (0, globals_1.describe)('Error Handling', () => { (0, globals_1.it)('should handle invalid output directory gracefully', async () => { const invalidPath = '/invalid/path/that/does/not/exist/test.html'; const countMetrics = (0, metrics_1.metrics)().count(); // The function should handle errors gracefully and might create directories // Let's test that it either succeeds or throws appropriately try { await countMetrics.exportAsHTML(invalidPath); // If it succeeds, that's also acceptable behavior } catch (error) { // If it throws, that's expected for truly invalid paths (0, globals_1.expect)(error).toBeDefined(); } }); (0, globals_1.it)('should handle missing export options gracefully', async () => { const outputPath = path.join(testOutputDir, 'no-options-test.html'); const countMetrics = (0, metrics_1.metrics)().count(); // Should not throw when no options provided await (0, globals_1.expect)(countMetrics.exportAsHTML(outputPath)).resolves.not.toThrow(); (0, globals_1.expect)(fs.existsSync(outputPath)).toBe(true); }); }); }); //# sourceMappingURL=export-functionality.spec.js.map