Skip to content

Commit 7593837

Browse files
committed
refactor: separate out node warning check
Separate out the node version warning rule to its own function and add tests to cover the cases.
1 parent 40e9e53 commit 7593837

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

packages/hardhat-core/src/internal/cli/bootstrap.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/usr/bin/env node
2-
import semver from "semver";
2+
33
import chalk from "chalk";
44

5-
import { SUPPORTED_NODE_VERSIONS } from "./constants";
5+
import { isNodeVersionToWarnOn } from "./is-node-version-to-warn-on";
66

7-
if (!semver.satisfies(process.version, SUPPORTED_NODE_VERSIONS.join(" || "))) {
7+
if (isNodeVersionToWarnOn(process.version)) {
88
console.warn(
99
chalk.yellow.bold(`WARNING:`),
1010
`You are currently using Node.js ${process.version}, which is not supported by Hardhat. This can lead to unexpected behavior. See https://hardhat.org/nodejs-versions`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import semver from "semver";
2+
3+
import { SUPPORTED_NODE_VERSIONS } from "./constants";
4+
5+
/**
6+
* Determine if the node version is unsupported by Hardhat
7+
* and so a warning message should be shown.
8+
*
9+
* The current rule is that Hardhat supports all `Current`,
10+
* `Active LTS`, and `Maintenance LTS` versions of Node.js
11+
* as defined in the Node.js release schedule and encoded
12+
* in the `SUPPORTED_NODE_VERSIONS` constant.
13+
*/
14+
export function isNodeVersionToWarnOn(nodeVersion: string): boolean {
15+
return !semver.satisfies(nodeVersion, SUPPORTED_NODE_VERSIONS.join(" || "));
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { assert } from "chai";
2+
import { isNodeVersionToWarnOn } from "../../../src/internal/cli/is-node-version-to-warn-on";
3+
4+
describe("isNodeVersionToWarnOn", function () {
5+
it("Should not warn on supported versions", function () {
6+
assert.isFalse(isNodeVersionToWarnOn("v18.0.0"));
7+
assert.isFalse(isNodeVersionToWarnOn("v18.20.3"));
8+
9+
assert.isFalse(isNodeVersionToWarnOn("v20.0.0"));
10+
assert.isFalse(isNodeVersionToWarnOn("v20.14.0"));
11+
12+
assert.isFalse(isNodeVersionToWarnOn("v22.0.0"));
13+
assert.isFalse(isNodeVersionToWarnOn("v22.3.0"));
14+
});
15+
16+
it("Should warn on unsupported older node versions", function () {
17+
assert(isNodeVersionToWarnOn("v10.0.0"));
18+
assert(isNodeVersionToWarnOn("v10.24.1"));
19+
20+
assert(isNodeVersionToWarnOn("v12.0.0"));
21+
assert(isNodeVersionToWarnOn("v12.22.12"));
22+
23+
assert(isNodeVersionToWarnOn("v14.0.0"));
24+
assert(isNodeVersionToWarnOn("v14.21.3"));
25+
26+
assert(isNodeVersionToWarnOn("v16.0.0"));
27+
assert(isNodeVersionToWarnOn("v16.20.20"));
28+
});
29+
30+
it("Should warn on unsupported newer versions", function () {
31+
assert(isNodeVersionToWarnOn("v24.0.0"));
32+
assert(isNodeVersionToWarnOn("v24.3.0"));
33+
});
34+
35+
it("Should warn on unsupported odd number releases", function () {
36+
assert(isNodeVersionToWarnOn("v15.14.0"));
37+
assert(isNodeVersionToWarnOn("v17.9.1"));
38+
assert(isNodeVersionToWarnOn("v19.9.0"));
39+
assert(isNodeVersionToWarnOn("v21.7.3"));
40+
assert(isNodeVersionToWarnOn("v23.0.0"));
41+
assert(isNodeVersionToWarnOn("v25.0.0"));
42+
});
43+
});

0 commit comments

Comments
 (0)