Skip to content

Commit

Permalink
feat: create a mocha/ethers template project
Browse files Browse the repository at this point in the history
As part of the Alpha prep this adds a mocha/ethers template project that
tries to recreate the functionality (we are currently able to support)
of the v2 Ethers typescript project in v3.

It pulls in the `v-next` versions of the mocha test runner, the
`hardhat-test-helpers` and `hardhat-ethers`.

It adds a the `Lock.sol` and `Lock.ts` test file. The test file has been
modified to allow for running the tests while we still have rough edges.

Specifically the rough edges are:
- we need new patterns for sharing `connection` among tests (I added a
  `beforeEach`)
- we don't have chai matchers so I added `chai-as-promised` and adapted
  the tests ... crudely.
  • Loading branch information
kanej committed Oct 28, 2024
1 parent 733b0b3 commit 5e9955a
Show file tree
Hide file tree
Showing 9 changed files with 347 additions and 11 deletions.
1 change: 1 addition & 0 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"template-empty-javascript": "0.0.1",
"template-empty-typescript": "0.0.1",
"template-javascript-with-examples": "0.0.1",
"template-mocha-ethers": "0.0.1",
"template-typescript-with-examples": "0.0.1"
},
"changesets": [
Expand Down
74 changes: 63 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions v-next/hardhat/templates/mocha-ethers/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Node modules
/node_modules

# Compilation output
/dist

# pnpm deploy output
/bundle

# Hardhat Build Artifacts
/artifacts

# Hardhat compilation (v2) support directory
/cache
20 changes: 20 additions & 0 deletions v-next/hardhat/templates/mocha-ethers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# A TypeScript Hardhat project using Mocha and Ethers

> WARNING: This demonstration project is still in development. It is part of the Hardhat v3 upgrade. It is not for production use.
> NOTE: There are several plugins from the Hardhat toolbox that have not been ported to Hardhat v3 yet. In testing terms, the biggest ommision is `hardhat-chai-matchers`.
This project demonstrates basic Hardhat usecases within a TypeScript project. It comes with a minimal Hardhat configuration file and a test.

For integration testing it uses the Mocha test runner and the Ethers.js library for interacting with Ethereum nodes.

Try running the following commands to see Hardhat in action:

```shell
# Setup a running Hardhat v2 node in another terminal: `npx hardhat node`

# The test command does not currently trigger the compilation step
npx hardhat3 compile
# Run the mocha integration test suite
npx hardhat3 test
```
34 changes: 34 additions & 0 deletions v-next/hardhat/templates/mocha-ethers/contracts/Lock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;

// Uncomment this line to use console.log
// import "hardhat/console.sol";

contract Lock {
uint public unlockTime;
address payable public owner;

event Withdrawal(uint amount, uint when);

constructor(uint _unlockTime) payable {
require(
block.timestamp < _unlockTime,
"Unlock time should be in the future"
);

unlockTime = _unlockTime;
owner = payable(msg.sender);
}

function withdraw() public {
// Uncomment this line, and the import of "hardhat/console.sol", to print a log in your terminal
// console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp);

require(block.timestamp >= unlockTime, "You can't withdraw yet");
require(msg.sender == owner, "You aren't the owner");

emit Withdrawal(address(this).balance, block.timestamp);

owner.transfer(address(this).balance);
}
}
12 changes: 12 additions & 0 deletions v-next/hardhat/templates/mocha-ethers/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { HardhatUserConfig } from "@ignored/hardhat-vnext/types/config";

import HardhatMochaTestRunner from "@ignored/hardhat-vnext-mocha-test-runner";
import HardhatEthers from "@ignored/hardhat-vnext-ethers";
import HardhatNetworkHelpers from "@ignored/hardhat-vnext-network-helpers";

const config: HardhatUserConfig = {
plugins: [HardhatMochaTestRunner, HardhatEthers, HardhatNetworkHelpers],
solidity: "0.8.24",
};

export default config;
20 changes: 20 additions & 0 deletions v-next/hardhat/templates/mocha-ethers/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "template-mocha-ethers",
"private": true,
"version": "0.0.1",
"description": "A Typescript Hardhat project using Mocha and Ethers.js",
"type": "module",
"devDependencies": {
"@ignored/hardhat-vnext": "workspace:^3.0.0-next.5",
"@ignored/hardhat-vnext-ethers": "workspace:^3.0.0-next.1",
"@ignored/hardhat-vnext-mocha-test-runner": "workspace:^3.0.0-next.1",
"@ignored/hardhat-vnext-network-helpers": "workspace:^3.0.0-next.1",
"@types/chai": "^4.2.0",
"@types/chai-as-promised": "^8.0.1",
"@types/mocha": ">=9.1.0",
"chai": "^4.4.1",
"chai-as-promised": "^8.0.0",
"mocha": "^10.0.0",
"typescript": "~5.5.0"
}
}
Loading

0 comments on commit 5e9955a

Please sign in to comment.