Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hardhat test doesn't run any tests? #3117

Closed
JimLynchCodes opened this issue Sep 2, 2022 · 8 comments
Closed

hardhat test doesn't run any tests? #3117

JimLynchCodes opened this issue Sep 2, 2022 · 8 comments
Assignees

Comments

@JimLynchCodes
Copy link

JimLynchCodes commented Sep 2, 2022

Hi, when I point directly to my file with tests in it I get the output 0 passing

const { ethers } = require("hardhat");
const { use, expect } = require("chai");
const { solidity } = require("ethereum-waffle");

use(solidity);

describe("APPLE", async function () {
  let apple;

  const [owner, user1, user2] = await ethers.getSigners();

  beforeEach(async () => {
    const AppleContract = await ethers.getContractFactory('APPLE');
    apple = await AppleContract.deploy()
  })

  describe("init", function () {

    it('initializes everyone owning zero apples', async () => {

      expect(await apple.balanceOf(owner.address)).to.equal(0);
      expect(await apple.balanceOf(user1.address)).to.equal(0);
      expect(await apple.balanceOf(user2.address)).to.equal(0);

    })

    it('initializes all nutrition scores to zero', async () => {

      expect(await apple.get_nutrition_score(owner.address)).to.equal(0);
      expect(await apple.get_nutrition_score(user1.address)).to.equal(0);
      expect(await apple.get_nutrition_score(user2.address)).to.equal(0);

    })

    it('initializes total supply of apples to zero (none minted up front)', async () => {

      expect(await apple.totalSupply()).to.equal(0);

    })

  });

  describe('minting apple', () => {

    it('can\'t be called by owner or user addresses', async () => {

      await expect(apple.mint(owner.address, 1)).to.be.revertedWith('Only the TREE contract can call this function');

    })

    describe('called by TREE contract', () => {

      it('mints apple to the specified user', () => {
        // TODO
      })

    })

  })


});
 yarn test ./test/APPLE.test.js

Why does it not see any of my tests???

Screen Shot 2022-09-02 at 2 17 14 AM

@github-actions
Copy link
Contributor

github-actions bot commented Sep 2, 2022

This issue is also being tracked on Linear.

We use Linear to manage our development process, but we keep the conversations on Github.

LINEAR-ID: 1b539d9a-7257-4706-9b39-8193dbb6f391

@ghost
Copy link

ghost commented Sep 2, 2022

Do you have any script (shortcut) for yarn test, because basically you need to write yarn hardhat test?
When I write yarn test I got error Command "test" not found.

@ghost
Copy link

ghost commented Sep 2, 2022

Create new test file (inside test folder with name like a.test.js), paste this:

describe("testDesc", function () {
	it("testIt", async () => {
		console.log("Works!");
	});
});

Run command: yarn hardhat test.
Does it work and pass 1 test?

@alcuadrado
Copy link
Member

Hey @JimLynchCodes,

The problem here is that you are using an async function within a describe, and that's not supported by mocha. Your describe callbacks need to be synchronous.

Thanks @woojiq for helping :)

@ghost
Copy link

ghost commented Sep 2, 2022

The problem here is that you are using an async function within a describe, and that's not supported by mocha. Your describe callbacks need to be synchronous.

I didn't think async describe was an issue before, because if I changed to async everything worked for me as before. But now I realized that this word means nothing if we do not cause anything asynchronous in the body. So I added const test = await ethers.getSigner() and now I have 0 passing. +1 point to understanding asynchrony.

@JimLynchCodes

This comment was marked as spam.

@JimLynchCodes

This comment was marked as spam.

@ghost
Copy link

ghost commented Sep 9, 2022

This is kind of a problem though bc I need to call "ethers.getSigners()" in my code, which is an async function.

Why is mocha not supporting async describe? It doesn't make any sense to me... 👎

What is the problem to do async code in beforeEach?
Instead of const [owner, user1, user2] = await ethers.getSigners();, do smth like this:

let owner, user1, user2;
beforeEach() {
     [owner, user1, user2] = await ethers.getSigners();
}

P.S. didn't test

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants