Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions packages/hardhat-mocha/src/task-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ const testWithHardhat: NewTaskActionFunction<TestActionArguments> = async (
perf.startPhase("Build");

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();
}

Expand Down
103 changes: 99 additions & 4 deletions packages/hardhat-mocha/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { HardhatRuntimeEnvironment, TaskArguments } from "hardhat/types";

import assert from "node:assert/strict";
import { describe, it } from "node:test";

Expand All @@ -6,14 +8,17 @@ import {
assertRejectsWithHardhatError,
useFixtureProject,
} from "@nomicfoundation/hardhat-test-utils";
import { ensureError } from "@nomicfoundation/hardhat-utils/error";
import { overrideTask } from "hardhat/config";
import { createHardhatRuntimeEnvironment } from "hardhat/hre";

import HardhatMochaPlugin from "../src/index.js";

describe("Hardhat Mocha plugin", () => {
describe("Success", () => {
useFixtureProject("test-project");

it("should work", async () => {
const { createHardhatRuntimeEnvironment } = await import("hardhat/hre");

const hardhatConfig = await import(
"./fixture-projects/test-project/hardhat.config.js"
);
Expand All @@ -35,8 +40,6 @@ describe("Hardhat Mocha plugin", () => {
useFixtureProject("invalid-mocha-config");

it("should fail", async () => {
const { createHardhatRuntimeEnvironment } = await import("hardhat/hre");

const errors =
"\t* Config error in config.test.mocha.delay: Expected boolean, received number";

Expand All @@ -51,4 +54,96 @@ describe("Hardhat Mocha plugin", () => {
);
});
});

describe("build invocation", () => {
useFixtureProject("test-project");

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 };
}

async function runMochaIgnoringEsmReRunErrors(
hre: HardhatRuntimeEnvironment,
args: TaskArguments = {},
) {
try {
await hre.tasks.getTask(["test", "mocha"]).run(args);
} catch (error) {
ensureError(error);
assert.match(
error.message,
/ESM and you've programmatically run your tests twice/i,
);
}
Comment thread
alcuadrado marked this conversation as resolved.
}

it("should call build without noTests when splitTestsCompilation is false", async () => {
Comment thread
alcuadrado marked this conversation as resolved.
const { buildArgs, buildOverride } = buildArgCaptor();
const hre = await createHardhatRuntimeEnvironment({
plugins: [HardhatMochaPlugin],
tasks: [buildOverride],
});

await runMochaIgnoringEsmReRunErrors(hre);

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: [HardhatMochaPlugin],
tasks: [buildOverride],
});

await runMochaIgnoringEsmReRunErrors(hre);

assert.equal(buildArgs.length, 1);
assert.equal(buildArgs[0].noTests, true);
});

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: [HardhatMochaPlugin],
tasks: [buildOverride],
});

await runMochaIgnoringEsmReRunErrors(hre, { noCompile: true });

assert.equal(buildArgs.length, 0);
});

it("should skip compilation when noCompile is true without splitTestsCompilation", async () => {
const { buildArgs, buildOverride } = buildArgCaptor();
const hre = await createHardhatRuntimeEnvironment({
plugins: [HardhatMochaPlugin],
tasks: [buildOverride],
});

await runMochaIgnoringEsmReRunErrors(hre, { noCompile: true });

assert.equal(buildArgs.length, 0);
Comment thread
alcuadrado marked this conversation as resolved.
});
});
});
Loading