3

I have a few test files:

./test/ContractA.test.js

const { expect } = require("chai");

describe("ContractA tests", function () {
  it("does function one", async function () {
    expect(await contractA.someFunc()).to.equal(something);
  });
  it("does function two", async function () {
    expect(await contractA.someOtherFunction()).to.equal(somethingOtherThing);
  });
});

./test/ContractB.test.js

const { expect } = require("chai");

describe("ContractB tests", function () {
  it("does function one", async function () {
    expect(await contractB.someFunc()).to.equal(something);
  });
  it("does function two", async function () {
    expect(await contractB.someOtherFunction()).to.equal(somethingOtherThing);
  });
});

When I run hardhat test all the files run.

How Can I run all the tests in ContractA.test.js?


Things I've Already Tried

hardhat test ./test/ContratA.test.js - this does not run any tests at all...

hardhat test --grep "ContractA" - also does not run any tests...

1
  • Did you try npx hardhat test ./test/ContratA.test.js
    – Sky
    Commented Dec 15, 2022 at 9:27

5 Answers 5

5
$ yarn hardhat test ./test/ContratA.test.js

Should work as the command line helper message explains:

$ yarn hardhat test --help

    Usage: hardhat [GLOBAL OPTIONS] test [--bail] [--deploy-fixture] [--grep <STRING>] [--no-compile] [--no-impersonation] [--parallel] [...testFiles]
    POSITIONAL ARGUMENTS:
    
      testFiles     An optional list of files to test (default: [])
3
  • nope! this stills runs the other test files for me. WTF??
    – Jim
    Commented Aug 15, 2022 at 13:34
  • @Jim um.. what's your hardhat version? Commented Aug 15, 2022 at 14:50
  • In my experience, this will run only the desired test file Commented Sep 15, 2022 at 6:25
1

yes you can do that. You just need to put ".only" after that describe, that you want to run. In your case:

const { expect } = require("chai");

describe.only("ContractA tests", function () {   it("does function one", async function () {
    expect(await contractA.someFunc()).to.equal(something);   });   it("does function two", async function () {
    expect(await contractA.someOtherFunction()).to.equal(somethingOtherThing);   }); });
3
  • This answer is incorrect. When I add .only all my other test files are still run.
    – Jim
    Commented Aug 6, 2022 at 5:07
  • Explaining once again: In my above answer, ".only" is to be used for single test case file which have more than one describe. Only that describe will work, which have .only in front of it. For different test case files you have to comment other ones, which you don't want to test. Commented Aug 8, 2022 at 5:01
  • This "answer" does not address my question at all. Please try again!
    – Jim
    Commented Aug 9, 2022 at 15:01
1

I encountered the same problem, try this

npx hardhat test ../test/ContratA.test.js

The difference between your command is the path is ../test/ContratA.test.js not ./test/ContratA.test.js

0

hardhat test ./test/ContractA.test.js is correct. You misspelt ContractA in your original post (you were missing the c and ended up with ContratA)

0

you can also use hardhat.config.js to configure your sources and tests directories and write a custom task to prescribe which test file to execute at run time. Sample of one such custom task -

task("testFile", "Test a specific contract")
  .addParam("test", "Path to the corresponding test file")
  .setAction(async (args, { run }) => {
    if (args.test) {
      await run("compile");
      await run("test", { files: args.test });
    } else {
      console.error("Please provide the test file path.");
    }
  })

The compiler will know where to pick the contract and test files from the sources and tests descriptions and will pick up which specific file to run based on the subtask. Sample request: npx hardhat testFile --test <test-file-name>.test.js

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.