|
| 1 | +import { DebugResults } from '../debugging.js'; |
| 2 | +import { vi, expect } from 'vitest'; |
| 3 | + |
| 4 | +interface Debuggable { |
| 5 | + debug(): DebugResults | Promise<DebugResults>; |
| 6 | +} |
| 7 | + |
| 8 | +interface ExpectationResult { |
| 9 | + pass: boolean |
| 10 | + message: () => string |
| 11 | + // If you pass these, they will automatically appear inside a diff when |
| 12 | + // the matcher does not pass, so you don't need to print the diff yourself |
| 13 | + actual?: unknown |
| 14 | + expected?: unknown |
| 15 | +} |
| 16 | + |
| 17 | +export function toLog( |
| 18 | + // FIXME: improve type definition |
| 19 | + this: any, |
| 20 | + transaction: Debuggable, |
| 21 | + match?: RegExp | string, |
| 22 | +): ExpectationResult { |
| 23 | + const loggerSpy = vi.spyOn(console, 'log'); |
| 24 | + const { utils, isNot } = this; |
| 25 | + |
| 26 | + // Clear any previous calls (if spy reused accidentally) |
| 27 | + loggerSpy.mockClear(); |
| 28 | + |
| 29 | + // silence actual stdout output |
| 30 | + loggerSpy.mockImplementation(() => { }); |
| 31 | + |
| 32 | + try { |
| 33 | + executeDebug(transaction); |
| 34 | + } catch (error) { |
| 35 | + if (error instanceof OldTransactionBuilderError) throw error; |
| 36 | + } |
| 37 | + |
| 38 | + // We concatenate all the logs into a single string - if no logs are present, we set received to undefined |
| 39 | + const receivedBase = loggerSpy.mock.calls.reduce((acc, [log]) => `${acc}\n${log}`, '').trim(); |
| 40 | + const received = receivedBase === '' ? undefined : receivedBase; |
| 41 | + |
| 42 | + const matcherHint = utils.matcherHint('toLog', 'received', 'expected', { isNot: isNot }); |
| 43 | + const expectedText = `Expected: ${isNot ? 'not ' : ''}${utils.printExpected(match)}`; |
| 44 | + const receivedText = `Received: ${utils.printReceived(received)}`; |
| 45 | + const message = (): string => `${matcherHint}\n\n${expectedText}\n${receivedText}`; |
| 46 | + |
| 47 | + try { |
| 48 | + // We first check if the expected string is present in any of the individual console.log calls |
| 49 | + expect(loggerSpy).toHaveBeenCalledWith(match ? expect.stringMatching(match) : expect.anything()); |
| 50 | + } catch { |
| 51 | + try { |
| 52 | + // We add this extra check to allow expect().toLog() to check multiple console.log calls in a single test |
| 53 | + // (e.g. for log ordering), which would fail the first check because that compares the individual console.log calls |
| 54 | + expect(receivedBase).toMatch(match ? match : expect.anything()); |
| 55 | + } catch { |
| 56 | + return { message, pass: false }; |
| 57 | + } |
| 58 | + } finally { |
| 59 | + // Restore the original console.log implementation |
| 60 | + loggerSpy.mockRestore(); |
| 61 | + } |
| 62 | + |
| 63 | + return { message, pass: true }; |
| 64 | +} |
| 65 | + |
| 66 | +export function toFailRequireWith( |
| 67 | + this: any, |
| 68 | + transaction: Debuggable, |
| 69 | + match: RegExp | string, |
| 70 | +): ExpectationResult { |
| 71 | + const { utils, isNot } = this; |
| 72 | + try { |
| 73 | + executeDebug(transaction); |
| 74 | + |
| 75 | + const matcherHint = utils.matcherHint('.toFailRequireWith', undefined, match.toString(), { isNot: isNot }); |
| 76 | + const message = (): string => `${matcherHint}\n\nContract function did not fail a require statement.`; |
| 77 | + return { message, pass: false }; |
| 78 | + } catch (transactionError: any) { |
| 79 | + if (transactionError instanceof OldTransactionBuilderError) throw transactionError; |
| 80 | + |
| 81 | + const matcherHint = utils.matcherHint('toFailRequireWith', 'received', 'expected', { isNot: isNot }); |
| 82 | + const expectedText = `Expected pattern: ${isNot ? 'not ' : ''}${utils.printExpected(match)}`; |
| 83 | + const receivedText = `Received string: ${utils.printReceived(transactionError?.message ?? '')}`; |
| 84 | + const message = (): string => `${matcherHint}\n\n${expectedText}\n${receivedText}`; |
| 85 | + |
| 86 | + try { |
| 87 | + expect(transactionError?.message ?? '').toMatch(match); |
| 88 | + return { message, pass: true }; |
| 89 | + } catch { |
| 90 | + return { message, pass: false }; |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +export function toFailRequire( |
| 97 | + this: any, |
| 98 | + transaction: Debuggable, |
| 99 | +): ExpectationResult { |
| 100 | + const { utils } = this; |
| 101 | + try { |
| 102 | + executeDebug(transaction); |
| 103 | + const message = (): string => 'Contract function did not fail a require statement.'; |
| 104 | + return { message, pass: false }; |
| 105 | + } catch (transactionError: any) { |
| 106 | + if (transactionError instanceof OldTransactionBuilderError) throw transactionError; |
| 107 | + |
| 108 | + const receivedText = `Received string: ${utils.printReceived(transactionError?.message ?? '')}`; |
| 109 | + const message = (): string => `Contract function failed a require statement.\n${receivedText}`; |
| 110 | + return { message, pass: true }; |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | + |
| 115 | +// Wrapper function with custom error in case people use it with the old transaction builder |
| 116 | +// This is a temporary solution until we fully remove the old transaction builder from the SDK |
| 117 | +const executeDebug = (transaction: Debuggable): void => { |
| 118 | + const debugResults = transaction.debug(); |
| 119 | + |
| 120 | + if (debugResults instanceof Promise) { |
| 121 | + debugResults.catch(() => { }); |
| 122 | + throw new OldTransactionBuilderError(); |
| 123 | + } |
| 124 | +}; |
| 125 | + |
| 126 | +class OldTransactionBuilderError extends Error { |
| 127 | + constructor() { |
| 128 | + super('The CashScript VitestExtensions do not support the old transaction builder since v0.11.0. Please use the new TransactionBuilder class.'); |
| 129 | + this.name = 'OldTransactionBuilderError'; |
| 130 | + } |
| 131 | +} |
0 commit comments