New Ethereum talks, every Monday. The week's conference uploads by event, in your inbox.

Loading player…

Writing Tests in Hardhat

BaseMon, Oct 9, 2023, 12:00 AM

Write solid tests for your smart contracts. Discover how to utilize the powerful tools and plugins like Chai matchers and ethers.js to ensure your contract functions work as intended. Verify events and parameters with confidence, making your smart contracts secure and reliable. Get started now for more robust and error-free deployments! 📚 Head to Base Camp for full courses on smart contract development: https://docs.base.org/base-camp/docs/welcome ✅ Subscribe to our channel here: https://www.youtube.com/@BuildonBase ► Build on Base: https://base.org ► Follow us on Twitter: https://twitter.com/buildonbase ► Join our Discord: https://base.org/discord

Transcript

in this video we're going to write a test for our deposit function that we put together in the last video so we already have the skeleton for that uh and what we can do here is we can take advantage of some of those plugins and tools that we installed when we created our project in particular the toolkit the chai matchers and a package called ethers that's from hard hat but it's actually a part of ethers.js which is a popular library that lets you write JavaScript and typescript that interact with the blockchain you'll also use that later on when you're writing your front end if we go down um we can take advantage of our instance of the lock contract and we can call our deposit function um and it doesn't need any arguments as we can see right here but uh with ethers the way that you add payable is you can put in some additional parameters and arguments in here and we will add a value of one all right but we need to wrap that around a test so we're going to await for this function to complete and we will expect well what are we going to expect we could expect a lot of different things one of the things that we could do is we could expect a value to be equal to another value but our function doesn't actually return anything so that won't work one of the simplest built-in ones that we can try is we can expect our function to not be reverted so as long as it doesn't work this will pass and it'll be good to go we can save and we can run that along with all of our other tests with npx hard hat test let it run through and you can see that we have all 10 passing including our new test uh which is right here should allow additional deposits but we don't actually know whether or not it worked all we know is that it didn't didn't revert it didn't cause a panic or anything like that another thing that we could test is our event and we set our function to emit an event at the end and the way that we can do that is by calling expect our lock contracts deposit function to emit and the emit requires the contract is a parameter so we'll put lock in here again and the event name as a string is deposit and it is very important that you do that exactly right otherwise it won't work and we can even check not only that the event happened but what arguments were with it so we'll say with args and we expect a value of one um but we don't know what the time stamp is going to be uh and we could and sometimes might need to go through gymnastics to try and figure out what the time stamp is um and figure out that that's what was expected but we don't really need it here the timestamp is just informational uh and so we'll take advantage of a built-in Little Helper of any value so this will let it pass with any value so we'll save that run it again and it took a little faster this time because we didn't need to compile and we'll go and take a look and we can see that should allow additional deposits still works right so that was a quick introduction into writing tests thank you for listening

Automatic transcript — names and jargon may be misspelled.