Skip to content

Commit 61e63df

Browse files
Create good end to end tests (#4620)
Add end-to-end tests
1 parent acbda54 commit 61e63df

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+707
-11
lines changed

.github/workflows/e2e-tests.yml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: E2E tests
2+
3+
on:
4+
push:
5+
branches:
6+
- "**"
7+
8+
jobs:
9+
run-e2e:
10+
strategy:
11+
matrix:
12+
os: [ubuntu-latest, macos-latest, windows-latest]
13+
name: Run E2E tests on ${{ matrix.os }}
14+
runs-on: ${{ matrix.os }}
15+
steps:
16+
- uses: actions/checkout@v2
17+
- uses: pnpm/action-setup@v2
18+
with:
19+
version: 8
20+
- uses: actions/setup-node@v2
21+
with:
22+
node-version: 18
23+
- name: Run fixture-projects script
24+
run: |
25+
cd e2e
26+
chmod +x run-fixture-projects.sh
27+
./run-fixture-projects.sh
28+
shell: bash
29+
- name: Run test-project-initialization script
30+
run: |
31+
cd e2e
32+
chmod +x test-project-initialization.sh
33+
./test-project-initialization.sh
34+
shell: bash

docs/src/content/hardhat-runner/docs/getting-started/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ $ npx hardhat init
8585
? What do you want to do? …
8686
❯ Create a JavaScript project
8787
Create a TypeScript project
88+
Create a TypeScript project (with Viem)
8889
Create an empty hardhat.config.js
8990
Quit
9091
```

docs/src/content/hardhat-runner/docs/guides/project-setup.md

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Welcome to Hardhat v{HARDHAT_VERSION}
8686
? What do you want to do? …
8787
▸ Create a JavaScript project
8888
Create a TypeScript project
89+
Create a TypeScript project (with Viem)
8990
Create an empty hardhat.config.js
9091
Quit
9192
```

docs/src/content/hardhat-runner/docs/other-guides/waffle-testing.md

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Welcome to Hardhat v{HARDHAT_VERSION}
3838
? What do you want to do? …
3939
▸ Create a JavaScript project
4040
Create a TypeScript project
41+
Create a TypeScript project (with Viem)
4142
Create an empty hardhat.config.js
4243
Quit
4344
```

docs/src/content/tutorial/creating-a-new-hardhat-project.md

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ $ npx hardhat init
101101
? What do you want to do? …
102102
Create a JavaScript project
103103
Create a TypeScript project
104+
Create a TypeScript project (with Viem)
104105
❯ Create an empty hardhat.config.js
105106
Quit
106107
```

e2e/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# All e2e test temporary folders
2+
projects-initialization-tests-*
3+
fixture-projects-run-*

e2e/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
### Hardhat initialization tests
2+
3+
To run the "hardhat init" tests, use the command:
4+
5+
```
6+
./test-project-initialization.sh
7+
```
8+
9+
### All other Hardhat scenario tests
10+
11+
To run all the fixture tests, use the command:
12+
13+
```
14+
./run-fixture-projects.sh
15+
```
16+
17+
You can run a single project's tests by passing the project folder name as a parameter.
18+
19+
Example: to run only the tests inside the `vars` folder, use:
20+
21+
```
22+
./run-fixture-projects.sh vars
23+
```

e2e/clean.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#! /usr/bin/env bash
2+
rm -fr fixture-projects-run-*
3+
rm -fr projects-initialization-tests-*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.19;
3+
4+
// Uncomment this line to use console.log
5+
// import "hardhat/console.sol";
6+
7+
contract Lock {
8+
uint public unlockTime;
9+
address payable public owner;
10+
11+
event Withdrawal(uint amount, uint when);
12+
13+
constructor(uint _unlockTime) payable {
14+
require(
15+
block.timestamp < _unlockTime,
16+
"Unlock time should be in the future"
17+
);
18+
19+
unlockTime = _unlockTime;
20+
owner = payable(msg.sender);
21+
}
22+
23+
function withdraw() public {
24+
// Uncomment this line, and the import of "hardhat/console.sol", to print a log in your terminal
25+
// console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp);
26+
27+
require(block.timestamp >= unlockTime, "You can't withdraw yet");
28+
require(msg.sender == owner, "You aren't the owner");
29+
30+
emit Withdrawal(address(this).balance, block.timestamp);
31+
32+
owner.transfer(address(this).balance);
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
solidity: "0.8.19",
3+
};
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "clean"
3+
}

e2e/fixture-projects/clean/test.sh

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#! /usr/bin/env sh
2+
3+
# fail if any commands fails
4+
set -e
5+
6+
# import helpers functions
7+
. ../../helpers.sh
8+
9+
echo "Running tests: $(basename "$(pwd)")"
10+
11+
run_test_and_handle_failure "npx hardhat compile" 0
12+
13+
echo "assert that the artifacts and cache directory exist and are not empty"
14+
assert_directory_exists "artifacts"
15+
assert_directory_not_empty "artifacts"
16+
assert_directory_exists "cache"
17+
assert_directory_not_empty "cache"
18+
19+
echo "it should run the command clean successfully"
20+
run_test_and_handle_failure "npx hardhat clean" 0
21+
22+
echo "it should have deleted the artifacts directory and emptied the cache directory"
23+
assert_directory_doesnt_exist "artifacts"
24+
assert_directory_exists "cache"
25+
assert_directory_empty "cache"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
contract Foo {
5+
uint public x;
6+
7+
function inc() public {
8+
x++;
9+
error
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
solidity: "0.8.20"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "compile-fail"
3+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#! /usr/bin/env sh
2+
3+
# fail if any commands fails
4+
set -e
5+
6+
# import helpers functions
7+
. ../../helpers.sh
8+
9+
echo "Running tests: $(basename "$(pwd)")"
10+
11+
echo "it should fail the compilation"
12+
run_test_and_handle_failure "npx hardhat compile" 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
contract Foo {
5+
uint public x;
6+
7+
function inc() public {
8+
x++;
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
solidity: "0.8.20"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "compile"
3+
}

e2e/fixture-projects/compile/test.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#! /usr/bin/env sh
2+
3+
# fail if any commands fails
4+
set -e
5+
6+
# import helpers functions
7+
. ../../helpers.sh
8+
9+
echo "Running tests: $(basename "$(pwd)")"
10+
11+
echo "it should compile the contracts in the default project folder"
12+
run_test_and_handle_failure "npx hardhat compile" 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* SPDX-License-Identifier: MIT */
2+
3+
pragma abicoder v2;
4+
pragma solidity ^0.8.19;
5+
6+
import "./B.sol";
7+
8+
contract A {
9+
uint256 private storedData;
10+
11+
function set(uint256 value) public {
12+
storedData = value;
13+
}
14+
15+
function get() public view returns (uint256) {
16+
return storedData;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// SPDX-License-Identifier: MPL-2.0
2+
3+
pragma solidity ^0.8.19;
4+
5+
import "./C.sol";
6+
7+
contract B {
8+
uint public x;
9+
10+
function inc() public {
11+
x++;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pragma solidity ^0.8.19;
2+
3+
contract C {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.19;
3+
4+
// Uncomment this line to use console.log
5+
// import "hardhat/console.sol";
6+
7+
contract Lock {
8+
uint public unlockTime;
9+
address payable public owner;
10+
11+
event Withdrawal(uint amount, uint when);
12+
13+
constructor(uint _unlockTime) payable {
14+
require(
15+
block.timestamp < _unlockTime,
16+
"Unlock time should be in the future"
17+
);
18+
19+
unlockTime = _unlockTime;
20+
owner = payable(msg.sender);
21+
}
22+
23+
function withdraw() public {
24+
// Uncomment this line, and the import of "hardhat/console.sol", to print a log in your terminal
25+
// console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp);
26+
27+
require(block.timestamp >= unlockTime, "You can't withdraw yet");
28+
require(msg.sender == owner, "You aren't the owner");
29+
30+
emit Withdrawal(address(this).balance, block.timestamp);
31+
32+
owner.transfer(address(this).balance);
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
solidity: "0.8.19",
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "flatten"
3+
}

e2e/fixture-projects/flatten/test.sh

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#! /usr/bin/env sh
2+
3+
# fail if any commands fails
4+
set -e
5+
6+
# import helpers functions
7+
. ../../helpers.sh
8+
9+
echo "Running tests: $(basename "$(pwd)")"
10+
11+
echo "it should flatten all the files in the folder because no file names are passed"
12+
run_test_and_handle_failure "npx hardhat flatten" 0
13+
14+
echo "it should flatten only the 'Lock.sol' file because it is passed as an argument"
15+
run_test_and_handle_failure "npx hardhat flatten contracts/Lock.sol" 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
contract Foo {
5+
uint public x;
6+
7+
function inc() public {
8+
x++;
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require("@nomicfoundation/hardhat-toolbox");
2+
3+
module.exports = {
4+
solidity: "0.8.20",
5+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "gas-reporter",
3+
"devDependencies": {
4+
"@nomicfoundation/hardhat-toolbox": "^4.0.0"
5+
}
6+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#! /usr/bin/env sh
2+
3+
# fail if any commands fails
4+
set -e
5+
6+
# import helpers functions
7+
. ../../helpers.sh
8+
9+
echo "Running tests: $(basename "$(pwd)")"
10+
11+
echo "it should run the gas reporter while running the tests"
12+
export "REPORT_GAS='true'"
13+
run_test_and_handle_failure "npx hardhat test" 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it("provider should be available", async function () {
2+
await hre.network.provider.send("eth_accounts");
3+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
solidity: "0.8.19",
3+
};
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "help"
3+
}

e2e/fixture-projects/help/test.sh

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#! /usr/bin/env sh
2+
3+
# fail if any commands fails
4+
set -e
5+
6+
# import helpers functions
7+
. ../../helpers.sh
8+
9+
echo "Running tests: $(basename "$(pwd)")"
10+
11+
echo "it should show the help information when no argument is passed"
12+
run_test_and_handle_failure "npx hardhat" 0
13+
14+
echo "it should show the help information when the 'help' argument is passed"
15+
run_test_and_handle_failure "npx hardhat help" 0

0 commit comments

Comments
 (0)