Closed claudioantonio closed 3 years ago
Hi Claudio
Mocking window.ethereum
is literally what happens in the background:
https://github.com/DePayFi/depay-web3mock/blob/main/src/blockchains/ethereum/index.js#L23
If the exception asks you to mock the contract, maybe try to mock the contract.
If you could share test code and the exception, I might be able to help you out.
And if you initialize web3mock you need to actually mock all your contract calls (yes all of them) explicitly as otherwise you will see exceptions being raised that it's not mocked yet.
Hi Sebastian! Thank You very much for Your reply.
The picture below will give You an overview of my DApp and I think It will make more clear what I want to do.
My interest is to test if my services (yellow box) can communicate with the chain, call the real smart contracts and receive the expected events/values from them. In this case, It's not interesting to me to do a mock for my contracts. 😞
As You can see in the picture, my services receive a initialized window.ethereum instance that is wrapped by Ethers. I was wondering If I could setup a mock for window.ethereum (provider object created by MetaMask) with the location of my network and some accounts in my test case and then give this mock object to the service to be tested.
The mock for window.ethereum I'm interested in should make it possible to ethers library connect to the network where my contracts are deployed, instead of simulate interaction with those contracts.
Do You think depay-web3mock can help me?
Best regards!
We know from web2 to mock application requests to other systems on the side you can control (so your application). So in web2 you've mocked HTTP requests to other systems and not the other system itself.
Web3Mock follows the same best practice to mock the request to the other system (via window.ethereum) rather than running the other system.
Especially because running the other system in Web3 means running a local blockchain which comes with huge overhead and can give you quite some headache.
If your service accesses window.ethereum
after you have initialized mock()
it will already access the mocked window.ethereum.
In case you need to pass providers around explicitly you can do that with web3mock, too.
See here: https://github.com/DePayFi/depay-web3mock#mock-for-specific-providers
Then you just forward the initialized provider to your service after you have initialized the mock with mock({ provider , ... })
Hi Sebastian! We're currently testing our DApp more likely e2e. 😉
If your service accesses window.ethereum after you have initialized mock() it will already access the mocked window.ethereum.
Yeah! I did it, but when I started to call my contracts I received the exception I mentioned before asking me to mock my contracts.
We decided to initialize our services with a provider. So during the normal operation (user interaction) the services will use the window.ethereum from MetaMask and during the e2e tests the service will use a JsonRpcPRovider pointing to a running network.
I want to thank You a lot and I will put depay-web3mock in my toolbox for use near in the future!
Thanks for giving me the insight. I've just realized that's your own contracts you're testing during e2e.
I think then makes e2e more sense, as you want to test the contracts themselves, too.
Missed that earlier 🙏
Hi everybody! I have a Web DApp that uses MetaMask. This Web Dapp has services written in ethers to make calls to the smart contracts. I want to test if our services are calling the deployed contracts and receiving the expected results. I don't want to mock my contracts. I just want to mock the object window.ethereum (point it to my localnetwork and define some accounts). Is It possible to do it using depay-web3mock?
I did a quick test but I receive a exception asking me to mock my contracts. :(
Best regards and congrats!