Check out a free preview of the full A Tour of Web 3: Ethereum & Smart Contracts with Solidity course

The "Setup & Deploy to Test Network" Lesson is part of the full, A Tour of Web 3: Ethereum & Smart Contracts with Solidity course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen installs the necessary testing dependencies and writes the unit test for the HelloWorld contract. The test checks to make sure the hello() method in the contract returns the string "Hello World".


Transcript from the "Setup & Deploy to Test Network" Lesson

>> So we have just compiled. What do we do now? Well, does anyone have any ideas? No, probably not. Because it doesn't make any sense. You've just compiled that how to even run thing. Well, instead of running it, let's first test it. Let's make sure it does the thing we want it to do.

So I've added three separate statements of yarn install links. Because remember, high scores people high scores we need these. So if you have the site, hopefully you do, you got to go here and cut and paste each one of these ends. We're gonna be adding nomiclabs hardhat-ethers and waffle.

Those will help us a whole bunch. Nomiclabs, build these nice. Like little NPM packages that will kind of make the experience of working with hardhat, even better, it just layers in some extra functions. You'll see what we're doing. And so there we go. Pretty awesome. We're gonna add this, but you know what, I've been having the strangest problem every time I do this, it I will reinstall TS note here in a moment you'll see it.

I just got done doing it you can see that I did it and now, we're gonna have to do it again. I guarantee you and then we're gonna do this and this is for testing unfortunately the testing platform they really support chai. I didn't wanna have to go through the process of using jest and trying to figure out how to make these two play together.

So I just said forget about it, I'd rather just use what they have because the testing will be fairly simple. And just makes life a little bit easier. But you do have to use that chai based testing we have to be like, this value shouldn't be. If I feel this way, you have to do that long chaining.

You'll see it's very uncomfortable. And of course, before we write any tests, we need to do two more things. Let's take our hardhat.config and rename it from hardhat.config.JavaScript to TypeScript, or else you can't run TypeScript based tests. I know great. And then you have to add in one more thing.

So let's first go to the hardhat.config. And let's just rename that to TypeScript. And then of course, we have to add in this import statement, hardhat.waffle. If you don't do that, it will not work for TypeScript and I just cannot do, I just can't do JavaScript. It's emotionally painful language to work with TypeScript at least makes it fairly okay.

And so now that we have that, we can write our test. Everything's been set up to write tests with TypeScript. And so let's write our first test. So remember, our folder style should be a test directory. And then we can write our test. So let's go here and create a test directory.

And then inside of it, you can call it whatever you want. I'll call it HelloWorld.ts to make it easy. We're gonna import the first thing as nomic labs, hardhat-ethers. You don't actually have to save anything for this one. This one just simply adds a bunch of convenient methods, to the ethers library.

You'll get more familiar with the ethers library as we go on. But for now, I just always import this. You don't technically have to import this. But I always have these problems with, like the TypeScript language server. That if I don't do it in the file, it won't actually run it.

Because what happens when you run hardhat, is just like WebPacker and in these other kind of programs that you can run. They run their config file, which will alter your environment and add things to the global. And then you have these other files that are gonna be ran in that perspective.

And then you have this like, TypeScript can't find these certain methods hanging off these objects. Because it doesn't understand that it's actually gonna be mutated by a library. So I just always intentionally add this to every file, so that I have a very nice language server experience. Alright, awesome.

So let's go back here. And so now that we've imported that, we're gonna import ethers from hardhat. So as I said, this nomiclab adds this nice little ethers wrapper around ethers. It just adds a few extra convenient methods. And so after we've done that we just need to do one last thing which is we need expect from chai.

There we go chai and that's gonna be how we're gonna do our testing. And then we're just gonna do a little quick describe of hello world. There we go, function do one of those it should say hi. I've never been good at naming test. I always want to be better but I'm not gonna just, I feel like I should type this nice sentence and then get too long never know what to do here.

So there we go, right away, I'm sure everyone that's really hip into the JavaScript are like man you're not using arrow functions. What's that all about? I don't use arrow functions unless if I lexically, I'm using the this object. It actually has semantic meaning underneath the hood and the compiler does do something different.

So I really do try to give the best hints to the compiler at all times while coding. It's just one of my personal things that I do performance and it just drives me nuts. Seeing people do that. I don't know why. It probably has no meaning, but for me, it just has a lot of meaning.

It drives me crazy. All right, so when we're gonna do testing, we need to do three things. We need to first set up our environment, which we just did, right? We have hardhat-ethers, we have hardhat. We're looking good. Two, we need to deploy our contract. We'll do that here shortly.

And then three, we need to call our functions to task, right? So I'm pretty sure everyone kinda saw that coming. So step one we've already done. So let's do two let's deploy our contract. Now, as I told you nomic labs add some pretty nice little functions to the ether object.

So let's grab our contract factory. Now, this normally doesn't exist, but with this extension it does. And so the standard naming convention I've seen is that you do it in capital letters as if it was like a floating class, right? So const HelloWorld equals await ethers.getContractFactory up is set at a factory, and then you can just give it the name that you named it.

And remember it's not the name of the file, it's the name of the contract. What effectively is gonna happen here? I think I may have a note for me to explain this later, but just in case I don't. What happens here is that hardhat has compiled your contract.

It made a JSON file filled with a bunch of information. In fact, we can even go look at said, JSON file. And so I'm gonna cat out artifacts contracts. HelloWorld.sol, HelloWorld.json and I'm gonna pipe that to JQ. And so there you go, you can see the deployed Bytecode.

You can see just the Bytecode. You can see what functions exist on there, I can see that we have a function called Hello. It returns type string, state immutability is pure and it's type function. The contract name is this. This is where it's located. There you go. So you can actually see a lot about your contract and this is really how it generates all the code that's needed for us to be able to deploy it.

Because it needs to be able to store the Bytecode somewhere. Because the bike code's clearly not the language we wrote in. It's some sort of intermediate format that can be understood by the EVM the Ethereum Virtual Machine. So there we go. That's what that step does. Raise the file make sure everything's in place.

So now we can do the next step which is like this const Hello equals await HelloWorld.deploy. So this is gonna deploy it to a network, meaning it's gonna make the transaction that actually puts a contract on to an ethereum network. It can be a test network. It could be the Rinkeby network.

It could be the main net, but then you're spending hundreds of dollars. So make sure you know what network we're actually gonna be going to. And then lastly, we can go await hello.deployed. Nice little method right here. It just ensures that your contract has been confirmed by the network enough times, that it's considered on the network.

Because remember, when you do something in ethereum, it's kinda like Cassandra, it's an eventually consistent machine. And so don't happen instantaneously. There has to be consensus and agreement among the nodes. So now, we can just do a simple expect await hello.hello. And then to equal yes, there's that whole sentence thing, Hello, World.

There we go, awesome. So now we have our first test done and kind of our first interaction with the contract or with the network, done. And so what we're gonna do now, is I'm gonna scroll through this and make sure I'm kinda doing things in the right order.

We're gonna go npx hardhat test. It's gonna go to your test folder, it's going to execute things long as you've renamed hardhat config JSON or JavaScript to TypeScript. And included the waffle, the nomiclabs waffle, you will be able to run it in TypeScript. There we go. But again, as I've said, I still don't understand why it's not just saving this yarn TS node thing.

Yarn add give them the D, ts node, boom! There we go. I'm gonna redo I just did this right, I just did this. All right, so I'm gonna re execute it again. And it's gonna go through. And look at that, we passed.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now