Skip to content

Commit

Permalink
refactor: separate out node warning check
Browse files Browse the repository at this point in the history
Separate out the node version warning rule to its own function and add
tests to cover the cases.
  • Loading branch information
kanej committed Jun 18, 2024
1 parent fdd3591 commit 21cbd80
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/hardhat-core/src/internal/cli/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env node
import semver from "semver";

import chalk from "chalk";

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

if (!semver.satisfies(process.version, SUPPORTED_NODE_VERSIONS.join(" || "))) {
if (isNodeVersionToWarnOn(process.version)) {
console.warn(
chalk.yellow.bold(`WARNING:`),
`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`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import semver from "semver";

import { SUPPORTED_NODE_VERSIONS } from "./constants";

/**
* Determine if the node version is unsupported by Hardhat
* and so a warning message should be shown.
*
* The current rule is that Hardhat supports all `Current`,
* `Active LTS`, and `Maintenance LTS` versions of Node.js
* as defined in the Node.js release schedule and encoded
* in the `SUPPORTED_NODE_VERSIONS` constant.
*/
export function isNodeVersionToWarnOn(nodeVersion: string): boolean {
return !semver.satisfies(nodeVersion, SUPPORTED_NODE_VERSIONS.join(" || "));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { assert } from "chai";
import { isNodeVersionToWarnOn } from "../../../src/internal/cli/is-node-version-to-warn-on";

describe("isNodeVersionToWarnOn", function () {
it("Should not warn on supported versions", function () {
assert.isFalse(isNodeVersionToWarnOn("v18.0.0"));
assert.isFalse(isNodeVersionToWarnOn("v18.20.3"));

assert.isFalse(isNodeVersionToWarnOn("v20.0.0"));
assert.isFalse(isNodeVersionToWarnOn("v20.14.0"));

assert.isFalse(isNodeVersionToWarnOn("v22.0.0"));
assert.isFalse(isNodeVersionToWarnOn("v22.3.0"));
});

it("Should warn on unsupported older node versions", function () {
assert(isNodeVersionToWarnOn("v10.0.0"));
assert(isNodeVersionToWarnOn("v10.24.1"));

assert(isNodeVersionToWarnOn("v12.0.0"));
assert(isNodeVersionToWarnOn("v12.22.12"));

assert(isNodeVersionToWarnOn("v14.0.0"));
assert(isNodeVersionToWarnOn("v14.21.3"));

assert(isNodeVersionToWarnOn("v16.0.0"));
assert(isNodeVersionToWarnOn("v16.20.20"));
});

it("Should warn on unsupported newer versions", function () {
assert(isNodeVersionToWarnOn("v24.0.0"));
assert(isNodeVersionToWarnOn("v24.3.0"));
});

it("Should warn on unsupported odd number releases", function () {
assert(isNodeVersionToWarnOn("v15.14.0"));
assert(isNodeVersionToWarnOn("v17.9.1"));
assert(isNodeVersionToWarnOn("v19.9.0"));
assert(isNodeVersionToWarnOn("v21.7.3"));
assert(isNodeVersionToWarnOn("v23.0.0"));
assert(isNodeVersionToWarnOn("v25.0.0"));
});
});

0 comments on commit 21cbd80

Please sign in to comment.