@@ -10,21 +10,30 @@ import type {
1010 VerifyCaseJestCallback ,
1111} from './types.js' ;
1212
13+ /**
14+ * @internal
15+ * The timeout to use to override jest's default timeout.
16+ *
17+ * If you update this, make sure you update the comments in {@link defineContract} and {@link verifyContract}.
18+ */
1319const TIMEOUT = 30000 ;
20+
1421/**
1522 * Convenience wrapper for defining contracts
1623 *
1724 * @param config - Configuration for this definition run (may be overridden in individual tests)
18- * @param callback - The test definitions
25+ * @param setupCallback - The test definitions
1926 * @param onContractDefineSuccess - An optional callback to call if the contract was written successfully
27+ * @param timeoutMillis - An optional timeout to set for jest's hooks and tests. Defaults to 30000
2028 * @returns
2129 */
2230export const defineContract = (
2331 config : ContractCaseJestConfig ,
24- callback : DefineCaseJestCallback ,
32+ setupCallback : DefineCaseJestCallback ,
2533 onContractDefineSuccess : (
2634 details : ContractWriteSuccess ,
2735 ) => Promise < void > | void = ( ) => { } ,
36+ timeoutMillis : number = TIMEOUT ,
2837) : void =>
2938 describe ( `Case contract definition` , ( ) => {
3039 const contract = new ContractCaseDefiner ( {
@@ -33,25 +42,42 @@ export const defineContract = (
3342 ...config ,
3443 } ) ;
3544
36- afterAll ( ( ) => contract . endRecord ( ) . then ( onContractDefineSuccess ) , TIMEOUT ) ;
45+ afterAll (
46+ ( ) => contract . endRecord ( ) . then ( onContractDefineSuccess ) ,
47+ timeoutMillis ,
48+ ) ;
3749
3850 describe ( `between ${ config . consumerName } and ${ config . providerName } ` , ( ) => {
39- jest . setTimeout ( TIMEOUT ) ;
51+ jest . setTimeout ( timeoutMillis ) ;
4052
41- callback ( contract ) ;
53+ setupCallback ( contract ) ;
4254 } ) ;
4355 } ) ;
4456
57+ /**
58+ * Convenience wrapper for verifying contracts. Calling this will generate all the tests
59+ * you need for
60+ *
61+ * @param config - Configuration for this verification run
62+ * @param setupCallback - Optional. Called with the contract verifier before any tests are run. You can use this to do any
63+ * computed setup necessary
64+ * @param verificationCompleteCallback - Optional. Called once the verification is complete. In a future release, this will contain the details
65+ * of the verification success / failure.
66+ * @param timeoutMillis - An optional timeout to set for jest's hooks and tests. Defaults to 30000
67+ * @returns
68+ */
4569export const verifyContract = (
4670 config : ContractCaseJestVerifierConfig ,
4771 setupCallback : VerifyCaseJestCallback = ( ) => { } ,
72+ verificationComplete = ( ) => { } ,
73+ timeoutMillis : number = TIMEOUT ,
4874) : void => {
4975 if ( ! config . providerName ) {
5076 throw new Error ( 'Must specify a providerName to verify' ) ;
5177 }
5278 describe ( `Provider verification for ${ config . providerName } ` , ( ) => {
5379 const verifier = new ContractVerifier ( config ) ;
54- jest . setTimeout ( TIMEOUT ) ;
80+ jest . setTimeout ( timeoutMillis ) ;
5581
5682 setupCallback ( verifier ) ;
5783
@@ -63,6 +89,14 @@ export const verifyContract = (
6389 } ) ;
6490 // TODO: Determine whether Jest runs tests in order always, and if not, do something else here.
6591 it ( 'Overall verification result' , ( ) =>
66- verifier . closePreparedVerification ( ) ) ;
92+ verifier . closePreparedVerification ( ) . then (
93+ ( ) => {
94+ verificationComplete ( ) ;
95+ } ,
96+ ( e ) => {
97+ verificationComplete ( ) ;
98+ throw e ;
99+ } ,
100+ ) ) ;
67101 } ) ;
68102} ;
0 commit comments