|
6 | 6 | assertRejectsWithHardhatError, |
7 | 7 | useFixtureProject, |
8 | 8 | } from "@nomicfoundation/hardhat-test-utils"; |
| 9 | +import { overrideTask } from "hardhat/config"; |
| 10 | + |
| 11 | +import HardhatMochaPlugin from "../src/index.js"; |
9 | 12 |
|
10 | 13 | describe("Hardhat Mocha plugin", () => { |
11 | 14 | describe("Success", () => { |
@@ -51,4 +54,79 @@ describe("Hardhat Mocha plugin", () => { |
51 | 54 | ); |
52 | 55 | }); |
53 | 56 | }); |
| 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 | + }); |
54 | 132 | }); |
0 commit comments