Use HardHat tasks directly from the code (and tests)

hardhattesting

I'm starting to use HardHat for my project and I created a list of (super useful) tasks ans now I'm trying to implement tests and I want to use some task (like deploy to setup my smart-contract environment without re-writing the code everywhere.

Is this possible ?
Thank

Best Answer

Yes you can run a Hardhat task programmatically by importing the run function from the Hardhat Runtime Environment:

import { run } from "hardhat";

run("task-name");

See the docs for more examples.

Related Topic