3 min read

Primitives Tests - Hardhat

Overview

Let's create some tests to verify the default values of Solidity's language primitives. In this section, we are using Hardhat to create our tests. If you haven't set up Hardhat correctly, check out our Hardhat Configuration guide. If you'd like to use Foundry or would like to compare both development environments, follow the guide below.

  1. First let's create a folder to hold our tests and lessons. Navigate to the directory (folder) of your choice. Then, create a new directory.
mkdir Solidity101-Hardhat
  1. Next let's create a Hardhat project. When prompted select create a Javascript project, create a .gitignore, and install the prompted dependencies.
npx hardhat init
  1. Now, let's delete the default contracts, tests, and deploy scripts.
rm contract/Lock.sol test/Lock.js scripts/deploy.js
  1. Create the files first test for our primitives overview.
touch contracts/Primitives.sol test/Primitives.test.js
  1. Add the following code to contracts/Primitives.sol
// Primitives.sol
pragma solidity ^0.8.16;

contract Primitives {
  // public variables have automatic getter functions
  bool public storedBool;  // default: false
  int public storedInt;    // default: 0 
  uint public storedUint;  // default: 0
  address public storedAddress; // default address: 0x0000000000000000000000000000000000000000
  bytes public storedBytes;     // default: 0x
  string public storedString;   // default: empty stiring aka ""
  mapping(address => uint) public balances;   // 0x0..00 address => 0 uint

  enum State { 
		PENDING, 
		APPROVED, REJECTED 
	} // creates enum structure
	
  State public state; // creates instance of enum
}
  1. Add the following code to test/Primitives.test.js
// Import the expect function from Chai for making assertions
const { expect } = require('chai');

// Import the ethers library from Hardhat for interacting with the Ethereum network
const { ethers } = require('hardhat');

// Define a test suite using the describe function from Mocha
describe('Checking lesson 1: Primitives Defaults', function () {
  let primitives;

  // Use the beforeEach function from Mocha to create a new instance of the Primitives contract before each test
  beforeEach(async function () {
    const Primitives = await ethers.getContractFactory('Primitives');
    primitives = await Primitives.deploy();
    await primitives.deployed();
  });

  // Define a test case using the it function from Mocha to check the storedBool variable
  it('bool should be false ', async function () {
    expect(await primitives.storedBool()).to.be.false;
  });

  // Define a test case to check the storedInt variable
  it('int should be equal to 0', async function () {
    expect(await primitives.storedInt()).to.equal(0);
  });
  
  // Define a test case to check the storedUint variable
  it('uint should be equal to 0', async function () {
    expect(await primitives.storedUint()).to.equal(0);
  });
  
  // Define a test case to check the storedAddress variable
  it('address should be equal to default constant zero address', async function () {
    expect(await primitives.storedAddress()).to.equal(ethers.constants.AddressZero);
  });
  
  // Define a test case to check the storedBytes variable
  it('bytes should be equal to default 0x', async function () {
    expect(await primitives.storedBytes()).to.equal('0x');
  });
  
  // Define a test case to check the storedString variable
  it('string should be equal to default empty string \'\'', async function () {
    expect(await primitives.storedString()).to.equal('');
  });

  // Define a test case to check the State enum
  it('enum should be equal to default first element index', async function () {
    expect(await primitives.state()).to.equal(0);
  });

  // Define a test case to check the balances mapping
  it('balance should be equal to zero', async function () {
    expect(await primitives.balances(ethers.constants.AddressZero)).to.equal(0);
  });
});

Hardhat Tests Explained

The tests themselves are fairly simple. Each test uses the expect function from Chai to make assertions about the value of a particular variable in the Primitives contract. The beforeEach function is used to create a new instance of the Primitives contract before each test, so that the tests can run independently of each other.

To run these tests using Hardhat, you can use the following command:

npx hardhat test

This will compile the Primitives contract and the Primitives.test.js test file, and then run all of the tests in the file.

You should see something like this:

  Checking lesson 1: Primitives Defaults
    ✔ bool should be false 
    ✔ int should be equal to 0
    ✔ uint should be equal to 0
    ✔ address should be equal to default constant zero address
    ✔ bytes should be equal to default 0x
    ✔ string should be equal to default empty string ''
    ✔ enum should be equal to default first element index
    ✔ balance should be equal to zero


  8 passing (684ms)

Feedback

Have feedback? Found something that needs to be updated, accurate, or explained?

Join our discord and share what can be improved.

Any and all feedback is welcomed.