@@ -22,7 +22,6 @@ jest.mock('../../src/s3', () => ({
2222 createMultiPartUpload : jest . fn ( ) . mockResolvedValue ( uploadIdMock ) ,
2323} ) )
2424
25- jest . mock ( 'hardhat' )
2625jest . mock ( '../../hardhat.config.ts' , ( ) => ( {
2726 config : {
2827 solidity : '0.8.9' ,
@@ -66,7 +65,23 @@ jest.mock('../../hardhat.config.ts', () => ({
6665 } ,
6766 } ,
6867} ) )
69- // jest.mock('@nomicfoundation/hardhat-network-helpers')
68+ jest . mock ( 'hardhat' , ( ) => ( {
69+ run : jest . fn ( ) ,
70+ ethers : {
71+ provider : {
72+ request : jest . fn ( ) ,
73+ } ,
74+ } ,
75+ config : {
76+ networks : {
77+ hardhat : {
78+ forking : {
79+ url : 'https://eth-mainnet.alchemyapi.io/v2/abcd' ,
80+ } ,
81+ } ,
82+ } ,
83+ } ,
84+ } ) )
7085
7186describe ( 'Unit test for sqs consumer' , function ( ) {
7287 beforeEach ( ( ) => {
@@ -75,15 +90,23 @@ describe('Unit test for sqs consumer', function () {
7590 } )
7691
7792 it ( 'Analyze, upload and create ddb events' , async ( ) => {
93+ global . fetch = jest . fn ( ) . mockResolvedValue ( {
94+ json : jest . fn ( ) . mockResolvedValue ( {
95+ result : {
96+ number : '0x10' ,
97+ } ,
98+ } ) ,
99+ } )
78100 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
79101 // @ts -ignore
80102 hardhat . run = ( ) => ( {
81103 send : ( ) => sampleTraceResult ,
104+ request : jest . fn ( ) . mockResolvedValue ( { } ) ,
82105 } )
83106
84107 const TX_HASH = '0xf2a56c4a9edc31fd3a8ed3c3e256d500f548035e84e55df6e1c6b631d91c04f9'
85108 const CHAIN_ID = '1'
86- const HARDHAT_FORKING_URL = ' https://eth-mainnet.alchemyapi.io/v2/abcd'
109+ const HARDHAT_FORKING_URL = 'https://eth-mainnet.alchemyapi.io/v2/abcd'
87110 const testEvent = createSQSRecordEvent ( TX_HASH , CHAIN_ID , HARDHAT_FORKING_URL , '4444' )
88111 const bucketName = 'www.bucket.com'
89112 process . env . ANALYZER_DATA_BUCKET_NAME = bucketName
@@ -109,15 +132,28 @@ describe('Unit test for sqs consumer', function () {
109132 expect ( getMockCalledInputItem ( ddbMock , 0 ) . status ) . toEqual ( TransactionTraceResponseStatus . RUNNING )
110133 expect ( getMockCalledInputItem ( ddbMock , 1 ) . status ) . toEqual ( TransactionTraceResponseStatus . SUCCESS )
111134 expect ( getMockCalledInputItem ( ddbMock , 1 ) . s3Location ) . toEqual ( `${ bucketName } /trace/${ CHAIN_ID } /${ TX_HASH } .json` )
135+ expect ( fetch ) . toHaveBeenCalledWith ( HARDHAT_FORKING_URL , {
136+ method : 'POST' ,
137+ body : JSON . stringify ( { params : [ 'latest' , false ] , method : 'eth_getBlockByNumber' , jsonrpc : '2.0' , id : 1 } ) ,
138+ } )
112139 } )
113140
114141 it ( 'Add fail event in case of the error' , async ( ) => {
115142 const ERROR_MSG = 'Sample error during hardhat run'
143+ global . fetch = jest . fn ( ) . mockResolvedValue ( {
144+ json : jest . fn ( ) . mockResolvedValue ( {
145+ result : {
146+ number : '0x10' ,
147+ } ,
148+ } ) ,
149+ } )
116150 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
117151 // @ts -ignore
118152 hardhat . run = ( ) => ( {
119153 send : jest . fn ( ) . mockRejectedValue ( new Error ( ERROR_MSG ) ) ,
154+ request : jest . fn ( ) . mockResolvedValue ( { } ) ,
120155 } )
156+
121157 const TX_HASH = '0xf2a56c4a9edc31fd3a8ed3c3e256d500f548035e84e55df6e1c6b631d91c04f9'
122158 const CHAIN_ID = '1'
123159 const HARDHAT_FORKING_URL = ' https://eth-mainnet.alchemyapi.io/v2/abcd'
0 commit comments