archunit
Version:
ArchUnit TypeScript is an architecture testing library, to specify and assert architecture rules in your TypeScript app
215 lines • 10.2 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const result_factory_1 = require("../common/result-factory");
const violation_factory_1 = require("../common/violation-factory");
const jasmine_adapter_1 = require("./jasmine-adapter");
/* eslint-disable @typescript-eslint/no-explicit-any */
// Mock the dependencies
jest.mock('../common/result-factory');
jest.mock('../common/violation-factory');
describe('extendJasmineMatchers', () => {
let originalJasmine;
let originalBeforeEach;
let mockAddMatchers;
let mockBeforeEach;
let mockResultFactory;
let mockViolationFactory;
beforeEach(() => {
// Store original global values
originalJasmine = globalThis.jasmine;
originalBeforeEach = globalThis.beforeEach;
// Setup mocks
mockAddMatchers = jest.fn();
mockBeforeEach = jest.fn();
mockResultFactory = result_factory_1.ResultFactory;
mockViolationFactory = violation_factory_1.ViolationFactory;
// Clear all mocks
jest.clearAllMocks();
});
afterEach(() => {
// Restore original global values
globalThis.jasmine = originalJasmine;
globalThis.beforeEach = originalBeforeEach;
});
describe('when jasmine is not available', () => {
beforeEach(() => {
globalThis.jasmine = undefined;
});
it('should not throw an error', () => {
expect(() => (0, jasmine_adapter_1.extendJasmineMatchers)()).not.toThrow();
});
it('should not call any matchers or beforeEach functions', () => {
(0, jasmine_adapter_1.extendJasmineMatchers)();
expect(mockAddMatchers).not.toHaveBeenCalled();
expect(mockBeforeEach).not.toHaveBeenCalled();
});
});
describe('when jasmine is available but addMatchers is not', () => {
beforeEach(() => {
globalThis.jasmine = {};
globalThis.beforeEach = mockBeforeEach;
});
it('should not call addMatchers or beforeEach', () => {
(0, jasmine_adapter_1.extendJasmineMatchers)();
expect(mockAddMatchers).not.toHaveBeenCalled();
expect(mockBeforeEach).not.toHaveBeenCalled();
});
});
describe('when jasmine is available but beforeEach is not', () => {
beforeEach(() => {
globalThis.jasmine = { addMatchers: mockAddMatchers };
globalThis.beforeEach = undefined;
});
it('should not call addMatchers or beforeEach', () => {
(0, jasmine_adapter_1.extendJasmineMatchers)();
expect(mockAddMatchers).not.toHaveBeenCalled();
expect(mockBeforeEach).not.toHaveBeenCalled();
});
});
describe('when both jasmine and beforeEach are available', () => {
let registeredBeforeEachCallback;
beforeEach(() => {
globalThis.jasmine = { addMatchers: mockAddMatchers };
globalThis.beforeEach = mockBeforeEach;
mockBeforeEach.mockImplementation((callback) => {
registeredBeforeEachCallback = callback;
});
(0, jasmine_adapter_1.extendJasmineMatchers)();
});
it('should call beforeEach with a callback', () => {
expect(mockBeforeEach).toHaveBeenCalledTimes(1);
expect(mockBeforeEach).toHaveBeenCalledWith(expect.any(Function));
});
it('should register toPassAsync matcher when beforeEach callback is executed', () => {
registeredBeforeEachCallback();
expect(mockAddMatchers).toHaveBeenCalledTimes(1);
expect(mockAddMatchers).toHaveBeenCalledWith({
toPassAsync: expect.any(Function),
});
});
describe('toPassAsync matcher', () => {
let toPassAsyncFactory;
beforeEach(() => {
registeredBeforeEachCallback();
const matchersArg = mockAddMatchers.mock.calls[0][0];
toPassAsyncFactory = matchersArg.toPassAsync;
});
it('should return a matcher with compare function', () => {
const matcher = toPassAsyncFactory();
expect(matcher).toHaveProperty('compare');
expect(typeof matcher.compare).toBe('function');
});
describe('compare function', () => {
let compareFunction;
beforeEach(() => {
const matcher = toPassAsyncFactory();
compareFunction = matcher.compare;
});
it('should return failure when checkable is null', async () => {
const result = await compareFunction(null);
expect(result).toEqual({
pass: false,
message: 'expected something checkable as an argument for expect()',
});
});
it('should return failure when checkable is undefined', async () => {
const result = await compareFunction(undefined);
expect(result).toEqual({
pass: false,
message: 'expected something checkable as an argument for expect()',
});
});
it('should create result through ResultFactory', async () => {
const mockViolations = [{ type: 'error' }];
const mockCheckable = {
check: jest.fn().mockResolvedValue(mockViolations),
};
const mockProcessedViolation = { processed: true };
const mockResult = {
pass: true,
message: jest.fn().mockReturnValue('test message'),
};
mockViolationFactory.from.mockReturnValue(mockProcessedViolation);
mockResultFactory.result.mockReturnValue(mockResult);
await compareFunction(mockCheckable);
expect(mockResultFactory.result).toHaveBeenCalledTimes(1);
expect(mockResultFactory.result).toHaveBeenCalledWith(false, [
mockProcessedViolation,
]);
});
it('should return result with pass and message from ResultFactory', async () => {
const mockCheckable = {
check: jest.fn().mockResolvedValue([]),
};
const mockResult = {
pass: true,
message: jest.fn().mockReturnValue('success message'),
};
mockViolationFactory.from.mockReturnValue({});
mockResultFactory.result.mockReturnValue(mockResult);
const result = await compareFunction(mockCheckable);
expect(mockResult.message).toHaveBeenCalledTimes(1);
expect(result).toEqual({
pass: true,
message: 'success message',
});
});
it('should handle multiple violations', async () => {
const mockViolations = [
{ type: 'error1', message: 'first violation' },
{ type: 'error2', message: 'second violation' },
];
const mockCheckable = {
check: jest.fn().mockResolvedValue(mockViolations),
};
const mockProcessedViolations = [
{ processed: true, id: 1 },
{ processed: true, id: 2 },
];
mockViolationFactory.from
.mockReturnValueOnce(mockProcessedViolations[0])
.mockReturnValueOnce(mockProcessedViolations[1]);
await compareFunction(mockCheckable);
expect(mockViolationFactory.from).toHaveBeenCalledTimes(2);
expect(mockViolationFactory.from).toHaveBeenNthCalledWith(1, mockViolations[0]);
expect(mockViolationFactory.from).toHaveBeenNthCalledWith(2, mockViolations[1]);
expect(mockResultFactory.result).toHaveBeenCalledWith(false, mockProcessedViolations);
});
it('should handle async violations processing', async () => {
const mockCheckable = {
check: jest.fn().mockResolvedValue([{ async: 'violation' }]),
};
mockViolationFactory.from.mockReturnValue({
processed: 'async',
});
mockResultFactory.result.mockReturnValue({
pass: false,
message: jest.fn().mockReturnValue('async failure'),
});
const result = await compareFunction(mockCheckable);
expect(result.pass).toBe(false);
expect(result.message).toBe('async failure');
});
});
});
});
describe('edge cases', () => {
it('should handle jasmine object without proper typing', () => {
const mockJasmineObject = {
addMatchers: mockAddMatchers,
someOtherProperty: 'test',
};
globalThis.jasmine = mockJasmineObject;
globalThis.beforeEach = mockBeforeEach;
expect(() => (0, jasmine_adapter_1.extendJasmineMatchers)()).not.toThrow();
expect(mockBeforeEach).toHaveBeenCalledTimes(1);
});
it('should handle beforeEach function without proper typing', () => {
globalThis.jasmine = { addMatchers: mockAddMatchers };
globalThis.beforeEach = mockBeforeEach;
expect(() => (0, jasmine_adapter_1.extendJasmineMatchers)()).not.toThrow();
expect(mockBeforeEach).toHaveBeenCalledTimes(1);
});
});
});
//# sourceMappingURL=jasmine-adapter.spec.js.map
;