From 80a4437b8bc54452db05a0bf581a402fc1324fce Mon Sep 17 00:00:00 2001 From: John Kane Date: Wed, 8 Dec 2021 15:57:27 +0000 Subject: [PATCH 1/2] Fix reruns of `test` in scripts This is a mocha caching and cleanup issue addressed here: https://github.com/mochajs/mocha/issues/2783. Multiple runs of the test task within a hardhat script are now enabled. The resolution is `mocha.dispose` at the end of a run to clear up state. The mocha dispose method was added in mocha@7.2.0 but the @types/mocha doesn't reflect it till `9.0.0`. This commit: * bumps mocha as a dep in hardhat-core to 7.2.0 to be explicit * bumps mocha as a dev-dep everywhere else to 7.2.0 for consistency in our local dev environment * bumps @types/mocha to 9 in dev-deps in all packages to allow use of dispose and consistency across packages - no code changes are required to support this. Note that the sample typescript project already provides a default @types/mocha on version 9.0.0 Relates to #1720 --- .changeset/thirty-olives-laugh.md | 5 ++++ packages/e2e/package.json | 4 +-- .../basic-project/scripts/multi-run-test.js | 25 +++++++++++++++++ .../basic-project/test/simple.js | 5 ++++ packages/e2e/test/index.ts | 27 +++++++++++++++++++ packages/hardhat-core/package.json | 6 ++--- .../hardhat-core/src/builtin-tasks/test.ts | 2 ++ packages/hardhat-docker/package.json | 4 +-- packages/hardhat-ethers/package.json | 4 +-- packages/hardhat-etherscan/package.json | 4 +-- packages/hardhat-ganache/package.json | 4 +-- packages/hardhat-shorthand/package.json | 4 +-- packages/hardhat-solhint/package.json | 4 +-- packages/hardhat-solpp/package.json | 4 +-- packages/hardhat-truffle4/package.json | 4 +-- packages/hardhat-truffle5/package.json | 4 +-- packages/hardhat-vyper/package.json | 4 +-- packages/hardhat-waffle/package.json | 4 +-- packages/hardhat-web3-legacy/package.json | 4 +-- packages/hardhat-web3/package.json | 4 +-- yarn.lock | 10 +++---- 21 files changed, 100 insertions(+), 36 deletions(-) create mode 100644 .changeset/thirty-olives-laugh.md create mode 100644 packages/e2e/test/fixture-projects/basic-project/scripts/multi-run-test.js create mode 100644 packages/e2e/test/fixture-projects/basic-project/test/simple.js diff --git a/.changeset/thirty-olives-laugh.md b/.changeset/thirty-olives-laugh.md new file mode 100644 index 0000000000..0f62ef9778 --- /dev/null +++ b/.changeset/thirty-olives-laugh.md @@ -0,0 +1,5 @@ +--- +"hardhat": patch +--- + +Fix running the `test` task multiple times in a script (issue #1720) diff --git a/packages/e2e/package.json b/packages/e2e/package.json index 07d0c2c0ff..dc7a108458 100644 --- a/packages/e2e/package.json +++ b/packages/e2e/package.json @@ -21,7 +21,7 @@ "dependencies": { "@types/chai": "^4.2.0", "@types/fs-extra": "^5.1.0", - "@types/mocha": "^5.2.6", + "@types/mocha": "^9.0.0", "@types/node": "^12.0.0", "@types/shelljs": "^0.8.6", "@typescript-eslint/eslint-plugin": "4.29.2", @@ -32,7 +32,7 @@ "eslint-plugin-import": "2.24.1", "eslint-plugin-prettier": "3.4.0", "fs-extra": "^7.0.1", - "mocha": "^7.1.2", + "mocha": "^7.2.0", "prettier": "2.4.1", "rimraf": "^3.0.2", "shelljs": "^0.8.3", diff --git a/packages/e2e/test/fixture-projects/basic-project/scripts/multi-run-test.js b/packages/e2e/test/fixture-projects/basic-project/scripts/multi-run-test.js new file mode 100644 index 0000000000..4d2b3509e2 --- /dev/null +++ b/packages/e2e/test/fixture-projects/basic-project/scripts/multi-run-test.js @@ -0,0 +1,25 @@ +// eslint-disable-next-line import/no-extraneous-dependencies +const hre = require("hardhat"); + +async function main() { + const code = await hre.run("test"); + + if (code > 0) { + console.error("Failed first test run"); + process.exit(1); + } + + const secondCode = await hre.run("test"); + + if (secondCode > 0) { + console.error("Failed second test run"); + process.exit(1); + } +} + +main() + .then(() => process.exit(0)) + .catch((error) => { + console.error(error); + process.exit(1); + }); diff --git a/packages/e2e/test/fixture-projects/basic-project/test/simple.js b/packages/e2e/test/fixture-projects/basic-project/test/simple.js new file mode 100644 index 0000000000..2aaa973845 --- /dev/null +++ b/packages/e2e/test/fixture-projects/basic-project/test/simple.js @@ -0,0 +1,5 @@ +describe("simple", () => { + it("should pass", () => { + // throw new Error("not equal"); + }); +}); diff --git a/packages/e2e/test/index.ts b/packages/e2e/test/index.ts index 45043ea861..dadd9c40d4 100644 --- a/packages/e2e/test/index.ts +++ b/packages/e2e/test/index.ts @@ -39,6 +39,33 @@ describe("e2e tests", function () { const { code: hhCleanCode2 } = shell.exec(`${hardhatBinary} clean`); assert.equal(hhCleanCode2, 0); }); + + it("should test programmatically", function () { + // hh clean + const { code: hhCleanCode1 } = shell.exec(`${hardhatBinary} clean`); + assert.equal(hhCleanCode1, 0); + + // hh compile + const { code: testRunCode, stdout } = shell.exec( + `${hardhatBinary} run ./scripts/multi-run-test.js` + ); + assert.equal(testRunCode, 0); + + // check stdout + + // check we get passing runs + assert.match(stdout, /1 passing/); + // check we get no runs without tests + assert.notMatch( + stdout, + /0 passing/, + "A test run occured with 0 tests - potential caching issue" + ); + + // hh clean + const { code: hhCleanCode2 } = shell.exec(`${hardhatBinary} clean`); + assert.equal(hhCleanCode2, 0); + }); }); describe("sample projects", function () { diff --git a/packages/hardhat-core/package.json b/packages/hardhat-core/package.json index 792b71e39b..45226fb042 100644 --- a/packages/hardhat-core/package.json +++ b/packages/hardhat-core/package.json @@ -63,7 +63,7 @@ "@types/fs-extra": "^5.1.0", "@types/glob": "^7.1.1", "@types/lodash": "^4.14.123", - "@types/mocha": "^5.2.6", + "@types/mocha": "^9.0.0", "@types/node": "^12.0.0", "@types/node-fetch": "^2.3.7", "@types/qs": "^6.5.3", @@ -81,7 +81,7 @@ "eslint-plugin-import": "2.24.1", "eslint-plugin-prettier": "3.4.0", "ethers": "^5.0.0", - "mocha": "^7.1.2", + "mocha": "^7.2.0", "prettier": "2.4.1", "proxy": "^1.0.2", "rimraf": "^3.0.2", @@ -124,7 +124,7 @@ "lodash": "^4.17.11", "merkle-patricia-tree": "^4.2.0", "mnemonist": "^0.38.0", - "mocha": "^7.1.2", + "mocha": "^7.2.0", "node-fetch": "^2.6.0", "qs": "^6.7.0", "raw-body": "^2.4.1", diff --git a/packages/hardhat-core/src/builtin-tasks/test.ts b/packages/hardhat-core/src/builtin-tasks/test.ts index 63d7dcf8d4..1978106645 100644 --- a/packages/hardhat-core/src/builtin-tasks/test.ts +++ b/packages/hardhat-core/src/builtin-tasks/test.ts @@ -57,6 +57,8 @@ subtask(TASK_TEST_RUN_MOCHA_TESTS) mocha.run(resolve); }); + mocha.dispose(); + return testFailures; }); diff --git a/packages/hardhat-docker/package.json b/packages/hardhat-docker/package.json index a09d5acb9b..d66b299ff2 100644 --- a/packages/hardhat-docker/package.json +++ b/packages/hardhat-docker/package.json @@ -31,7 +31,7 @@ "@types/chai": "^4.2.0", "@types/dockerode": "^2.5.19", "@types/fs-extra": "^5.1.0", - "@types/mocha": "^5.2.6", + "@types/mocha": "^9.0.0", "@types/node-fetch": "^2.3.7", "@types/node": "^12.0.0", "@typescript-eslint/eslint-plugin": "4.29.2", @@ -41,7 +41,7 @@ "eslint-config-prettier": "8.3.0", "eslint-plugin-import": "2.24.1", "eslint-plugin-prettier": "3.4.0", - "mocha": "^7.1.2", + "mocha": "^7.2.0", "prettier": "2.4.1", "rimraf": "^3.0.2", "ts-node": "^8.1.0", diff --git a/packages/hardhat-ethers/package.json b/packages/hardhat-ethers/package.json index 6aa7790cb5..5ba1091ce1 100644 --- a/packages/hardhat-ethers/package.json +++ b/packages/hardhat-ethers/package.json @@ -38,7 +38,7 @@ ], "devDependencies": { "@types/chai": "^4.2.0", - "@types/mocha": "^5.2.6", + "@types/mocha": "^9.0.0", "@types/node": "^12.0.0", "@typescript-eslint/eslint-plugin": "4.29.2", "@typescript-eslint/parser": "4.29.2", @@ -49,7 +49,7 @@ "eslint-plugin-prettier": "3.4.0", "ethers": "^5.0.0", "hardhat": "^2.0.0", - "mocha": "^7.1.2", + "mocha": "^7.2.0", "prettier": "2.4.1", "rimraf": "^3.0.2", "ts-node": "^8.1.0", diff --git a/packages/hardhat-etherscan/package.json b/packages/hardhat-etherscan/package.json index fd06db4313..d534bd4a68 100644 --- a/packages/hardhat-etherscan/package.json +++ b/packages/hardhat-etherscan/package.json @@ -48,7 +48,7 @@ "@types/cbor": "^5.0.1", "@types/chai": "^4.2.0", "@types/fs-extra": "^5.1.0", - "@types/mocha": "^5.2.6", + "@types/mocha": "^9.0.0", "@types/node-fetch": "^2.3.7", "@types/node": "^12.0.0", "@types/semver": "^6.0.2", @@ -61,7 +61,7 @@ "eslint-plugin-prettier": "3.4.0", "ethers": "^5.0.8", "hardhat": "^2.0.4", - "mocha": "^7.1.2", + "mocha": "^7.2.0", "nock": "^13.0.5", "prettier": "2.4.1", "rimraf": "^3.0.2", diff --git a/packages/hardhat-ganache/package.json b/packages/hardhat-ganache/package.json index e4848ba244..0420a77acb 100644 --- a/packages/hardhat-ganache/package.json +++ b/packages/hardhat-ganache/package.json @@ -40,7 +40,7 @@ "@types/chai": "^4.2.0", "@types/debug": "^4.1.4", "@types/fs-extra": "^5.1.0", - "@types/mocha": "^5.2.6", + "@types/mocha": "^9.0.0", "@types/node": "^12.0.0", "@typescript-eslint/eslint-plugin": "4.29.2", "@typescript-eslint/parser": "4.29.2", @@ -50,7 +50,7 @@ "eslint-plugin-import": "2.24.1", "eslint-plugin-prettier": "3.4.0", "hardhat": "^2.0.0", - "mocha": "^7.1.2", + "mocha": "^7.2.0", "prettier": "2.4.1", "rimraf": "^3.0.2", "ts-interface-builder": "^0.2.0", diff --git a/packages/hardhat-shorthand/package.json b/packages/hardhat-shorthand/package.json index cfecfafe19..92ed92900f 100644 --- a/packages/hardhat-shorthand/package.json +++ b/packages/hardhat-shorthand/package.json @@ -34,7 +34,7 @@ "devDependencies": { "@types/chai": "^4.2.0", "@types/fs-extra": "^5.1.0", - "@types/mocha": "^5.2.6", + "@types/mocha": "^9.0.0", "@types/node": "^12.0.0", "@typescript-eslint/eslint-plugin": "4.29.2", "@typescript-eslint/parser": "4.29.2", @@ -44,7 +44,7 @@ "eslint-plugin-import": "2.24.1", "eslint-plugin-prettier": "3.4.0", "hardhat": "^2.0.0", - "mocha": "^7.1.2", + "mocha": "^7.2.0", "prettier": "2.4.1", "rimraf": "^3.0.2", "ts-node": "^8.1.0", diff --git a/packages/hardhat-solhint/package.json b/packages/hardhat-solhint/package.json index ba739e734b..45ccb15a0f 100644 --- a/packages/hardhat-solhint/package.json +++ b/packages/hardhat-solhint/package.json @@ -38,7 +38,7 @@ "devDependencies": { "@types/chai": "^4.2.0", "@types/fs-extra": "^5.1.0", - "@types/mocha": "^5.2.6", + "@types/mocha": "^9.0.0", "@types/node": "^12.0.0", "@typescript-eslint/eslint-plugin": "4.29.2", "@typescript-eslint/parser": "4.29.2", @@ -49,7 +49,7 @@ "eslint-plugin-prettier": "3.4.0", "fs-extra": "^7.0.1", "hardhat": "^2.0.0", - "mocha": "^7.1.2", + "mocha": "^7.2.0", "prettier": "2.4.1", "rimraf": "^3.0.2", "ts-node": "^8.1.0", diff --git a/packages/hardhat-solpp/package.json b/packages/hardhat-solpp/package.json index fdefb6f087..401e386dd7 100644 --- a/packages/hardhat-solpp/package.json +++ b/packages/hardhat-solpp/package.json @@ -39,7 +39,7 @@ "devDependencies": { "@types/chai": "^4.2.0", "@types/fs-extra": "^5.1.0", - "@types/mocha": "^5.2.6", + "@types/mocha": "^9.0.0", "@types/node": "^12.0.0", "@typescript-eslint/eslint-plugin": "4.29.2", "@typescript-eslint/parser": "4.29.2", @@ -49,7 +49,7 @@ "eslint-plugin-import": "2.24.1", "eslint-plugin-prettier": "3.4.0", "hardhat": "^2.0.0", - "mocha": "^7.1.2", + "mocha": "^7.2.0", "prettier": "2.4.1", "rimraf": "^3.0.2", "ts-node": "^8.1.0", diff --git a/packages/hardhat-truffle4/package.json b/packages/hardhat-truffle4/package.json index efd0901ee4..ebf36b3d62 100644 --- a/packages/hardhat-truffle4/package.json +++ b/packages/hardhat-truffle4/package.json @@ -42,7 +42,7 @@ "@nomiclabs/hardhat-web3-legacy": "^2.0.0", "@types/fs-extra": "^5.1.0", "@types/glob": "^7.1.1", - "@types/mocha": "^5.2.6", + "@types/mocha": "^9.0.0", "@types/node": "^12.0.0", "@typescript-eslint/eslint-plugin": "4.29.2", "@typescript-eslint/parser": "4.29.2", @@ -52,7 +52,7 @@ "eslint-plugin-import": "2.24.1", "eslint-plugin-prettier": "3.4.0", "hardhat": "^2.6.4", - "mocha": "^7.1.2", + "mocha": "^7.2.0", "prettier": "2.4.1", "rimraf": "^3.0.2", "ts-node": "^8.1.0", diff --git a/packages/hardhat-truffle5/package.json b/packages/hardhat-truffle5/package.json index 58b0e0b541..16c75f5b73 100644 --- a/packages/hardhat-truffle5/package.json +++ b/packages/hardhat-truffle5/package.json @@ -42,7 +42,7 @@ "@nomiclabs/hardhat-web3": "^2.0.0", "@types/fs-extra": "^5.1.0", "@types/glob": "^7.1.1", - "@types/mocha": "^5.2.6", + "@types/mocha": "^9.0.0", "@types/node": "^12.0.0", "@typescript-eslint/eslint-plugin": "4.29.2", "@typescript-eslint/parser": "4.29.2", @@ -52,7 +52,7 @@ "eslint-plugin-import": "2.24.1", "eslint-plugin-prettier": "3.4.0", "hardhat": "^2.6.4", - "mocha": "^7.1.2", + "mocha": "^7.2.0", "prettier": "2.4.1", "rimraf": "^3.0.2", "ts-node": "^8.1.0", diff --git a/packages/hardhat-vyper/package.json b/packages/hardhat-vyper/package.json index 0068efeaa8..fb5ec407b4 100644 --- a/packages/hardhat-vyper/package.json +++ b/packages/hardhat-vyper/package.json @@ -40,7 +40,7 @@ "@types/chai": "^4.2.0", "@types/fs-extra": "^5.1.0", "@types/glob": "^7.1.1", - "@types/mocha": "^5.2.6", + "@types/mocha": "^9.0.0", "@types/node": "^12.0.0", "@typescript-eslint/eslint-plugin": "4.29.2", "@typescript-eslint/parser": "4.29.2", @@ -50,7 +50,7 @@ "eslint-plugin-import": "2.24.1", "eslint-plugin-prettier": "3.4.0", "hardhat": "^2.0.0", - "mocha": "^7.1.2", + "mocha": "^7.2.0", "prettier": "2.4.1", "rimraf": "^3.0.2", "ts-node": "^8.1.0", diff --git a/packages/hardhat-waffle/package.json b/packages/hardhat-waffle/package.json index ea9eef0ff0..3be1f7890c 100644 --- a/packages/hardhat-waffle/package.json +++ b/packages/hardhat-waffle/package.json @@ -34,7 +34,7 @@ "@nomiclabs/hardhat-ethers": "^2.0.0", "@types/chai": "^4.2.0", "@types/fs-extra": "^5.1.0", - "@types/mocha": "^5.2.6", + "@types/mocha": "^9.0.0", "@types/node": "^12.0.0", "@typescript-eslint/eslint-plugin": "4.29.2", "@typescript-eslint/parser": "4.29.2", @@ -46,7 +46,7 @@ "ethereum-waffle": "^3.2.0", "ethers": "^5.0.0", "hardhat": "^2.0.0", - "mocha": "^7.1.2", + "mocha": "^7.2.0", "prettier": "2.4.1", "rimraf": "^3.0.2", "ts-node": "^8.1.0", diff --git a/packages/hardhat-web3-legacy/package.json b/packages/hardhat-web3-legacy/package.json index 7e608961ce..b8905e1c9c 100644 --- a/packages/hardhat-web3-legacy/package.json +++ b/packages/hardhat-web3-legacy/package.json @@ -33,7 +33,7 @@ ], "devDependencies": { "@types/chai": "^4.2.0", - "@types/mocha": "^5.2.6", + "@types/mocha": "^9.0.0", "@types/node": "^12.0.0", "@typescript-eslint/eslint-plugin": "4.29.2", "@typescript-eslint/parser": "4.29.2", @@ -43,7 +43,7 @@ "eslint-plugin-import": "2.24.1", "eslint-plugin-prettier": "3.4.0", "hardhat": "^2.0.0", - "mocha": "^7.1.2", + "mocha": "^7.2.0", "prettier": "2.4.1", "rimraf": "^3.0.2", "ts-node": "^8.1.0", diff --git a/packages/hardhat-web3/package.json b/packages/hardhat-web3/package.json index 8b37c3a6d9..44d5aa2a8f 100644 --- a/packages/hardhat-web3/package.json +++ b/packages/hardhat-web3/package.json @@ -33,7 +33,7 @@ ], "devDependencies": { "@types/chai": "^4.2.0", - "@types/mocha": "^5.2.6", + "@types/mocha": "^9.0.0", "@types/node": "^12.0.0", "@typescript-eslint/eslint-plugin": "4.29.2", "@typescript-eslint/parser": "4.29.2", @@ -43,7 +43,7 @@ "eslint-plugin-import": "2.24.1", "eslint-plugin-prettier": "3.4.0", "hardhat": "^2.0.0", - "mocha": "^7.1.2", + "mocha": "^7.2.0", "prettier": "2.4.1", "rimraf": "^3.0.2", "ts-node": "^8.1.0", diff --git a/yarn.lock b/yarn.lock index f4a113b803..f29b9d8a7a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2147,10 +2147,10 @@ dependencies: "@types/node" "*" -"@types/mocha@^5.2.6": - version "5.2.7" - resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-5.2.7.tgz#315d570ccb56c53452ff8638738df60726d5b6ea" - integrity sha512-NYrtPht0wGzhwe9+/idPaBB+TqkY9AhTvOLMkThm0IoEfLaiVQZwBwyJ5puCkO3AUCWrmcoePjp2mbFocKy4SQ== +"@types/mocha@^9.0.0": + version "9.0.0" + resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-9.0.0.tgz#3205bcd15ada9bc681ac20bef64e9e6df88fd297" + integrity sha512-scN0hAWyLVAvLR9AyW7HoFF5sJZglyBsbPuHO4fv7JRvfmPBMfp1ozWqOf/e4wwPNxezBZXRfWzMb6iFLgEVRA== "@types/node-fetch@^2.3.7", "@types/node-fetch@^2.5.5": version "2.5.11" @@ -10476,7 +10476,7 @@ mnemonist@^0.38.0: dependencies: obliterator "^1.6.1" -mocha@^7.1.2: +mocha@^7.2.0: version "7.2.0" resolved "https://registry.yarnpkg.com/mocha/-/mocha-7.2.0.tgz#01cc227b00d875ab1eed03a75106689cfed5a604" integrity sha512-O9CIypScywTVpNaRrCAgoUnJgozpIofjKUYmJhiCIJMiuYnLI6otcb1/kpW9/n/tJODHGZ7i8aLQoDVsMtOKQQ== From 7724d0ff96951425073c3edb09cf664a306c641d Mon Sep 17 00:00:00 2001 From: John Kane Date: Wed, 15 Dec 2021 15:41:50 +0000 Subject: [PATCH 2/2] Refactor to remove commented out code --- .../e2e/test/fixture-projects/basic-project/test/simple.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/e2e/test/fixture-projects/basic-project/test/simple.js b/packages/e2e/test/fixture-projects/basic-project/test/simple.js index 2aaa973845..43908218d4 100644 --- a/packages/e2e/test/fixture-projects/basic-project/test/simple.js +++ b/packages/e2e/test/fixture-projects/basic-project/test/simple.js @@ -1,5 +1,3 @@ describe("simple", () => { - it("should pass", () => { - // throw new Error("not equal"); - }); + it("should pass", () => {}); });