Skip to content

Commit 0ee442d

Browse files
committed
NODE_ENV set to "test" for all test runners
1 parent 8ec09d6 commit 0ee442d

File tree

7 files changed

+139
-0
lines changed

7 files changed

+139
-0
lines changed

.changeset/calm-toes-throw.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@nomicfoundation/hardhat-node-test-runner": patch
3+
"@nomicfoundation/hardhat-mocha": patch
4+
"hardhat": patch
5+
---
6+
7+
All test runners now set NODE_ENV to "test" in case it is not set before the tests start

v-next/hardhat-mocha/src/task-action.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ const testWithHardhat: NewTaskActionFunction<TestActionArguments> = async (
5454
// Set an environment variable that plugins can use to detect when a process is running tests
5555
process.env.HH_TEST = "true";
5656

57+
// Sets the NODE_ENV environment variable to "test" so the code can detect that tests are running
58+
// This is done by other JS/TS test frameworks like vitest
59+
process.env.NODE_ENV ??= "test";
60+
5761
setGlobalOptionsAsEnvVariables(hre.globalOptions);
5862

5963
if (!noCompile) {

v-next/hardhat-mocha/test/env.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import assert from "node:assert/strict";
2+
import { describe, it } from "node:test";
3+
4+
import { useFixtureProject } from "@nomicfoundation/hardhat-test-utils";
5+
6+
describe("Hardhat Mocha env variables", () => {
7+
useFixtureProject("test-project");
8+
9+
it("should set the NODE_ENV variable if undefined and HH_TEST always", async () => {
10+
const { createHardhatRuntimeEnvironment } = await import("hardhat/hre");
11+
const hardhatConfig = await import(
12+
"./fixture-projects/test-project/hardhat.config.js"
13+
);
14+
const hre = await createHardhatRuntimeEnvironment(hardhatConfig.default);
15+
16+
const exitCode = process.exitCode;
17+
const nodeEnv = process.env.NODE_ENV;
18+
const hhTest = process.env.HH_TEST;
19+
try {
20+
process.env.NODE_ENV = undefined;
21+
await hre.tasks.getTask(["test", "mocha"]).run({ noCompile: true });
22+
assert.equal(process.env.NODE_ENV, "test");
23+
assert.equal(process.env.HH_TEST, "true");
24+
} finally {
25+
process.env.HH_TEST = hhTest;
26+
process.env.NODE_ENV = nodeEnv;
27+
process.exitCode = exitCode;
28+
}
29+
});
30+
});

v-next/hardhat-node-test-runner/src/task-action.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ const testWithHardhat: NewTaskActionFunction<TestActionArguments> = async (
6060
// Set an environment variable that plugins can use to detect when a process is running tests
6161
process.env.HH_TEST = "true";
6262

63+
// Sets the NODE_ENV environment variable to "test" so the code can detect that tests are running
64+
// This is done by other JS/TS test frameworks like vitest
65+
process.env.NODE_ENV ??= "test";
66+
6367
setGlobalOptionsAsEnvVariables(hre.globalOptions);
6468

6569
if (!noCompile) {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import assert from "node:assert/strict";
2+
import { describe, it } from "node:test";
3+
4+
import { useFixtureProject } from "@nomicfoundation/hardhat-test-utils";
5+
import { createHardhatRuntimeEnvironment } from "hardhat/hre";
6+
7+
describe("Hardhat Node plugin", () => {
8+
useFixtureProject("test-project");
9+
10+
it("should set the NODE_ENV variable if undefined and HH_TEST always", async () => {
11+
const baseHhConfig = (
12+
await import("./fixture-projects/test-project/hardhat.config.js")
13+
).default;
14+
const hre = await createHardhatRuntimeEnvironment(baseHhConfig);
15+
16+
const exitCode = process.exitCode;
17+
const nodeEnv = process.env.NODE_ENV;
18+
const hhTest = process.env.HH_TEST;
19+
try {
20+
process.env.NODE_ENV = undefined;
21+
await hre.tasks.getTask(["test", "nodejs"]).run({ noCompile: true });
22+
assert.equal(process.env.NODE_ENV, "test");
23+
assert.equal(process.env.HH_TEST, "true");
24+
} finally {
25+
process.env.HH_TEST = hhTest;
26+
process.env.NODE_ENV = nodeEnv;
27+
process.exitCode = exitCode;
28+
}
29+
});
30+
31+
it("should not set the NODE_ENV variable if defined before", async () => {
32+
const baseHhConfig = (
33+
await import("./fixture-projects/test-project/hardhat.config.js")
34+
).default;
35+
const hre = await createHardhatRuntimeEnvironment(baseHhConfig);
36+
37+
const exitCode = process.exitCode;
38+
const nodeEnv = process.env.NODE_ENV;
39+
const hhTest = process.env.HH_TEST;
40+
try {
41+
process.env.NODE_ENV = "HELLO";
42+
await hre.tasks.getTask(["test", "nodejs"]).run({ noCompile: true });
43+
assert.equal(process.env.NODE_ENV, "HELLO");
44+
assert.equal(process.env.HH_TEST, "true");
45+
} finally {
46+
process.env.HH_TEST = hhTest;
47+
process.env.NODE_ENV = nodeEnv;
48+
process.exitCode = exitCode;
49+
}
50+
});
51+
});

v-next/hardhat/src/internal/builtin-plugins/solidity-test/task-action.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ const runSolidityTests: NewTaskActionFunction<TestActionArguments> = async (
4747
{ testFiles, chainType, grep, noCompile, verbosity },
4848
hre,
4949
) => {
50+
// Set an environment variable that plugins can use to detect when a process is running tests
51+
process.env.HH_TEST = "true";
52+
53+
// Sets the NODE_ENV environment variable to "test" so the code can detect that tests are running
54+
// This is done by other JS/TS test frameworks like vitest
55+
process.env.NODE_ENV ??= "test";
56+
5057
if (!isSupportedChainType(chainType)) {
5158
throw new HardhatError(
5259
HardhatError.ERRORS.CORE.ARGUMENTS.INVALID_VALUE_FOR_TYPE,

v-next/hardhat/test/internal/builtin-plugins/solidity-test/task-action.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,42 @@ describe("solidity-test/task-action", function () {
123123
});
124124

125125
describe("running the tests", () => {
126+
it("should set the NODE_ENV variable if undefined and HH_TEST always", async () => {
127+
hre = await createHardhatRuntimeEnvironment(hardhatConfigAllTests);
128+
129+
const exitCode = process.exitCode;
130+
const nodeEnv = process.env.NODE_ENV;
131+
const hhTest = process.env.HH_TEST;
132+
try {
133+
process.env.NODE_ENV = undefined;
134+
await hre.tasks.getTask(["test", "solidity"]).run({ noCompile: true });
135+
assert.equal(process.env.NODE_ENV, "test");
136+
assert.equal(process.env.HH_TEST, "true");
137+
} finally {
138+
process.env.HH_TEST = hhTest;
139+
process.env.NODE_ENV = nodeEnv;
140+
process.exitCode = exitCode;
141+
}
142+
});
143+
144+
it("should not set the NODE_ENV variable if defined before", async () => {
145+
hre = await createHardhatRuntimeEnvironment(hardhatConfigAllTests);
146+
147+
const exitCode = process.exitCode;
148+
const nodeEnv = process.env.NODE_ENV;
149+
const hhTest = process.env.HH_TEST;
150+
try {
151+
process.env.NODE_ENV = "HELLO";
152+
await hre.tasks.getTask(["test", "solidity"]).run({ noCompile: true });
153+
assert.equal(process.env.NODE_ENV, "HELLO");
154+
assert.equal(process.env.HH_TEST, "true");
155+
} finally {
156+
process.env.HH_TEST = hhTest;
157+
process.env.NODE_ENV = nodeEnv;
158+
process.exitCode = exitCode;
159+
}
160+
});
161+
126162
it("should run all the tests and throw if any of them fail", async () => {
127163
hre = await createHardhatRuntimeEnvironment(hardhatConfigFailingTests);
128164

0 commit comments

Comments
 (0)