archunit
Version:
ArchUnit TypeScript is an architecture testing library, to specify and assert architecture rules in your TypeScript app
124 lines • 6.03 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const violation_factory_1 = require("./violation-factory");
const matching_files_1 = require("../../files/assertion/matching-files");
const cycle_free_1 = require("../../files/assertion/cycle-free");
const depend_on_files_1 = require("../../files/assertion/depend-on-files");
const admissible_edges_1 = require("../../slices/assertion/admissible-edges");
const custom_file_logic_1 = require("../../files/assertion/custom-file-logic");
const EmptyTestViolation_1 = require("../../common/assertion/EmptyTestViolation");
const metric_thresholds_1 = require("../../metrics/assertion/metric-thresholds");
const count_metrics_1 = require("../../metrics/fluentapi/metrics/count-metrics");
describe('ViolationFactory', () => {
beforeEach(() => {
Object.defineProperty(process.stdout, 'isTTY', {
value: true,
configurable: true,
});
delete process.env.NO_COLOR;
delete process.env.CI;
});
describe('from', () => {
it('should handle ViolatingNode', () => {
const node = {
label: 'src/test.ts',
incoming: [],
outgoing: [],
};
const violation = new matching_files_1.ViolatingNode('*.test.ts', node, false);
const result = violation_factory_1.ViolationFactory.from(violation);
expect(result.message).toContain('File pattern violation');
expect(result.details).toBe(violation);
});
it('should handle negated ViolatingNode', () => {
const node = {
label: 'src/test.ts',
incoming: [],
outgoing: [],
};
const violation = new matching_files_1.ViolatingNode('*.test.ts', node, true);
const result = violation_factory_1.ViolationFactory.from(violation);
expect(result.message).toContain('should not match');
});
it('should handle EmptyTestViolation', () => {
const violation = new EmptyTestViolation_1.EmptyTestViolation(['src/**/*.ts']);
const result = violation_factory_1.ViolationFactory.from(violation);
expect(result.message).toContain('Empty test violation');
expect(result.details).toBe(violation);
});
it('should handle ViolatingEdge', () => {
const edge = {
sourceLabel: 'sliceA',
targetLabel: 'sliceB',
cumulatedEdges: [],
};
const violation = new admissible_edges_1.ViolatingEdge(null, edge);
const result = violation_factory_1.ViolationFactory.from(violation);
expect(result.message).toContain('Slice dependency violation');
expect(result.details).toBe(violation);
});
it('should handle ViolatingCycle', () => {
const edges = [
{
sourceLabel: 'a.ts',
targetLabel: 'b.ts',
cumulatedEdges: [],
},
{
sourceLabel: 'b.ts',
targetLabel: 'a.ts',
cumulatedEdges: [],
},
];
const violation = new cycle_free_1.ViolatingCycle(edges);
const result = violation_factory_1.ViolationFactory.from(violation);
expect(result.message).toContain('Circular dependency detected');
expect(result.details).toBe(violation);
});
it('should handle ViolatingFileDependency', () => {
const edge = {
sourceLabel: 'src/a.ts',
targetLabel: 'src/b.ts',
cumulatedEdges: [],
};
const violation = new depend_on_files_1.ViolatingFileDependency(edge);
const result = violation_factory_1.ViolationFactory.from(violation);
expect(result.message).toContain('File dependency violation');
expect(result.details).toBe(violation);
});
it('should handle MetricViolation', () => {
const violation = new metric_thresholds_1.MetricViolation('MyClass', 'src/my-class.ts', 'methodCount', 15, 10, 'below');
const result = violation_factory_1.ViolationFactory.from(violation);
expect(result.message).toContain('Metric violation');
expect(result.message).toContain('MyClass');
expect(result.details).toBe(violation);
});
it('should handle FileCountViolation', () => {
const violation = new count_metrics_1.FileCountViolation('src/test.ts', 'linesOfCode', 200, 100, 'below');
const result = violation_factory_1.ViolationFactory.from(violation);
expect(result.message).toContain('File count violation');
expect(result.details).toBe(violation);
});
it('should handle CustomFileViolation', () => {
const fileInfo = {
path: 'src/test.ts',
name: 'test',
extension: 'ts',
directory: 'src',
content: 'const x = 1;',
linesOfCode: 1,
};
const violation = new custom_file_logic_1.CustomFileViolation('Custom rule failed', fileInfo, 'custom-rule');
const result = violation_factory_1.ViolationFactory.from(violation);
expect(result.message).toContain('Custom file condition violation');
expect(result.message).toContain('Custom rule failed');
expect(result.details).toBe(violation);
});
it('should return UnknownTestViolation for unknown types', () => {
const unknownViolation = { someField: 'value' };
const result = violation_factory_1.ViolationFactory.from(unknownViolation);
expect(result.message).toBe('Unknown Violation found');
});
});
});
//# sourceMappingURL=violation-factory.spec.js.map