@@ -2,8 +2,11 @@ import assert from "node:assert/strict";
22import { describe , it } from "node:test" ;
33
44import { useFixtureProject } from "@nomicfoundation/hardhat-test-utils" ;
5+ import { overrideTask } from "hardhat/config" ;
56import { createHardhatRuntimeEnvironment } from "hardhat/hre" ;
67
8+ import HardhatNodeTestRunnerPlugin from "../src/index.js" ;
9+
710describe ( "Hardhat Node plugin" , ( ) => {
811 useFixtureProject ( "test-project" ) ;
912
@@ -44,4 +47,71 @@ describe("Hardhat Node plugin", () => {
4447 process . env . NODE_ENV = nodeEnv ;
4548 }
4649 } ) ;
50+
51+ describe ( "build invocation" , ( ) => {
52+ function buildArgCaptor ( ) {
53+ const buildArgs : any [ ] = [ ] ;
54+ const buildOverride = overrideTask ( "build" )
55+ . setAction ( async ( ) => {
56+ return {
57+ default : ( args : any ) => {
58+ buildArgs . push ( args ) ;
59+ return { contractRootPaths : [ ] , testRootPaths : [ ] } ;
60+ } ,
61+ } ;
62+ } )
63+ . build ( ) ;
64+ return { buildArgs, buildOverride } ;
65+ }
66+
67+ it ( "should call build without noTests when splitTestsCompilation is false" , async ( ) => {
68+ const { buildArgs, buildOverride } = buildArgCaptor ( ) ;
69+ const hre = await createHardhatRuntimeEnvironment ( {
70+ plugins : [ HardhatNodeTestRunnerPlugin ] ,
71+ tasks : [ buildOverride ] ,
72+ } ) ;
73+
74+ // The task may throw because of the ESM re-run guard, but we only
75+ // care about the build invocation args captured before that point.
76+ try {
77+ await hre . tasks . getTask ( [ "test" , "nodejs" ] ) . run ( { } ) ;
78+ } catch { }
79+
80+ assert . equal ( buildArgs . length , 1 ) ;
81+ assert . equal ( buildArgs [ 0 ] . noTests , false ) ;
82+ } ) ;
83+
84+ it ( "should call build with noTests when splitTestsCompilation is true" , async ( ) => {
85+ const { buildArgs, buildOverride } = buildArgCaptor ( ) ;
86+ const hre = await createHardhatRuntimeEnvironment ( {
87+ solidity : {
88+ version : "0.8.28" ,
89+ splitTestsCompilation : true ,
90+ } ,
91+ plugins : [ HardhatNodeTestRunnerPlugin ] ,
92+ tasks : [ buildOverride ] ,
93+ } ) ;
94+
95+ try {
96+ await hre . tasks . getTask ( [ "test" , "nodejs" ] ) . run ( { } ) ;
97+ } catch { }
98+
99+ assert . equal ( buildArgs . length , 1 ) ;
100+ assert . equal ( buildArgs [ 0 ] . noTests , true ) ;
101+ } ) ;
102+
103+ it ( "should skip compilation when noCompile is true regardless of splitTestsCompilation" , async ( ) => {
104+ const { buildArgs, buildOverride } = buildArgCaptor ( ) ;
105+ const hre = await createHardhatRuntimeEnvironment ( {
106+ plugins : [ HardhatNodeTestRunnerPlugin ] ,
107+ tasks : [ buildOverride ] ,
108+ } ) ;
109+
110+ try {
111+ await hre . tasks . getTask ( [ "test" , "nodejs" ] ) . run ( { noCompile : true } ) ;
112+ } catch { }
113+
114+ assert . equal ( buildArgs . length , 0 ) ;
115+ } ) ;
116+ } ) ;
47117} ) ;
0 commit comments