mirror of
https://github.com/PaulRBerg/hardhat-template.git
synced 2026-04-20 03:00:26 -04:00
refactor: initialise signers without mocha delay
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"delay": true,
|
||||
"extension": ["ts"],
|
||||
"recursive": "test",
|
||||
"require": ["@nomiclabs/buidler/register"],
|
||||
"require": ["hardhat/register"],
|
||||
"timeout": 20000
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
const shell = require("shelljs");
|
||||
|
||||
// The environment variables are loaded in buidler.config.ts
|
||||
// The environment variables are loaded in hardhat.config.ts
|
||||
const mnemonic = process.env.MNEMONIC;
|
||||
if (!mnemonic) {
|
||||
throw new Error("Please set your MNEMONIC in a .env file");
|
||||
@@ -8,9 +8,6 @@ if (!mnemonic) {
|
||||
|
||||
module.exports = {
|
||||
istanbulReporter: ["html"],
|
||||
mocha: {
|
||||
delay: true,
|
||||
},
|
||||
onCompileComplete: async function (_config) {
|
||||
await run("typechain");
|
||||
},
|
||||
|
||||
@@ -52,10 +52,6 @@ function createTestnetConfig(network: keyof typeof chainIds): NetworkUserConfig
|
||||
|
||||
const config: HardhatUserConfig = {
|
||||
defaultNetwork: "hardhat",
|
||||
mocha: {
|
||||
// Without this property set, the "setTimeout" from the Greeter.js file wouldn't work.
|
||||
delay: true,
|
||||
},
|
||||
networks: {
|
||||
hardhat: {
|
||||
chainId: chainIds.hardhat,
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import { Signer } from "@ethersproject/abstract-signer";
|
||||
import { expect } from "chai";
|
||||
|
||||
export function shouldBehaveLikeGreeter(_signers: Signer[]): void {
|
||||
export function shouldBehaveLikeGreeter(): void {
|
||||
it("should return the new greeting once it's changed", async function () {
|
||||
expect(await this.greeter.greet()).to.equal("Hello, world!");
|
||||
expect(await this.greeter.connect(this.signers.admin).greet()).to.equal("Hello, world!");
|
||||
|
||||
await this.greeter.setGreeting("Hola, mundo!");
|
||||
expect(await this.greeter.greet()).to.equal("Hola, mundo!");
|
||||
expect(await this.greeter.connect(this.signers.admin).greet()).to.equal("Hola, mundo!");
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3,23 +3,28 @@ import { ethers, waffle } from "hardhat";
|
||||
|
||||
import GreeterArtifact from "../artifacts/contracts/Greeter.sol/Greeter.json";
|
||||
|
||||
import { Accounts, Signers } from "../types";
|
||||
import { Greeter } from "../typechain/Greeter";
|
||||
import { shouldBehaveLikeGreeter } from "./Greeter.behavior";
|
||||
|
||||
const { deployContract } = waffle;
|
||||
|
||||
setTimeout(async function () {
|
||||
const signers: Signer[] = await ethers.getSigners();
|
||||
const admin: Signer = signers[0];
|
||||
describe("Unit tests", function () {
|
||||
before(async function () {
|
||||
this.accounts = {} as Accounts;
|
||||
this.signers = {} as Signers;
|
||||
|
||||
const signers: Signer[] = await ethers.getSigners();
|
||||
this.signers.admin = signers[0];
|
||||
this.accounts.admin = await signers[0].getAddress();
|
||||
});
|
||||
|
||||
describe("Greeter", function () {
|
||||
beforeEach(async function () {
|
||||
const greeting: string = "Hello, world!";
|
||||
this.greeter = (await deployContract(admin, GreeterArtifact, [greeting])) as Greeter;
|
||||
this.greeter = (await deployContract(this.signers.admin, GreeterArtifact, [greeting])) as Greeter;
|
||||
});
|
||||
|
||||
shouldBehaveLikeGreeter(signers);
|
||||
shouldBehaveLikeGreeter();
|
||||
});
|
||||
|
||||
run();
|
||||
}, 1000);
|
||||
});
|
||||
|
||||
3
types/augmentations.d.ts
vendored
3
types/augmentations.d.ts
vendored
@@ -1,3 +1,4 @@
|
||||
import { Accounts, Signers } from "./";
|
||||
import { Greeter } from "../typechain/Greeter";
|
||||
|
||||
declare module "hardhat/types" {
|
||||
@@ -9,6 +10,8 @@ declare module "hardhat/types" {
|
||||
|
||||
declare module "mocha" {
|
||||
export interface Context {
|
||||
accounts: Accounts;
|
||||
greeter: Greeter;
|
||||
signers: Signers;
|
||||
}
|
||||
}
|
||||
|
||||
9
types/index.ts
Normal file
9
types/index.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { Signer } from "@ethersproject/abstract-signer";
|
||||
|
||||
export interface Accounts {
|
||||
admin: string;
|
||||
}
|
||||
|
||||
export interface Signers {
|
||||
admin: Signer;
|
||||
}
|
||||
Reference in New Issue
Block a user