1

I created a basic hardhat sample project. When I do npx hardhat test , Hardhat is running the out of the box test in scripts/sample-script.js (which supposedly is the deployment pipeline) and hardhat is NOT running my test which is in test/sample-test.js How do I fix this? How do I tell hardhat where my tests are? Below is my test file sample-test.js:

describe("NFTMarket", function() {
  it("Should create and execute market sales", async function() {
    /* deploy the marketplace */
    const NFTMarketplace = await ethers.getContractFactory("NFTMarketplace")
    const nftMarketplace = await NFTMarketplace.deploy()
    await nftMarketplace.deployed()

    let listingPrice = await nftMarketplace.getListingPrice()
    listingPrice = listingPrice.toString()

    const auctionPrice = ethers.utils.parseUnits('1', 'ether')

    /* create two tokens */
    await nftMarketplace.createToken("https://www.mytokenlocation.com", auctionPrice, { value: listingPrice })
    await nftMarketplace.createToken("https://www.mytokenlocation2.com", auctionPrice, { value: listingPrice })

    const [_, buyerAddress] = await ethers.getSigners()

    /* execute sale of token to another user */
    await nftMarketplace.connect(buyerAddress).createMarketSale(1, { value: auctionPrice })

    /* resell a token */
    await nftMarketplace.connect(buyerAddress).resellToken(1, auctionPrice, { value: listingPrice })

    /* query for and return the unsold items */
    items = await nftMarketplace.fetchMarketItems()
    items = await Promise.all(items.map(async i => {
      const tokenUri = await nftMarketplace.tokenURI(i.tokenId)
      let item = {
        price: i.price.toString(),
        tokenId: i.tokenId.toString(),
        seller: i.seller,
        owner: i.owner,
        tokenUri
      }
      return item
    }))
    console.log('items: ', items)
  })
})

and here is my deployment pipeline, sample-script.js

const hre = require("hardhat");

async function main() {
 
  // We get the contract to deploy
  const Greeter = await hre.ethers.getContractFactory("Greeter");
  const greeter = await Greeter.deploy("Hello, Hardhat!");

  await greeter.deployed();

  console.log("Greeter deployed to:", greeter.address);
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

 
1
  • Post the test file code
    – Kof
    May 24, 2022 at 18:29

2 Answers 2

1

Update: Your test file looks OK and it runs on my env; Hardhat config docs explains how to configure test path, the default is test but make sure it's not configured otherwise in your hardhat.config.js -

module.exports = {
  ...
  paths: {
    sources: "./contracts",
    tests: "./test",
    cache: "./cache",
    artifacts: "./artifacts"
  },
  ...
}

Try to run npx hardhat test test/sample-test.js and see if it does anything.


Your filename should be test/sample-test.js, if it already is, then your test file is invalid. Find the error and it'll work.

6
  • I'm sorry, could you rephrase that? My file, as I said above, is called sample-test.js, which is in the test/ folder. So test/sample-test.js. 'Find the error'.... there is no error. it's running the test. But one from the wrong directory.
    – GUZZ
    May 24, 2022 at 20:45
  • That's what you assume... post the test script as I request and I'll be able to examine it.
    – Kof
    May 25, 2022 at 4:04
  • btw above you said that the test file is called test/sample-test, that's why I was making sure it has the js extension.
    – Kof
    May 25, 2022 at 4:06
  • indeed, thanks for pointing that out. just added a bit more depth to the explanation. In summary I just want to know where does hardhat define what is the path to test files. I find very weird that the tests are ran from the scripts folder out-of-the-box, but it does make sense that one could include tests in the deployment pipeline.
    – GUZZ
    May 25, 2022 at 8:36
  • Looks OK and it runs on a new project, did you try to run npx hardhat test test/sample-test.js? Let me know if it works
    – Kof
    May 25, 2022 at 9:10
1

I was able to solve this by

  1. adding to the npm package.json file a field defining explicitly the paths for testing with:

paths: { sources: "./contracts", tests: "./test", cache: "./cache", artifacts: "./artifacts" },

  1. uninstalling all deps and dev-deps. reinstalling the normal dependencies first and then the devDependencies with these commands in this order:

npm install

npm install -D

Running npx hardhat test now points to tests instead of scripts. More info in https://hardhat.org/config/#path-configuration

1
  • The paths shouldn't be in package.json, they should be in hardhat.config.js as I've written in my answer
    – Kof
    May 26, 2022 at 9:24

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.