Skip to content

Commit a714dc4

Browse files
committed
Add JestExtensions toFailRequire() without message + update relevant tests
1 parent 9b1d858 commit a714dc4

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

packages/cashscript/src/test/JestExtensions.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ declare global {
1010
interface Matchers<R> {
1111
toLog(value?: RegExp | string): Promise<void>;
1212
toFailRequireWith(value: RegExp | string): Promise<void>;
13+
toFailRequire(): Promise<void>;
1314
}
1415
}
1516
}
@@ -57,7 +58,7 @@ expect.extend({
5758
await transaction.debug();
5859

5960
const matcherHint = this.utils.matcherHint('.toFailRequireWith', undefined, match.toString(), { isNot: this.isNot });
60-
const message = (): string => `${matcherHint}\n\nContract function did not fail a require statement`;
61+
const message = (): string => `${matcherHint}\n\nContract function did not fail a require statement.`;
6162
return { message, pass: false };
6263
} catch (transactionError: any) {
6364
const matcherHint = this.utils.matcherHint('toFailRequireWith', 'received', 'expected', { isNot: this.isNot });
@@ -73,4 +74,18 @@ expect.extend({
7374
}
7475
}
7576
},
77+
async toFailRequire(
78+
this: MatcherContext,
79+
transaction: Transaction,
80+
): Promise<SyncExpectationResult> {
81+
try {
82+
await transaction.debug();
83+
const message = (): string => 'Contract function did not fail a require statement.';
84+
return { message, pass: false };
85+
} catch (transactionError: any) {
86+
const receivedText = `Received string: ${this.utils.printReceived(transactionError?.message ?? '')}`;
87+
const message = (): string => `Contract function failed a require statement.\n${receivedText}`;
88+
return { message, pass: true };
89+
}
90+
},
7691
});

packages/cashscript/test/debugging.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ describe('Debugging tests', () => {
450450
provider.addUtxo(contract.address, randomUtxo());
451451

452452
const transaction = contract.functions.test_require_no_failure().to(contract.address, 1000n);
453-
await expect(transaction).not.toFailRequireWith(/.*/);
453+
await expect(transaction).not.toFailRequire();
454454
});
455455

456456
// test_multiple_require_statements_no_message_final
@@ -721,7 +721,7 @@ describe('Debugging tests', () => {
721721
provider.addUtxo(contract.address, randomUtxo());
722722

723723
const transaction = contract.functions.test_zero_handling(0n).to(contract.address, 1000n);
724-
await expect(transaction).not.toFailRequireWith(/.*/);
724+
await expect(transaction).not.toFailRequire();
725725
});
726726
});
727727

@@ -807,8 +807,8 @@ describe('Debugging tests', () => {
807807
).rejects.toThrow(/Expected pattern: not .*1 should equal 2.*\nReceived string: (.|\n)*?1 should equal 2/);
808808

809809
await expect(
810-
expect(transaction).not.toFailRequireWith(/.*/),
811-
).rejects.toThrow(/Expected pattern: not .*\nReceived string: (.|\n)*?1 should equal 2/);
810+
expect(transaction).not.toFailRequire(),
811+
).rejects.toThrow(/Contract function failed a require statement\.*\nReceived string: (.|\n)*?1 should equal 2/);
812812
});
813813
});
814814
});

0 commit comments

Comments
 (0)