81 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			81 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import { expect as smartExpect } from '@push.rocks/smartexpect';
 | |
| import { generateDiff } from './tapbundle.utilities.diff.js';
 | |
| import { ProtocolEmitter } from '../dist_ts_tapbundle_protocol/index.js';
 | |
| import type { IEnhancedError } from '../dist_ts_tapbundle_protocol/index.js';
 | |
| 
 | |
| // Store the protocol emitter for event emission
 | |
| let protocolEmitter: ProtocolEmitter | null = null;
 | |
| 
 | |
| /**
 | |
|  * Set the protocol emitter for enhanced error reporting
 | |
|  */
 | |
| export function setProtocolEmitter(emitter: ProtocolEmitter) {
 | |
|   protocolEmitter = emitter;
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * Enhanced expect wrapper that captures assertion failures and generates diffs
 | |
|  */
 | |
| export function createEnhancedExpect() {
 | |
|   return new Proxy(smartExpect, {
 | |
|     apply(target, thisArg, argumentsList: any[]) {
 | |
|       const expectation = target.apply(thisArg, argumentsList);
 | |
|       
 | |
|       // Wrap common assertion methods
 | |
|       const wrappedExpectation = new Proxy(expectation, {
 | |
|         get(target, prop, receiver) {
 | |
|           const originalValue = Reflect.get(target, prop, receiver);
 | |
|           
 | |
|           // Wrap assertion methods that compare values
 | |
|           if (typeof prop === 'string' && typeof originalValue === 'function' && ['toEqual', 'toBe', 'toMatch', 'toContain'].includes(prop)) {
 | |
|             return function(expected: any) {
 | |
|               try {
 | |
|                 return originalValue.apply(target, arguments);
 | |
|               } catch (error: any) {
 | |
|                 // Enhance the error with diff information
 | |
|                 const actual = argumentsList[0];
 | |
|                 const enhancedError: IEnhancedError = {
 | |
|                   message: error.message,
 | |
|                   stack: error.stack,
 | |
|                   actual,
 | |
|                   expected,
 | |
|                   type: 'assertion'
 | |
|                 };
 | |
|                 
 | |
|                 // Generate diff if applicable
 | |
|                 if (prop === 'toEqual' || prop === 'toBe') {
 | |
|                   const diff = generateDiff(expected, actual);
 | |
|                   if (diff) {
 | |
|                     enhancedError.diff = diff;
 | |
|                   }
 | |
|                 }
 | |
|                 
 | |
|                 // Emit assertion:failed event if protocol emitter is available
 | |
|                 if (protocolEmitter) {
 | |
|                   const event = {
 | |
|                     eventType: 'assertion:failed' as const,
 | |
|                     timestamp: Date.now(),
 | |
|                     data: {
 | |
|                       error: enhancedError
 | |
|                     }
 | |
|                   };
 | |
|                   console.log(protocolEmitter.emitEvent(event));
 | |
|                 }
 | |
|                 
 | |
|                 // Re-throw the enhanced error
 | |
|                 throw error;
 | |
|               }
 | |
|             };
 | |
|           }
 | |
|           
 | |
|           return originalValue;
 | |
|         }
 | |
|       });
 | |
|       
 | |
|       return wrappedExpectation;
 | |
|     }
 | |
|   });
 | |
| }
 | |
| 
 | |
| // Create the enhanced expect function
 | |
| export const expect = createEnhancedExpect(); |