Javascript Testing – How to Run a Single Hardhat Test

hardhatjavascriptmochatesting

I have a test file in hardhat like so:

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

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

How can I test just one of the its at a time?

In mocha you can run a single test with the --grep command. Is there something similar for hardhat?

Something like:

npx hardhat test --grep "does function one"

or

npx hardhat test --grep "does function two"

Best Answer

Use .only(). For example, your test file would look like this:

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

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

and then you can run npx hardhat test and it will only run that test set.

If you just want one it, instead of using .only() on describe, you can use it.only().

EDIT:

As of hardhat version 2.9, you can use --grep as described in the original question.

Related Topic