0

I try to run a simple test in Hardhat but for some reason it is not working it always returns:

0 passing (1ms)

This is the test:

const { ethers } = require("hardhat")
const { expect, assert } = require("chai")

describe("SimpleStorage", async function () {
  let simpleStorageFactory, simpleStorage
  beforEach(async function () {
    simpleStorageFactory = await ethers.getContractFactory("SimpleStorage")
    simpleStorage = await simpleStorageFactory.deploy()
  })

  it("Should start with a favorite number of 0", async function () {
    const currentValue = await simpleStorage.retrieve()
    const expectedValue = "0"

    assert.equal(currentValue.toString(), expectedValue)
  })
})

When I run:

yarn hardhat test

it returns 0. As if the test file is empty

0 passing (1ms)

This is hardhat.config.js:

require("@nomicfoundation/hardhat-toolbox")
require("dotenv").config()
require("@nomiclabs/hardhat-etherscan")
require("./tasks/block_number")
require("@nomicfoundation/hardhat-chai-matchers")
require("@nomiclabs/hardhat-ethers")
require("@nomiclabs/hardhat-waffle")

const RINKEBY_RPC_URL = process.env.RINKEBY_RPC_URL
const PRIVATE_KEY = process.env.PRIVATE_KEY
const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  defaultNetwork: "hardhat",
  networks: {
    rinkeby: {
      url: RINKEBY_RPC_URL,
      accounts: [PRIVATE_KEY],
      chainId: 4,
    },
    localhost: {
      url: "http://127.0.0.1:8545",

      chainId: 31337,
    },
  },
  solidity: "0.8.8",
  etherscan: {
    apiKey: ETHERSCAN_API_KEY,
  },
}

package.json:

{
  "name": "hardhat-simple-ss-2",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "devDependencies": {
    "@ethersproject/abi": "^5.4.7",
    "@ethersproject/providers": "^5.4.7",
    "@nomicfoundation/hardhat-chai-matchers": "^1.0.2",
    "@nomicfoundation/hardhat-network-helpers": "^1.0.0",
    "@nomicfoundation/hardhat-toolbox": "^1.0.1",
    "@nomiclabs/hardhat-ethers": "^2.1.0",
    "@nomiclabs/hardhat-etherscan": "^3.1.0",
    "@nomiclabs/hardhat-waffle": "^2.0.3",
    "@typechain/ethers-v5": "^10.1.0",
    "@typechain/hardhat": "^6.1.2",
    "@types/mocha": "^9.1.1",
    "chai": "^4.3.6",
    "dotenv": "^16.0.1",
    "ethereum-waffle": "^3.4.4",
    "ethers": "^5.6.9",
    "hardhat": "^2.10.1",
    "hardhat-gas-reporter": "^1.0.8",
    "prettier": "^2.7.1",
    "prettier-plugin-solidity": "^1.0.0-dev.23",
    "solidity-coverage": "^0.7.21",
    "ts-node": "^10.9.1",
    "typechain": "^8.1.0",
    "typescript": "^4.7.4"
  }
}

All packages seem to be installed as in https://hardhat.org/hardhat-runner/docs/other-guides/waffle-testing

What could be the problem?

3
  • 2
    what's your folder structure / name of the file. It has to be in the "test" folder
    – thefett
    Jul 15, 2022 at 12:38
  • it is in the "test" folder
    – PinPiguin
    Jul 15, 2022 at 12:42
  • @PinPiguin consider using my Hardhat-based template so you don't have to configure this yourself. Jul 15, 2022 at 14:01

3 Answers 3

2

Your beforEach(async function () is missing an "e" at the end of before

Should be beforeEach(async function ()

0

I think the issue is that you have the async included in the top level describe block. Removing it should allow your tests to be executed. Replacing line 4 with this should solve your issue:

describe("SimpleStorage", function () {
0

Your test file need to be in the test folder, I ran into the same problem and all i had to do was move my test file which i mistakenly created in the root folder to the test folder. That will solve the problem.

1

Your Answer

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

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