diff --git a/packages/hardhat-node-test-runner/src/task-action.ts b/packages/hardhat-node-test-runner/src/task-action.ts index 238b085e0e1..b922d68872e 100644 --- a/packages/hardhat-node-test-runner/src/task-action.ts +++ b/packages/hardhat-node-test-runner/src/task-action.ts @@ -69,9 +69,8 @@ const testWithHardhat: NewTaskActionFunction = async ( setGlobalOptionsAsEnvVariables(hre.globalOptions); if (!noCompile) { - await hre.tasks.getTask("build").run({ - noTests: true, - }); + const noTests = hre.config.solidity.splitTestsCompilation; + await hre.tasks.getTask("build").run({ noTests }); console.log(); } diff --git a/packages/hardhat-node-test-runner/test/index.ts b/packages/hardhat-node-test-runner/test/index.ts index e9c0ad0cbfc..a654ea88386 100644 --- a/packages/hardhat-node-test-runner/test/index.ts +++ b/packages/hardhat-node-test-runner/test/index.ts @@ -2,8 +2,11 @@ import assert from "node:assert/strict"; import { describe, it } from "node:test"; import { useFixtureProject } from "@nomicfoundation/hardhat-test-utils"; +import { overrideTask } from "hardhat/config"; import { createHardhatRuntimeEnvironment } from "hardhat/hre"; +import HardhatNodeTestRunnerPlugin from "../src/index.js"; + describe("Hardhat Node plugin", () => { useFixtureProject("test-project"); @@ -44,4 +47,79 @@ describe("Hardhat Node plugin", () => { process.env.NODE_ENV = nodeEnv; } }); + + describe("build invocation", () => { + function buildArgCaptor() { + const buildArgs: any[] = []; + const buildOverride = overrideTask("build") + .setAction(async () => { + return { + default: (args: any) => { + buildArgs.push(args); + return { contractRootPaths: [], testRootPaths: [] }; + }, + }; + }) + .build(); + return { buildArgs, buildOverride }; + } + + it("should call build without noTests when splitTestsCompilation is false", async () => { + const { buildArgs, buildOverride } = buildArgCaptor(); + const hre = await createHardhatRuntimeEnvironment({ + plugins: [HardhatNodeTestRunnerPlugin], + tasks: [buildOverride], + }); + + await hre.tasks.getTask(["test", "nodejs"]).run({}); + + assert.equal(buildArgs.length, 1); + assert.equal(buildArgs[0].noTests, false); + }); + + it("should call build with noTests when splitTestsCompilation is true", async () => { + const { buildArgs, buildOverride } = buildArgCaptor(); + const hre = await createHardhatRuntimeEnvironment({ + solidity: { + version: "0.8.28", + splitTestsCompilation: true, + }, + plugins: [HardhatNodeTestRunnerPlugin], + tasks: [buildOverride], + }); + + await hre.tasks.getTask(["test", "nodejs"]).run({}); + + assert.equal(buildArgs.length, 1); + assert.equal(buildArgs[0].noTests, true); + }); + + it("should skip compilation when noCompile is true without splitTestsCompilation", async () => { + const { buildArgs, buildOverride } = buildArgCaptor(); + const hre = await createHardhatRuntimeEnvironment({ + plugins: [HardhatNodeTestRunnerPlugin], + tasks: [buildOverride], + }); + + await hre.tasks.getTask(["test", "nodejs"]).run({ noCompile: true }); + + assert.equal(buildArgs.length, 0); + }); + + it("should skip compilation when noCompile is true with splitTestsCompilation", async () => { + const { buildArgs, buildOverride } = buildArgCaptor(); + const hre = await createHardhatRuntimeEnvironment({ + solidity: { + version: "0.8.28", + splitTestsCompilation: true, + }, + plugins: [HardhatNodeTestRunnerPlugin], + tasks: [buildOverride], + }); + + await hre.tasks.getTask(["test", "nodejs"]).run({ noCompile: true }); + + assert.equal(buildArgs.length, 0); + }); + }); });