0

I am trying to write Hardhat tests with TypeScript and am struggling with the scope of (thus accessing) certain variables. I have provided a simplified snippet of what I am struggling with.

Below, is the example that works. Declaring and assigning accountOne in the same unit test I need it in. However, I need the variable in more than one test, so I need to change its scope to be accessible in more than one test. This way I can avoid having to repeat the same lines of code over and over.

import { ethers } from "hardhat";
import { Signer } from "ethers";

describe("Runs unit tests with Hardhat", () => {

    it("Logs accountOne's address from Hardhat", async () => {
        const [accountOne] = await ethers.getSigners();
        console.log(accountOne.address);
    });

})

Below is my attempt at declaring and assigning it in the before hook's block of code. It didn't work because it is a local variable to that block. (I've also tried using a beforeEach hook to similar results.) However, I've done it this way using JavaScript, and it worked previously, so I assumed it would work with TypeScript. This logs Cannot find name 'accountOne'.

import { ethers } from "hardhat";
import { Signer } from "ethers";

describe("Runs unit tests with Hardhat", () => {

    before(async () => {
        const [accountOne] = await ethers.getSigners();
    });

    it("Logs accountOne from Hardhat", () => {
        console.log(accountOne.address);
    });

})

Below, is an attempt at accessing accountOne in the unit test. I declared it in the describe block of code with a type of Signer, assigned it a value in the before hook, then used it in the unit test I needed. This is the one I am least sure about because I am declaring accountOne as type Signer, when the signers are originally created from calling ethers.getSigners(). This logs Property 'address' does not exist on type 'Signer'.

import { ethers } from "hardhat";
import { Signer } from "ethers";

describe("Runs unit tests with Hardhat", () => {
    let accountOne : Signer;

    before(async () => {
        accountOne = await ethers.getSigners();
    });

    it("Logs accountOne from Hardhat", () => {
        console.log(accountOne.address);
    });
})

Below is my most recent attempt, in which I declared and assigned accountOne a value in the describe block, then access it in the unit test. This logs nothing to the console.

import { ethers } from "hardhat";
import { Signer } from "ethers";

describe("Runs unit tests with Hardhat", async () => {
    const [accountOne] = await ethers.getSigners();

    it("Logs accountOne from Hardhat", () => {
        console.log(accountOne.address);
    });
})

I am currently learning TypeScript, so that is where I believe the issue lies. Also, because I didn't encounter this issue when writing tests in JavaScript.

1

2 Answers 2

0

As I suspected, declaring accountOne as type Signer was incorrect, and instead I declared it as let accountOne : any in the describe block, assigned a value in the before block, then logged it from the unit test. So it seems I gave it the wrong "type" when I declared it. Seeing as how I have to give it a type of the time of declaration, I assigned it the type of any.

0

I know it is old and you resolve it :) Your declarations will work inside your statement (Describe, it, before, ...). Even you can declare outside to make the variable "global" in all the file and access in every statements, like:

let yourVar;

before("before", function(){
   // you can access here
})

describe("Context", function () {
   // you can access here
   it("test1", async function () {
      // you can acccess here
   }) 
})

In this case, the await ethers.getSigners() return an array of type SignerWithAddress type, and you can import it from hardhat-ethers with @nomiclabs/hardhat-ethers/signers. So, you declaration should be:

import type { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";

let account: SignerWithAddress;

Hope make more clear to anyone else that will have the same question :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.