UNPKG

archunit

Version:

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

136 lines 6.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const result_factory_1 = require("../common/result-factory"); const violation_factory_1 = require("../common/violation-factory"); const vitest_adapter_1 = require("./vitest-adapter"); jest.mock('../common/result-factory'); jest.mock('../common/violation-factory'); describe('extendVitestMatchers', () => { let mockResultFactory; let mockViolationFactory; beforeEach(() => { mockResultFactory = result_factory_1.ResultFactory; mockViolationFactory = violation_factory_1.ViolationFactory; jest.clearAllMocks(); }); it('should throw an error when expect is not defined', () => { // eslint-disable-next-line @typescript-eslint/no-explicit-any const originalExpect = globalThis.expect; try { // eslint-disable-next-line @typescript-eslint/no-explicit-any globalThis.expect = undefined; // We can't call expect() after removing it, so we use a try/catch let threw = false; let errorMessage = ''; try { (0, vitest_adapter_1.extendVitestMatchers)(); } catch (e) { threw = true; errorMessage = e.message; } // Restore before asserting // eslint-disable-next-line @typescript-eslint/no-explicit-any globalThis.expect = originalExpect; expect(threw).toBe(true); expect(errorMessage).toContain('ArchUnitTS Vitest Integration Error'); } catch (e) { // eslint-disable-next-line @typescript-eslint/no-explicit-any globalThis.expect = originalExpect; throw e; } }); it('should not throw when force is false and not a vitest project', () => { const originalVitest = process.env.VITEST; delete process.env.VITEST; try { // force=false and no VITEST env var should just return early expect(() => (0, vitest_adapter_1.extendVitestMatchers)(false)).not.toThrow(); } finally { if (originalVitest !== undefined) { process.env.VITEST = originalVitest; } } }); it('should call expect.extend when expect is available', () => { const extendSpy = jest.spyOn(expect, 'extend'); (0, vitest_adapter_1.extendVitestMatchers)(); expect(extendSpy).toHaveBeenCalledWith(expect.objectContaining({ toPassAsync: expect.any(Function), })); extendSpy.mockRestore(); }); describe('toPassAsync matcher', () => { let toPassAsync; beforeEach(() => { const extendSpy = jest.spyOn(expect, 'extend'); (0, vitest_adapter_1.extendVitestMatchers)(); toPassAsync = extendSpy.mock.calls[0][0].toPassAsync; extendSpy.mockRestore(); }); it('should return error when checkable is null', async () => { mockResultFactory.error.mockReturnValue({ pass: false, message: () => 'expected something checkable as an argument for expect()', }); const result = await toPassAsync.call({ isNot: false }, null); expect(mockResultFactory.error).toHaveBeenCalledWith('expected something checkable as an argument for expect()'); expect(result.pass).toBe(false); }); it('should return error when checkable is undefined', async () => { mockResultFactory.error.mockReturnValue({ pass: false, message: () => 'expected something checkable as an argument for expect()', }); await toPassAsync.call({ isNot: false }, undefined); expect(mockResultFactory.error).toHaveBeenCalledWith('expected something checkable as an argument for expect()'); }); it('should process violations and return result', async () => { const mockViolations = [{ type: 'test' }]; const mockCheckable = { check: jest.fn().mockResolvedValue(mockViolations), }; const mockProcessedViolation = { message: 'test', details: {} }; const mockResult = { pass: true, message: jest.fn().mockReturnValue('success'), }; mockViolationFactory.from.mockReturnValue(mockProcessedViolation); mockResultFactory.result.mockReturnValue(mockResult); await toPassAsync.call({ isNot: false }, mockCheckable); expect(mockCheckable.check).toHaveBeenCalled(); expect(mockViolationFactory.from).toHaveBeenCalledWith(mockViolations[0]); expect(mockResultFactory.result).toHaveBeenCalledWith(false, [ mockProcessedViolation, ]); }); it('should pass isNot flag to ResultFactory', async () => { const mockCheckable = { check: jest.fn().mockResolvedValue([]), }; const mockResult = { pass: true, message: jest.fn().mockReturnValue('success'), }; mockResultFactory.result.mockReturnValue(mockResult); await toPassAsync.call({ isNot: true }, mockCheckable); expect(mockResultFactory.result).toHaveBeenCalledWith(true, []); }); it('should pass CheckOptions to checkable.check', async () => { const options = { allowEmptyTests: true }; const mockCheckable = { check: jest.fn().mockResolvedValue([]), }; const mockResult = { pass: true, message: jest.fn().mockReturnValue('success'), }; mockResultFactory.result.mockReturnValue(mockResult); await toPassAsync.call({ isNot: false }, mockCheckable, options); expect(mockCheckable.check).toHaveBeenCalledWith(options); }); }); }); //# sourceMappingURL=vitest-adapter.spec.js.map