Переглянути джерело

refactor: initialise signers without mocha delay

Paul Razvan Berg 4 роки тому
батько
коміт
dfc146c7b1
7 змінених файлів з 30 додано та 22 видалено
  1. 1 2
      .mocharc.json
  2. 1 4
      .solcover.js
  3. 0 4
      hardhat.config.ts
  4. 3 4
      test/Greeter.behavior.ts
  5. 13 8
      test/Greeter.ts
  6. 3 0
      types/augmentations.d.ts
  7. 9 0
      types/index.ts

+ 1 - 2
.mocharc.json

@@ -1,7 +1,6 @@
 {
-  "delay": true,
   "extension": ["ts"],
   "recursive": "test",
-  "require": ["@nomiclabs/buidler/register"],
+  "require": ["hardhat/register"],
   "timeout": 20000
 }

+ 1 - 4
.solcover.js

@@ -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");
   },

+ 0 - 4
hardhat.config.ts

@@ -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,

+ 3 - 4
test/Greeter.behavior.ts

@@ -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!");
   });
 }

+ 13 - 8
test/Greeter.ts

@@ -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 - 0
types/augmentations.d.ts

@@ -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 - 0
types/index.ts

@@ -0,0 +1,9 @@
+import { Signer } from "@ethersproject/abstract-signer";
+
+export interface Accounts {
+  admin: string;
+}
+
+export interface Signers {
+  admin: Signer;
+}