Skip to content

Commit e6fee07

Browse files
committed
Update hardhat-mocha
1 parent 69fd895 commit e6fee07

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

packages/hardhat-mocha/src/task-action.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,8 @@ const testWithHardhat: NewTaskActionFunction<TestActionArguments> = async (
8282
perf.startPhase("Build");
8383

8484
if (!noCompile) {
85-
await hre.tasks.getTask("build").run({
86-
noTests: true,
87-
});
85+
const noTests = hre.config.solidity.splitTestsCompilation;
86+
await hre.tasks.getTask("build").run({ noTests });
8887
console.log();
8988
}
9089

packages/hardhat-mocha/test/index.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import {
66
assertRejectsWithHardhatError,
77
useFixtureProject,
88
} from "@nomicfoundation/hardhat-test-utils";
9+
import { overrideTask } from "hardhat/config";
10+
11+
import HardhatMochaPlugin from "../src/index.js";
912

1013
describe("Hardhat Mocha plugin", () => {
1114
describe("Success", () => {
@@ -51,4 +54,79 @@ describe("Hardhat Mocha plugin", () => {
5154
);
5255
});
5356
});
57+
58+
describe("build invocation", () => {
59+
useFixtureProject("test-project");
60+
61+
function buildArgCaptor() {
62+
const buildArgs: any[] = [];
63+
const buildOverride = overrideTask("build")
64+
.setAction(async () => {
65+
return {
66+
default: (args: any) => {
67+
buildArgs.push(args);
68+
return { contractRootPaths: [], testRootPaths: [] };
69+
},
70+
};
71+
})
72+
.build();
73+
return { buildArgs, buildOverride };
74+
}
75+
76+
it("should call build without noTests when splitTestsCompilation is false", async () => {
77+
const { createHardhatRuntimeEnvironment } = await import("hardhat/hre");
78+
79+
const { buildArgs, buildOverride } = buildArgCaptor();
80+
const hre = await createHardhatRuntimeEnvironment({
81+
plugins: [HardhatMochaPlugin],
82+
tasks: [buildOverride],
83+
});
84+
85+
// The task may throw because of the ESM re-run guard, but we only
86+
// care about the build invocation args captured before that point.
87+
try {
88+
await hre.tasks.getTask(["test", "mocha"]).run({});
89+
} catch {}
90+
91+
assert.equal(buildArgs.length, 1);
92+
assert.equal(buildArgs[0].noTests, false);
93+
});
94+
95+
it("should call build with noTests when splitTestsCompilation is true", async () => {
96+
const { createHardhatRuntimeEnvironment } = await import("hardhat/hre");
97+
98+
const { buildArgs, buildOverride } = buildArgCaptor();
99+
const hre = await createHardhatRuntimeEnvironment({
100+
solidity: {
101+
version: "0.8.28",
102+
splitTestsCompilation: true,
103+
},
104+
plugins: [HardhatMochaPlugin],
105+
tasks: [buildOverride],
106+
});
107+
108+
try {
109+
await hre.tasks.getTask(["test", "mocha"]).run({});
110+
} catch {}
111+
112+
assert.equal(buildArgs.length, 1);
113+
assert.equal(buildArgs[0].noTests, true);
114+
});
115+
116+
it("should skip compilation when noCompile is true regardless of splitTestsCompilation", async () => {
117+
const { createHardhatRuntimeEnvironment } = await import("hardhat/hre");
118+
119+
const { buildArgs, buildOverride } = buildArgCaptor();
120+
const hre = await createHardhatRuntimeEnvironment({
121+
plugins: [HardhatMochaPlugin],
122+
tasks: [buildOverride],
123+
});
124+
125+
try {
126+
await hre.tasks.getTask(["test", "mocha"]).run({ noCompile: true });
127+
} catch {}
128+
129+
assert.equal(buildArgs.length, 0);
130+
});
131+
});
54132
});

0 commit comments

Comments
 (0)