Skip to content

Commit

Permalink
hardhat e2e tests (#1030)
Browse files Browse the repository at this point in the history
* hardhat e2e tests

* fix timeout

* fix

* add proxy test
  • Loading branch information
shunjizhan authored Sep 30, 2024
1 parent 12cc4ed commit 29e56bb
Show file tree
Hide file tree
Showing 16 changed files with 785 additions and 88 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,6 @@ lib
## yarn cache
**/.yarn/cache
**/.yarn/install-state.gz

## openzeppelin cache
**/.openzeppelin/
63 changes: 2 additions & 61 deletions e2e-tests/e2e-hardhat/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,4 @@
module.exports = {
env: {
node: true,
},
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint',
'import',
'sort-imports-es6-autofix',
],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:import/recommended',
'plugin:import/typescript',
],
rules: {
indent: [2, 2, { SwitchCase: 1 }],
quotes: [2, 'single'],
semi: [1, 'always'],
'no-trailing-spaces': [2],
'quote-props': [2, 'as-needed'],
'eol-last': [2, 'always'],
'object-curly-spacing': [2, 'always'],
'comma-dangle': [2, {
arrays: 'always-multiline',
objects: 'always-multiline',
imports: 'always-multiline',
exports: 'always-multiline',
functions: 'only-multiline',
}],

/* ---------- turn off ---------- */
'@typescript-eslint/no-extra-semi': 0,
'@typescript-eslint/no-use-before-define': 0,
'@typescript-eslint/explicit-member-accessibility': 0,
'@typescript-eslint/naming-convention': 0,
'@typescript-eslint/no-explicit-any': 0, // any is sometimes unavoidable
'@typescript-eslint/consistent-type-definitions': 0, // can use Type and Interface
'@typescript-eslint/explicit-function-return-type': 0, // type inference on return type is useful
'@typescript-eslint/no-parameter-properties': 0,
'@typescript-eslint/typedef': 0,
'no-unused-expressions': 0, // short ciucuit if
'max-lines': 0,
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'sort-imports-es6-autofix/sort-imports-es6': 'warn',
'@typescript-eslint/ban-ts-comment': 'off',
'no-useless-escape': 'off',
'@typescript-eslint/no-non-null-asserted-optional-chain': 'off',
'import/no-named-as-default-member': 'off',
'import/no-named-as-default': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-unused-vars': [
'warn',
{
argsIgnorePattern: '^_',
varsIgnorePattern: '^_',
caughtErrorsIgnorePattern: '^_',
},
],
},
root: true,
extends: ['../../.eslintrc.js'],
};
Binary file removed e2e-tests/e2e-hardhat/.yarn/install-state.gz
Binary file not shown.
19 changes: 19 additions & 0 deletions e2e-tests/e2e-hardhat/contracts/Echo.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pragma solidity =0.8.20;

contract Echo{
string public echo;
uint echoCount;

event NewEcho(string message, uint count);

constructor() {
echo = "Deployed successfully!";
}

function scream(string memory message) public returns(string memory){
echo = message;
echoCount += 1;
emit NewEcho(message, echoCount);
return message;
}
}
22 changes: 22 additions & 0 deletions e2e-tests/e2e-hardhat/contracts/Greeter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pragma solidity =0.8.20;

import "hardhat/console.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";

contract Greeter is Initializable {
string private greeting;

function initialize(string memory _greeting) public initializer {
console.log("Deploying a Greeter with greeting:", _greeting);
greeting = _greeting;
}

function greet() public view returns (string memory) {
return greeting;
}

function setGreeting(string memory _greeting) public {
console.log("Changing greeting from '%s' to '%s'", greeting, _greeting);
greeting = _greeting;
}
}
15 changes: 15 additions & 0 deletions e2e-tests/e2e-hardhat/contracts/GreeterV2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pragma solidity =0.8.20;

import "hardhat/console.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "./Greeter.sol";

contract GreeterV2 is Greeter {
string private greeting;

function setGreetingV2(string memory _greeting) public {
string memory newGreeting = string(abi.encodePacked(_greeting, " - V2"));
console.log("<V2> Changing greeting from '%s' to '%s'", greeting, newGreeting);
setGreeting(newGreeting);
}
}
2 changes: 1 addition & 1 deletion e2e-tests/e2e-hardhat/contracts/HelloWorld.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity =0.8.9;
pragma solidity =0.8.20;

contract HelloWorld{
string public helloWorld = 'Hello World!';
Expand Down
9 changes: 9 additions & 0 deletions e2e-tests/e2e-hardhat/contracts/Token.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pragma solidity =0.8.20;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract Token is ERC20 {
constructor(uint256 _initialBalance) ERC20("Token", "TKN") public {
_mint(msg.sender, _initialBalance);
}
}
5 changes: 3 additions & 2 deletions e2e-tests/e2e-hardhat/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import '@nomicfoundation/hardhat-toolbox';
import '@openzeppelin/hardhat-upgrades';
import { HardhatUserConfig } from 'hardhat/config';

const config: HardhatUserConfig = {
solidity: '0.8.9',
solidity: '0.8.20',
networks: {
acalaFork: {
chainId: 787,
Expand All @@ -14,7 +15,7 @@ const config: HardhatUserConfig = {
},
},
mocha: {
timeout: 100000,
timeout: 30 * 60 * 1000, // 30 min
},
};

Expand Down
5 changes: 4 additions & 1 deletion e2e-tests/e2e-hardhat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"build": "hardhat compile",
"lint": "eslint .",
"clean": "rm -rf artifacts/ cache/ typechain-types/",
"clean": "rm -rf artifacts/ cache/ typechain-types/ .openzeppelin/",
"test": "hardhat test",
"test:acalaFork": "hardhat test --network acalaFork"
},
Expand All @@ -17,6 +17,9 @@
"@nomicfoundation/hardhat-network-helpers": "^1.0.0",
"@nomicfoundation/hardhat-toolbox": "^4.0.0",
"@nomicfoundation/hardhat-verify": "^2.0.0",
"@openzeppelin/contracts": "^4.9.0",
"@openzeppelin/contracts-upgradeable": "5.0.1",
"@openzeppelin/hardhat-upgrades": "3.0.0",
"@typechain/ethers-v6": "^0.5.0",
"@typechain/hardhat": "^9.0.0",
"@types/chai": "^4.2.0",
Expand Down
51 changes: 51 additions & 0 deletions e2e-tests/e2e-hardhat/test/echo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { ethers } from 'hardhat';
import { expect } from 'chai';

import { Echo } from '../typechain-types';

describe('Echo contract', function () {
let instance: Echo;

beforeEach(async () => {
instance = await ethers.deployContract('Echo', []);
await instance.waitForDeployment();
});

it('should set the value of the echo when deploying', async () => {
expect(await instance.echo()).to.equal('Deployed successfully!');
});

describe('Operation', function () {
it('should update the echo variable', async () => {
await (await instance.scream('Hello World!')).wait();

expect(await instance.echo()).to.equal('Hello World!');
});

it('should emit a NewEcho event', async () => {
const tx = await instance.scream('Hello World!');
await tx.wait();

await expect(tx)
.to.emit(instance, 'NewEcho')
.withArgs('Hello World!', 1);
});

it('should increment echo counter in the NewEcho event', async () => {
await (await instance.scream('Hello World!')).wait();

const tx = await instance.scream('Hello Goku!');
await tx.wait();

await expect(tx)
.to.emit(instance, 'NewEcho')
.withArgs('Hello Goku!', 2);
});

it('should return input value', async () => {
const response = await instance.scream.staticCall('Hello World!');

expect(response).to.equal('Hello World!');
});
});
});
File renamed without changes.
Loading

0 comments on commit 29e56bb

Please sign in to comment.